HttpURLConnection传json

private static String sendToWangTing(DataRow dataRow) throws IOException{
        String ip = Configuration.getString("JZWangTing.ip");
        String port = Configuration.getString("JZWangTing.port"); 
        String service_id = Configuration.getString("JZWangTing.service_id"); 
        String url = Configuration.getString("JZWangTing.url"); 
        
        Map REQUESTS=new HashMap<String,String>();
        List list=new ArrayList();
        Map REQ_MSG_HDR=new HashMap<String,String>();
        REQ_MSG_HDR.put("OP_CODE", "8888");              //服务号
        REQ_MSG_HDR.put("OP_WAY", "1");                  //服务号
        REQ_MSG_HDR.put("MSG_ID", service_id);          //服务号
        REQ_MSG_HDR.put("OP_LANGUAGE", "1");          //服务号
        REQ_MSG_HDR.put("OP_CHANNEL", dataRow.get("channel_no"));              //渠道号
        REQ_MSG_HDR.put("SESSION_ID", "");              //sessionID
        
        
        Map REQ_COMM_DATA=new HashMap<String,String>();
        
        REQ_COMM_DATA.put("USER_CODE", dataRow.get("user_uuid"));            //用户唯一码
        REQ_COMM_DATA.put("service", service_id);                            //服务号
        REQ_COMM_DATA.put("CUST_CODE", dataRow.get("client_id"));            //客户号
        REQ_COMM_DATA.put("ACCESS_CHANNEL", dataRow.get("channel_no"));        //渠道编号
        REQ_COMM_DATA.put("ORGID", dataRow.get("branch_no"));                //营业部编号
        
        Map map1=new HashMap<String, Object>();
        map1.put("REQ_MSG_HDR", REQ_MSG_HDR);
        map1.put("REQ_COMM_DATA", REQ_COMM_DATA);
        //list.add(map2);
        list.add(map1);
        
        REQUESTS.put("REQUESTS",list);
        
        String date =JSONObject.fromObject(REQUESTS).toString();//转化成json
        System.out.println(date);
        
        String returninfo = "";
        
        URL httpUrl = new URL("http://"+ip+":"+port+url); 
        HttpURLConnection huc = (HttpURLConnection) httpUrl.openConnection(); 
        huc.setRequestMethod("POST");
        huc.setDoInput(true);  
        huc.setDoOutput(true);
        huc.setRequestProperty("Content-Type",  "application/json");  
        //链接地址  
        huc.connect(); 
        OutputStreamWriter writer = new OutputStreamWriter(huc.getOutputStream());  
        //发送参数  
        writer.write(date); 
      //清理当前编辑器的左右缓冲区,并使缓冲区数据写入基础流  
        writer.flush(); 
        BufferedReader br = new BufferedReader(new InputStreamReader(huc.getInputStream(),"UTF-8")) ;
        String line;
        Map js;
        while ((line = br.readLine()) != null) {
            returninfo = line;
            js=(Map)JSONObject.fromObject(returninfo); 
            System.out.println(js.toString());
        }        
        huc.connect();   
        br.close();
        return returninfo;
    }
    

发送数据:

{"REQUESTS":[{"REQ_COMM_DATA":{"USER_CODE":"EF48C567B74E4F8D952628EEDAF2C765","service":"B110004","CUST_CODE":"10100564286","ACCESS_CHANNEL":"1","ORGID":"0101"},"REQ_MSG_HDR":{"OP_WAY":"1","OP_CODE":"8888","MSG_ID":"B110004","OP_LANGUAGE":"1","SESSION_ID":"","OP_CHANNEL":"3"}}]}

返回数据
{"ANSWERS":[{"ANS_MSG_HDR":{"MSG_CODE":"999","MSG_TEXT":"","MSG_LEVEL":"","RESULT_NUM":"2","MSG_ID":"","PACKAGE_FLAG":"0","DATA_ROWS":"0","BPM_ROWS":"0","RUN_TIMES":"\"null\"","MSG_TRACE":""},"ANS_COMM_DATA":[{"BPM_DATA":[]},{"CONFIG":{}}]}]}

posted on 2016-09-02 16:51  小人物的奋斗  阅读(9025)  评论(0编辑  收藏  举报

导航