码迷,mamicode.com
首页 > Web开发 > 详细

json协议

时间:2018-12-25 11:40:50      阅读:643      评论:0      收藏:0      [点我收藏+]

标签:writer   协议   exce   mss   line   system   cep   uil   编码   

json对象-json字符串-编码byte[] -传输-解码为字符串-对象

client:

 

    String wholeUrl = String.format("%s?sdkappid=%d&random=%d", URL, SDKAPPID, rnd);

        java.net.URL object = new URL(wholeUrl);
        HttpURLConnection con = (HttpURLConnection) object.openConnection();
        con.setDoOutput(true);
        con.setDoInput(true);
        con.setRequestProperty("Content-Type", "application/json");
        con.setRequestProperty("Accept", "application/json");
        con.setRequestMethod("POST");
        JSONObject data = new JSONObject();
        JSONObject tel = new JSONObject();
        tel.put("nationcode", nationCode);
        String phone = phoneNumber;
        tel.put("phone", phone);
        data.put("type", "0");
        data.put("msg", content);
        String sig = stringMD5(APPKEY.concat(phone));
        data.put("sig", sig);
        data.put("tel", tel);
        OutputStreamWriter wr = new OutputStreamWriter(con.getOutputStream(), "utf-8");
        wr.write(data.toString());
        wr.flush();

        // 显示 POST 请求返回的内容
        StringBuilder sb = new StringBuilder();
        int HttpResult = con.getResponseCode();
        if (HttpResult == HttpURLConnection.HTTP_OK)
        {
            BufferedReader br = new BufferedReader(new InputStreamReader(con.getInputStream(), "utf-8"));
            String line;
            while ((line = br.readLine()) != null)
            {
                sb.append(line + "\n");
            }
            br.close();
            JSONObject reObj = new JSONObject(sb.toString());
            String res = (String)reObj.get("result");
            if(!"0".equals(res)) {
                System.out.println("" + sb.toString());
                LOGGER.error("SmsSender::sendMsg" + "-" + phoneNumber + ","+(String)reObj.get("errmsg"));
            } else {

            }


        } else
        {
            LOGGER.error("send failed and rps is: " + con.getResponseMessage());
            throw new SmsException("send failed and rps is: " + con.getResponseMessage());
        }

 

OutputStreamWriter wr = new OutputStreamWriter(con.getOutputStream(), "utf-8");
wr.write(data.toString());
while ((line = br.readLine()) != null)
{
sb.append(line + "\n");
}
br.close();
JSONObject reObj = new JSONObject(sb.toString());

 

 

 

 

server:

 

json协议

标签:writer   协议   exce   mss   line   system   cep   uil   编码   

原文地址:https://www.cnblogs.com/silyvin/p/10172445.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!