本文介绍了字符串被截断时,其太长的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从我们的服务器的JSON响应和响应字符串似乎是当字符串的长度达到约5525字总是被截断。

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.

HttpClient httpClient = new DefaultHttpClient();
HttpPost post = new HttpPost(URL);
ResponseHandler<String> responseHandler= new BasicResponseHandler();
String testResponse = httpClient.execute(post, responseHandler);

我也用HttpEntity和读取响应流尝试这样做。但是,这也截断字符串约为该长度。

I also tried this by using HttpEntity and reading the response stream. But that also truncates the string at approximately that length.

            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/

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/

感谢你。

推荐答案

首先,感谢大家的回答。

First of all thanks everyone for replying.

我刚刚发现这个问题是不是与我的code,但与线的的logcat正在显示的数目。问题是,我在我的JSON回复格式有一个问题,我的分析code吐出一个错误。于是,我开始尝试用它打印在logcat的响应JSON字符串进行调试。这是被截断总是和我猜测到现在,从服务器本身的响应被截断。

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.

所以,今天我开始与响应字符串生成器玩,有写搞笑的片段数字符和检测字符的位置我期待,我发现,响应被完全恢复。我还测试了我的code对一些大型蜇伤,我发现的logcat对字符串的长度是有限的,它显示或者至少看上去如此。这就是为什么我的反应,正在显示为截断的字符串,我认为它的问题与我的code。

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.

因此​​,它是工作的罚款为code是较早的,唯一的问题是该logcat中不显示完整的字符串。但它不打扰我[ATLEAST现在。

So it is working fine as the code is earlier and the only problem wasthat the logcat does not display the complete string. But it does notbother me[atleast right now].

再次感谢大家的努力帮助。

Thanks again everyone for trying to help.

这篇关于字符串被截断时,其太长的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-19 10:49