关于Http 传输二维json
传输一维的好说
public static String doPost(String url) {
String result = new String();
OutputStream out = null;
InputStream in = null;
try {
URL realUrl = new URL(url);
HttpURLConnection urlConnection = (HttpURLConnection) realUrl.openConnection();
urlConnection.setConnectTimeout();//设置连接超时
urlConnection.setReadTimeout();
// urlConnection.setRequestProperty("ser-Agent", "Fiddler");
urlConnection.setRequestProperty("connection", "Keep-Alive");
//urlConnection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
//urlConnection.setRequestProperty("Content-Type", "text/html; charset=UTF-8");
urlConnection.setRequestProperty("Content-Type", "application/json");
urlConnection.setRequestMethod("POST");
// 发送POST请求必须设置如下两行
urlConnection.setDoOutput(true);
urlConnection.setDoInput(true);
urlConnection.setChunkedStreamingMode();
// 获取URLConnection对象对应的输出流
out = new BufferedOutputStream(urlConnection.getOutputStream());
// 发送请求参数
// String postContent =
// URLEncoder.encode("userName", "UTF-8") + "=" +
// URLEncoder.encode("admin", "UTF-8") + "&" +
// URLEncoder.encode("passWord", "UTF-8") + "="+
// URLEncoder.encode("admin", "UTF-8") ;
JSONObject jsonObject = new JSONObject();
jsonObject.put("cmd","mobile");
jsonObject.put("param","00000000000");
Data = jsonObject.toString();
//String json = java.net.URLEncoder.encode(jsonObject.toString(), "utf-8");
Log.e("数据", Data );
// String postContent ="cmd="
// +URLEncoder.encode("check_mobile", "utf-8") + "&" +
// "mobile="+
// URLEncoder.encode("18363890557", "utf-8");
//Log.e("err",postContent);
out.write(Data.getBytes());
out.flush();
out.close();
if (urlConnection.getResponseCode() == ) {
InputStreamReader inputStreamReader = new InputStreamReader(urlConnection.getInputStream(),"UTF-8");
BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
String ee=null;
while ((ee = bufferedReader.readLine())!=null)
{
result = ee;
Log.e("err","接收到数据="+result);
}
}else {
result = null;
}
} catch (Exception e) {
Log.e("err",e+"");
} finally {//使用finally块来关闭输出流、输入流
try {
if (out != null) {
out.close();
}
if (in != null) {
in.close();
}
} catch (IOException ex) {
ex.printStackTrace();
}
}
return result;
}
然后直接用任务
new Thread(new Runnable() {
@Override
public void run () {
String sss = doPost("http://具体的地址");
//Log.e("err","Read="+sss);
}
}).start();
现在说二维的
假设要封装成这样
{ "cmd":"check_mobile", "param":{ "mobile":"" } }
后面的是一个二维的json
一开始我以为
JSONObject jsonObject1 = new JSONObject();
jsonObject1.put("mobile","");
String str = jsonObject1.toString(); JSONObject jsonObject = new JSONObject();
jsonObject.put("cmd","check_mobile");
jsonObject.put("param",str);
Data = jsonObject.toString();
不可以....发现数据是这样的

然后就在想是不是 String str = jsonObject1.toString(); jsonObject.put("param",str); 不应该这样
所以
JSONObject jsonObject1 = new JSONObject();
jsonObject1.put("mobile","");
//String str = jsonObject1.toString(); JSONObject jsonObject = new JSONObject();
jsonObject.put("cmd","check_mobile");
jsonObject.put("param",jsonObject1);
Data = jsonObject.toString();

