android 通过url获取JSON数据
publicstaticJSONObjectdoGet(Stringurl){try{Stringresult=null;DefaultHttpClienthttpCli...
public static JSONObject doGet(String url) {
try {
String result = null;
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpGet request = new HttpGet(url);
HttpResponse response = httpClient.execute(request);
result = EntityUtils.toString(response.getEntity());
JSONObject object = new JSONObject(result);
return object;
} catch (Exception e) {
// TODO: handle exception
}
return null;
}
这代码有什么问题?返回值为空了。求高手指教 展开
try {
String result = null;
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpGet request = new HttpGet(url);
HttpResponse response = httpClient.execute(request);
result = EntityUtils.toString(response.getEntity());
JSONObject object = new JSONObject(result);
return object;
} catch (Exception e) {
// TODO: handle exception
}
return null;
}
这代码有什么问题?返回值为空了。求高手指教 展开
1个回答
2013-04-14
展开全部
原因应该是没有以流的方式来接收,,另外取的地方不对,,应该是要取得包体的内容,,而非包体
if (httpRes.getStatusLine().getStatusCode() == 200) {//这个是判定是否有正常返回
buffer = new BufferedReader(new InputStreamReader(httpRes
.getEntity().getContent()));
for (String s = buffer.readLine(); s != null; s = buffer
.readLine()) {
str.append(s);
}
buffer.close();
JSONObject json = new JSONObject(str.toString());
if (httpRes.getStatusLine().getStatusCode() == 200) {//这个是判定是否有正常返回
buffer = new BufferedReader(new InputStreamReader(httpRes
.getEntity().getContent()));
for (String s = buffer.readLine(); s != null; s = buffer
.readLine()) {
str.append(s);
}
buffer.close();
JSONObject json = new JSONObject(str.toString());
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询