关于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格式化.正则表达式.时间 ...
随机推荐
- canvas-0scale.html
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- javaSE总结
1 java的历史 1991-至今 詹姆斯-高斯林 SUN公司 ORACLE 2009年 2 java的版本 javaSE java的标准桌面级开发 javaEE 企业级web开发 javaM ...
- Python爬虫html解析工具beautifulSoup在pycharm中安装及失败的解决办法
1.安装步骤: 首先,你要先进入pycharm的Project Interpreter界面,进入方法是:setting(ctrl+alt+s) ->Project Interpreter,Pro ...
- 【20181025】win10下Python安装osmnx包
系统:win10 64位 Python:3.7 在网上查了很多资料,主要有两种方法安装osmnx包,一种是通过anaconda安装,这种方法会自动帮你装好osmnx的依赖包:另一种是用pip安装,需要 ...
- 访问WEB-INF下的jsp页面
访问web-inf下的jsp文件, 1)使用springMVC,一般都会使用springMVC的视图解析器,大概会这样配置 <!--jsp视图解析器--> <bean class ...
- 读懂SAP Leonardo物联网平台
读懂SAP Leonardo物联网平台 https://blog.csdn.net/weixin_42137700/article/details/81903290 本文比较系统.全面地介绍了SAP ...
- Oracle 11g即时客户端在windows下的配置
Oracle 11g即时客户端在windows下的配置 by:授客QQ:1033553122 instantclient-basic-nt-11.2.0.3.0.zip客户端压缩包为例 步骤 1. 假 ...
- 安卓界面之Viewpager和Tablayout实现滑动界面
摘要:六部实现选项卡界面 一. 在gradle文件添加以下代码: implementation 'com.android.support:design:28.0.0' 在gradle文件添加以上代码后 ...
- Oracle SQL: DDL DML DCL TCL
Data Definition Language 自带commit,与表结构有关(数据字典)(会等待对象锁) Data Manipulation Language (数据文件相关变化有关,会产生锁)不 ...
- Jenkins 自动发布 Spring Boot 项目(Gitee)
1.下载 wget http://mirrors.jenkins.io/war-stable/latest/jenkins.war,并部署到tomcat下 2.机器安装好 java ,maven ,g ...