首页 > Web开发 > 详细

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

时间:2018-03-08 11:44:56      阅读:235      评论:0      收藏:0      [点我收藏+]

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字符串

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

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!