java 字符串太长时被截断
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3580062/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
String is being truncated when its too long
提问by achie
I am trying to get a JSON response from our server and the response string seems is always being truncated when the string length reaches to around 5525 characters.
我试图从我们的服务器获取 JSON 响应,当字符串长度达到大约 5525 个字符时,响应字符串似乎总是被截断。
HttpClient httpClient = new DefaultHttpClient();
HttpPost post = new HttpPost(URL);
ResponseHandler<String> responseHandler= new BasicResponseHandler();
String testResponse = httpClient.execute(post, responseHandler);
I also tried this by using HttpEntity and reading the response stream. But that also truncates the string at approximately that length.
我还通过使用 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();
}
}
}
Please let me know how I can handle this problem. I used this post as the reference while creating this. http://senior.ceng.metu.edu.tr/2009/praeda/2009/01/11/a-simple-restful-client-at-android/
请让我知道我如何处理这个问题。我在创建这篇文章时使用了这篇文章作为参考。 http://senior.ceng.metu.edu.tr/2009/praeda/2009/01/11/a-simple-restful-client-at-android/
Thank you.
谢谢你。
回答by achie
First of all thanks everyone for replying.
首先感谢大家的回复。
I just discovered that the problem is not with my code but with the number of lines that the logcat is displaying. The problem was that I got a problem in my json reply format and my parsing code spits an error. So I started trying to debug it by printing the response json string in logcat. This is being truncated always and I was guessing till now that the response from server itself is being truncated.
我刚刚发现问题不在于我的代码,而在于 logcat 显示的行数。问题是我的 json 回复格式有问题,我的解析代码出现错误。所以我开始尝试通过在 logcat 中打印响应 json 字符串来调试它。这总是被截断,直到现在我一直在猜测来自服务器本身的响应被截断了。
So today I started playing with the response string builder and has to write funny snippets to count characters and detect the characters at positions I was expecting and I found that the response was being returned completely. I also tested my code on some other large stings and I discovered that the logcat has a limit on the length of the string that it displays or at least it looked so. That was why my responses were being displayed as truncated strings and I thought that its the problem with my code.
所以今天我开始使用响应字符串构建器,并且必须编写有趣的片段来计算字符并检测我期望的位置的字符,我发现响应被完全返回。我还在其他一些大字符串上测试了我的代码,我发现 logcat 对其显示的字符串长度有限制,或者至少看起来是这样。这就是为什么我的回复被显示为截断的字符串,我认为这是我的代码的问题。
So it is working fine as the code is earlier and the only problem was that the logcat does not display the complete string. But it does not bother me[atleast right now].
所以它工作正常,因为代码更早,唯一的问题是 logcat 不显示完整的字符串。但这并不困扰我[至少现在]。
Thanks again everyone for trying to help.
再次感谢大家的帮助。
回答by CommonsWare
Use a BasicResponseHandler:
This example demonstrates how to process HTTP responses using a response handler. This is the recommended way of executing HTTP requests and processing HTTP responses. This approach enables the caller to concentrate on the process of digesting HTTP responses and to delegate the task of system resource deallocation to HttpClient. The use of an HTTP response guarantees that the underlying HTTP connection will be released back to the connection manager automatically in all cases.
此示例演示如何使用响应处理程序处理 HTTP 响应。这是执行 HTTP 请求和处理 HTTP 响应的推荐方式。这种方式使调用者能够专注于消化 HTTP 响应的过程,并将系统资源释放的任务委托给 HttpClient。使用 HTTP 响应保证在所有情况下底层 HTTP 连接都将自动释放回连接管理器。
回答by Stephen C
It is possible that the server can tell the difference between your browser and your application and is responding differently.
服务器可能会分辨出您的浏览器和应用程序之间的差异,并做出不同的响应。
Hack your Java code to print out the request and response headers, and compare them with the headers you get when using your browser.
破解您的 Java 代码以打印出请求和响应标头,并将它们与您在使用浏览器时获得的标头进行比较。
Also try doing the request using curland/or wgetfrom the command line. These give you a lot more information, plus the ability to set request headers.
还可以尝试使用curl和/或wget从命令行执行请求。这些为您提供了更多信息,以及设置请求标头的能力。
回答by Umesh
String line = reader.readLine();
String line = reader.readLine();
I believe the above line in your program is the problem. It reads a line from the server response and the String line. If the server response has a line feed (\n) then the reader will not be able to read the line after that.
我相信您程序中的上述行是问题所在。它从服务器响应和字符串行中读取一行。如果服务器响应有换行符 (\n),那么阅读器将无法在此之后读取该行。
use the following to avoid that..
使用以下方法来避免..
if (responseCode == HttpURLConnection.HTTP_OK) {
如果(响应代码 == HttpURLConnection.HTTP_OK){
InputStream in = ((URLConnection) httpConnection).getInputStream();
int len = 0;
byte[] data1 = new byte[1024];
while ( -1 != (len = in.read(data1)) )
dataFromServer.append(new String(data1, 0, len));
}
回答by Lakhwinder Singh Dhillon
Although its quite late but may help someone else i used this code to display complete LONG string. Found somewhere on same site
虽然它很晚但可能会帮助其他人我使用此代码显示完整的 LONG 字符串。在同一站点的某处找到
int maxLogSize = 1000;
for(int i = 0; i <= veryLongString.length() / maxLogSize; i++) {
int start = i * maxLogSize;<
int end = (i+1) * maxLogSize;
end = end > veryLongString.length() ? veryLongString.length() : end;
Log.v(TAG, veryLongString.substring(start, end));
}
Please Vote up if found helpful :)
如果觉得有帮助,请投票:)

