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

接口访问-并返回json字符串

时间:2018-03-08 12:03:04      阅读:219      评论:0      收藏:0      [点我收藏+]

标签:json   output   expr   exp   edr   method   ack   int   code   

String requestUrl = "http://www.kuaidi100.com/query?type="+express2.getCode()+"&postid="+express2.getExpress_number();
JSONObject jsonObject = null;
StringBuffer buffer = new StringBuffer();
try {

URL url = new URL(requestUrl);
// http协议传输
HttpURLConnection httpUrlConn = (HttpURLConnection) url.openConnection();

httpUrlConn.setDoOutput(true);
httpUrlConn.setDoInput(true);
httpUrlConn.setUseCaches(false);
// 设置请求方式(GET/POST)
httpUrlConn.setRequestMethod("POST");
httpUrlConn.connect();
// 将返回的输入流转换成字符串
InputStream inputStream = httpUrlConn.getInputStream();
InputStreamReader inputStreamReader = new InputStreamReader(inputStream, "utf-8");
BufferedReader bufferedReader = new BufferedReader(inputStreamReader);

String str = null;

while ((str = bufferedReader.readLine()) != null) {
buffer.append(str);
}
bufferedReader.close();
inputStreamReader.close();
// 释放资源
inputStream.close();
inputStream = null;
httpUrlConn.disconnect();
jsonObject = JSONObject.fromObject(buffer.toString());
} catch (Exception e) {
e.printStackTrace();
}
if(jsonObject.get("message").equals("ok")){
map.put("expressInfos", jsonObject.get("data"));
}

接口访问-并返回json字符串

标签:json   output   expr   exp   edr   method   ack   int   code   

原文地址:https://www.cnblogs.com/hu-net/p/8527150.html

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