private JSONObject httpGet(String path) {
try {
// 创建HttpClient实例
HttpClient httpclient = new DefaultHttpClient();
// 创建Get方法实例
HttpGet httpgets = new HttpGet(path);
HttpResponse response = httpclient.execute(httpgets);
HttpEntity entity = response.getEntity();
if (entity != null) {
InputStream instreams = entity.getContent();
String str = convertStreamToString(instreams);
JSONObject json = JSONObject.fromObject(str);
httpgets.abort();
return json;
}
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
try {
// 创建HttpClient实例
HttpClient httpclient = new DefaultHttpClient();
// 创建Get方法实例
HttpGet httpgets = new HttpGet(path);
HttpResponse response = httpclient.execute(httpgets);
HttpEntity entity = response.getEntity();
if (entity != null) {
InputStream instreams = entity.getContent();
String str = convertStreamToString(instreams);
JSONObject json = JSONObject.fromObject(str);
httpgets.abort();
return json;
}
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
本文介绍了一种使用Java通过HTTP GET方式获取远程路径数据的方法,并详细展示了如何将输入流转换为JSON对象的过程。该方法首先创建HttpClient实例,然后通过HttpGet发送请求,最后从返回的实体中提取内容并转换为JSON格式。
492

被折叠的 条评论
为什么被折叠?



