分享web开发知识

注册/登录|最近发布|今日推荐

主页 IT知识网页技术软件开发前端开发代码编程运营维护技术分享教程案例
当前位置:首页 > 网页技术

json协议

发布时间:2023-09-06 02:27责任编辑:白小东关键词:jsjson

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协议

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

知识推荐

我的编程学习网——分享web前端后端开发技术知识。 垃圾信息处理邮箱 tousu563@163.com 网站地图
icp备案号 闽ICP备2023006418号-8 不良信息举报平台 互联网安全管理备案 Copyright 2023 www.wodecom.cn All Rights Reserved