1
我想从this URL得到JSON,但由于JSON很大,服务器加载JSON的速度很慢。 我得到JSON从网上这段代码:没有收到完整的数据
public JSONObject getJSONFromUrl(String url, List<NameValuePair> params) {
// Making HTTP request
try {
// defaultHttpClient
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(url);
if (params != null)
httpPost.setEntity(new UrlEncodedFormEntity(params));
HttpResponse httpResponse = httpClient.execute(httpPost);
HttpEntity httpEntity = httpResponse.getEntity();
is = httpEntity.getContent();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
try {
BufferedReader reader = new BufferedReader(new InputStreamReader(
is, "UTF-8"), 8);
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
}
Log.i("TAG", sb.toString());
is.close();
json = sb.toString();
} catch (Exception e) {
Log.i("TAG", "Error converting result " + e.toString());
}
// try parse the string to a JSON object
try {
jObj = new JSONObject(json);
} catch (JSONException e) {
Log.i("TAG", "Error parsing data " + e.toString());
}
// return JSON String
return jObj;
}
但我可以只拿到第一部分为JSON和我JSON仍未完成 请帮助我。
你确定这是你的有效的JSON响应? – Setu
是的,这是有效的 – Alireza
Logcat可能无法完全打印大消息..你可以尝试使用system.out.print(message)。 –