这样就可以了
后来尝试了其它各种方式
发现用Map也是可以的
Map map1 = new HashMap<String,String>();
map1.put("mobile",""); Map map2 = new HashMap<String,Object>();
map2.put("cmd","check_mobile");
map2.put("param",map1); String Data = new JSONObject(map2).toString();
晚安
关于Http 传输二维json的更多相关文章
- json 拼二维json数组
js声明数组 以及向数组中添加as移除json数据 JavaScript声明JSON数组的方法: //部分条件,在数据渲上数据要求是数组格式而非json数组格式,取arrayJson.dataList ...
- ASP.NET实现二维码 ASP.Net上传文件 SQL基础语法 C# 动态创建数据库三(MySQL) Net Core 实现谷歌翻译ApI 免费版 C#发布和调试WebService ajax调用WebService实现数据库操作 C# 实体类转json数据过滤掉字段为null的字段
ASP.NET实现二维码 using System;using System.Collections.Generic;using System.Drawing;using System.Linq;us ...
- jquery解析php通过ajax传过来的json二维数组对象
ajax获得php传过来的json二维数组对象,jquery解析 php代码: <?php $news = array( '武汉'=>array(1,2,3), '广州'=>arra ...
- ***php解析JSON二维数组字符串(json_decode函数第二个参数True和False的区别)
客户端的请求体中的数据:[{"msg_id": 1, "msg_status": "HAS_READ" }, { "msg_id& ...
- 二维数组的传输 (host <-> device)
前言 本文的目的很明确:介绍如何将二维数组传递进显存,以及如何将二维数组从显存传递回主机端. 实现步骤 1. 在显存中为二维数组开辟空间 2. 获取该二维数组在显存中的 pitch 值 (cudaMa ...
- dbda封装类(包括:返回二维数组、Ajax调用返回字符串、Ajax调用返回JSON)
<?php class DBDA { public $host = "localhost"; public $uid = "root"; public $ ...
- 第六篇:二维数组的传输 (host <-> device)
前言 本文的目的很明确:介绍如何将二维数组传递进显存,以及如何将二维数组从显存传递回主机端. 实现步骤 1. 在显存中为二维数组开辟空间 2. 获取该二维数组在显存中的 pitch 值 (cudaMa ...
- json-lib解析json之二维JSONArray
在接口测试中,不管是接口的请求报文还是接口的响应报文,数据全部使用json,在工作中避免不了去解析响应报文以获取某个键的值,解析josn有两种方式,一种是利用jackson,还有一种就是利用json- ...
- HTML-DEV-ToolLink(常用的在线字符串编解码、代码压缩、美化、JSON格式化、正则表达式、时间转换工具、二维码生成与解码等工具,支持在线搜索和Chrome插件。)
HTML-DEV-ToolLink:https://github.com/easonjim/HTML-DEV-ToolLink 常用的在线字符串编解码.代码压缩.美化.JSON格式化.正则表达式.时间 ...
随机推荐
- 后台返回xml格式转json
之前后台做了一个xml格式的数据返回给前端,这个可愁坏了我,不过现在还是解决了,虽然方法有点笨,但没有找到其他的方法,先将就着用吧. 后台返回的是这样的: 那么我们就要这样处理:commonMetho ...
- wap2app(三)-- 添加引导页
1.在client_index.html文件中添加如下代码: <script type="text/javascript"> if(window.plus){ plus ...
- (后端)Java新人入职——配置环境及安装开发工具(完全)
转自csdn:执笔记忆的空白 很多新人对于进入新公司,相关工具的安装和环境变量的设定很苦恼.又苦于没有完整的配置开发环境的资料,我这里写一篇操作步骤的案例, 至少让你能把开发工具安装起来,并实用起来, ...
- HBase中此类异常解决记录org.apache.hadoop.ipc.RemoteException(java.io.IOException):
ERROR: Can't get master address from ZooKeeper; znode data == null 一定注意这只是问题的第一层表象,真的问题是: File /hb ...
- Linux 运行进程实时监控pidstat命令详解
简介 pidstat主要用于监控全部或指定进程占用系统资源的情况,如CPU,内存.设备IO.任务切换.线程等.pidstat首次运行时显示自系统启动开始的各项统计信息,之后运行pidstat将显示自上 ...
- Python scikit-learn (metrics): difference between r2_score and explained_variance_score?
I noticed that that 'r2_score' and 'explained_variance_score' are both build-in sklearn.metrics meth ...
- January 30th, 2018 Week 05th Tuesday
The things you own end up owning you. 你占有的东西终将会占有你. When we are longing for something, we would be w ...
- 【Beta Scrum】冲刺!4/5
1. 今日完成情况 人员 学号 分工 是否完成 完成情况 胡武成 031502610 完成登录注册api,协助web端完成api Y 基本逻辑写完,短信部分还在修改中,预计明天早上便能完成 郭剑南 0 ...
- oracle 12c 12.1.0.2.0 BUG 22562145
Wed May 23 17:46:14 2018TT01: Standby redo logfile selected for thread 1 sequence 42251 for destinat ...
- 【转】图像的上采样(upsampling)与下采样(subsampled)
转自:https://blog.csdn.net/stf1065716904/article/details/78450997 参考: http://blog.csdn.net/majinlei121 ...