zoukankan      html  css  js  c++  java
  • Json传值

    一、POST传值方法

    public static String SendUlr(String requestUrl,String xml) {
    	   	 String responseStr="";
    	       try {
    	           URL url = new URL(requestUrl);
    	           HttpURLConnection urlConnection = (HttpURLConnection)url.openConnection();
    	           urlConnection.setDoOutput(true);
    	           urlConnection.setDoInput(true);
    	           urlConnection.setRequestMethod("POST");
    	           urlConnection.setUseCaches(false); 
    	           urlConnection.setInstanceFollowRedirects(true);
    	           urlConnection.setRequestProperty("Content-Type","application/x-www-form-urlencoded");
    	           urlConnection.setRequestProperty("Charset", "UTF-8"); 
    	           urlConnection.connect();
    	           DataOutputStream out = new DataOutputStream(urlConnection.getOutputStream());
    	           String params= xml;       
    	           out.write(params.getBytes("UTF-8"));
    	         /*  out.writeBytes(params);*/
    	           out.flush();
    	           out.close(); 
    	           BufferedReader reader = new BufferedReader(new InputStreamReader(urlConnection.getInputStream(),"UTF-8"));
    	           String line;
    	           while ((line = reader.readLine()) != null){
    	               responseStr+=line;
    	           }
    	           reader.close();
    	           urlConnection.disconnect();
    	       }catch (MalformedURLException e) {
    	           // TODO Auto-generated catch block
    	           e.printStackTrace();
    	       } catch (ProtocolException e) {
    	           // TODO Auto-generated catch block
    	           e.printStackTrace();
    	       } catch (IOException e) {
    	           // TODO Auto-generated catch block
    	           e.printStackTrace();
    	       }
    	       return responseStr;
    	   }
    

    二、接口访问,返回json对象

    PS://{"stu":"11","stuName":"w","sex":"男"}
    String url = "http://localhost:8080/yikang/q/yikang/YtxHaixun/test";
    String par = "a="+"123";
    String str = YikangUtil.SendUlr(url, par);
    JSONObject jsonp = JSON.parseObject(str);
    System.out.println(jsonp.get("id"));
    

    三、接口访问,返回json集合

    	//PS:[{"stu":"11","stuName":"w","sex":"男"},
                    {"stu":"11","stuName":"w","sex":"男"},
                    {"stu":"11","stuName":"w","sex":"男"}]
    	String url = "http://localhost:8080/yikang/q/yikang/YtxHaixun/test";
    	String par = "a="+"123";
    	String str = YikangUtil.SendUlr(url, par);
         //获取集合 JSONArray parseArray = JSONArray.parseArray(str); int size = parseArray.size(); for(int i=0;i<size;i++){
           //获取对象里面的值   String id = (String)parseArray.getJSONObject(i).get("id");   System.out.println("==="+id); }

      

      

  • 相关阅读:
    康复计划
    Leetcode 08.02 迷路的机器人 缓存加回溯
    Leetcode 38 外观数列
    Leetcode 801 使序列递增的最小交换次数
    Leetcode 1143 最长公共子序列
    Leetcode 11 盛水最多的容器 贪心算法
    Leetcode 1186 删除一次得到子数组最大和
    Leetcode 300 最长上升子序列
    Leetcode95 不同的二叉搜索树II 精致的分治
    Leetcode 1367 二叉树中的列表 DFS
  • 原文地址:https://www.cnblogs.com/superxff/p/9220952.html
Copyright © 2011-2022 走看看