ach*_*hie 8 java android httpclient
我试图从我们的服务器获取JSON响应,并且当字符串长度达到大约5525个字符时,响应字符串似乎总是被截断.
HttpClient httpClient = new DefaultHttpClient();
HttpPost post = new HttpPost(URL);
ResponseHandler<String> responseHandler= new BasicResponseHandler();
String testResponse = httpClient.execute(post, responseHandler);
Run Code Online (Sandbox Code Playgroud)
我还尝试使用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();
}
}
}
Run Code Online (Sandbox Code Playgroud)
请告诉我如何处理这个问题.我在创建它时使用这篇文章作为参考. http://senior.ceng.metu.edu.tr/2009/praeda/2009/01/11/a-simple-restful-client-at-android/
谢谢.
ach*_*hie 14
首先感谢大家的回复.
我刚刚发现问题不在于我的代码,而在于logcat显示的行数.问题是我的json回复格式出现问题,我的解析代码出错了.所以我开始尝试通过在logcat中打印响应json字符串来调试它.这总是被截断,我猜到现在服务器本身的响应被截断了.
所以今天我开始玩响应字符串构建器,并且必须编写有趣的片段来计算字符并检测我期待的位置上的字符,并且我发现响应完全被返回.我还在其他一些大型测试中测试了我的代码,我发现logcat对它显示的字符串的长度有限制,或者至少看起来如此.这就是为什么我的回复显示为截断的字符串,我认为这是我的代码的问题.
所以它的工作正常,因为代码更早,唯一的问题是logcat没有显示完整的字符串.但它并没有打扰我[至少现在].
再次感谢大家的帮助.
| 归档时间: |
|
| 查看次数: |
12752 次 |
| 最近记录: |