json协议
Posted 无
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了json协议相关的知识,希望对你有一定的参考价值。
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 + " "); } 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 + "
");
}
br.close();
JSONObject reObj = new JSONObject(sb.toString());
server:
以上是关于json协议的主要内容,如果未能解决你的问题,请参考以下文章
json 可视代码工作室Angular with Firebase片段
错误代码:错误域 = NSCocoaErrorDomain 代码 = 3840“JSON 文本没有以数组或对象和允许未设置片段的选项开头。”