• 关于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(10000);//设置连接超时
                urlConnection.setReadTimeout(30000);
    //            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(0);
                // 获取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() == 200) {
                    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":"00000000" } }

    后面的是一个二维的json

    一开始我以为

    JSONObject jsonObject1 = new JSONObject();
                jsonObject1.put("mobile","0000000000");
                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","0000000000");
                //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","00000000000");
    
    
                Map map2 = new HashMap<String,Object>();
                map2.put("cmd","check_mobile");
                map2.put("param",map1);
    
                String Data = new JSONObject(map2).toString();

     晚安

  • 相关阅读:
    js时间操作扩展方法
    C#异步调用与线程总结
    ERP笔记5系统服务水平
    HtmlAgilityPack不能解析<select><option>的解决方法
    qq掉线咒语:aidusort.as
    情商的管理
    错误:ORA01036: 非法的变量名/编号 的多种解决办法
    安装MySQLdb时出错:EnvironmentError: mysql_config not found
    Ubuntu12.04安装maven
    (转)ASP.NET 3.5 企业级开发
  • 原文地址:https://www.cnblogs.com/yangfengwu/p/9684283.html
Copyright © 2020-2023  润新知