我试图从我们的服务器获取 JSON 响应,当字符串长度达到大约 5525 个字符时,响应字符串似乎总是被截断。
HttpClient httpClient = new DefaultHttpClient();
HttpPost post = new HttpPost(URL);
ResponseHandler<String> responseHandler= new BasicResponseHandler();
String testResponse = httpClient.execute(post, responseHandler);
我还通过使用 HttpEntity 并读取响应流进行了尝试。但这也会将字符串截断为大约该长度。
HttpClient httpClient = new DefaultHttpClient();
HttpPost post = new HttpPost(URL);
// HttpGet get = new HttpGet(URL);
HttpResponse response = null;
HttpEntity entity = null;
InputStream inputStream = null;
BufferedReader reader = null;
String result = "";
try {
response = (HttpResponse)httpClient.execute(post);
entity = response.getEntity();
if(entity != null){
inputStream = entity.getContent();
}
reader = new BufferedReader(new InputStreamReader(inputStream), 8000);
StringBuffer builder = new StringBuffer("");
String line = reader.readLine();
while(line != null){
Log.v(tag, "int max::::::::: "+Integer.MAX_VALUE);
Log.v(tag, "LINE::::::::: "+line+reader.toString());
Log.v(tag, "reader::::::::: "+reader.toString());
builder.append(line+"\n");
line = reader.readLine();
}
inputStream.close();
result = builder.toString();
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally{
if(inputStream != null){
try{
inputStream.close();
}catch(IOException e){
e.printStackTrace();
}
}
}
请让我知道如何处理这个问题。
我在创建此文章时使用了这篇文章作为参考。http://senior.ceng.metu.edu.tr/2009/praeda/2009/01/11/a-simple-restful-client-at-android/
谢谢。
首先感谢大家的回复。
我刚刚发现问题不在于我的代码,而在于 logcat 显示的行数。
问题是我的 json 回复格式有问题,并且我的解析代码出现错误。所以我开始尝试通过在 logcat 中打印响应 json 字符串来调试它。这总是被截断,到目前为止我一直猜测来自服务器本身的响应被截断。
因此,今天我开始使用响应字符串生成器,并且必须编写有趣的片段来计算字符并检测我期望的位置处的字符,我发现响应已完全返回。
我还在其他一些大字符串上测试了我的代码,我发现 logcat 对它显示的字符串的长度有限制,或者至少看起来是这样。这就是为什么我的回复显示为截断的字符串,我认为这是我的代码的问题。
所以它工作正常,因为代码较早,唯一的问题是
logcat 不显示完整的字符串。但事实并非如此
打扰我[至少现在]。
再次感谢大家的努力提供帮助。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)