如果 JSON 字符串非常大,我会看到一个问题,因为它根本无法获取所有内容。
所以我有一个基于流行示例的 JSONparsing 类:
public String makeServiceCall(String url, int method,
List<NameValuePair> params) {
try {
// http client
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpEntity httpEntity = null;
HttpResponse httpResponse = null;
// Checking http request method type
if (method == POST) {
HttpPost httpPost = new HttpPost(url);
// adding post params
if (params != null) {
httpPost.setEntity(new UrlEncodedFormEntity(params));
}
httpResponse = httpClient.execute(httpPost);
} else if (method == GET) {
// appending params to url
if (params != null) {
String paramString = URLEncodedUtils
.format(params, "utf-8");
url += "?" + paramString;
}
HttpGet httpGet = new HttpGet(url);
httpResponse = httpClient.execute(httpGet);
}
httpEntity = httpResponse.getEntity();
if (httpEntity != null) {
try {
response= new String(EntityUtils.toString(httpEntity));
Log.d(TAG, response);
}
catch (ParseException exc) {
// TODO Auto-generated catch block
exc.printStackTrace();
} catch (IllegalStateException exc) {
// TODO Auto-generated catch block
exc.printStackTrace();
}
}
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return response;
}
现在发生的是那行代码
response= new String(EntityUtils.toString(httpEntity));
返回一个不完整的字符串。如果我使 JSON 字符串更小,那么它就可以了。
有谁知道我必须做什么才能让 EntityUtils 返回我所有的东西或给它一个更大的缓冲区。
谢谢