传输一维的好说
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();
晚安