Home » Android » java – String is being truncated when its too long

java – String is being truncated when its too long

Posted by: admin June 15, 2020 Leave a comment

Questions:

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);

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();
                    }
                }
            }

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/

Thank you.

How to&Answers:

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.

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.

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].

Thanks again everyone for trying to help.

Answer:

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.

Answer:

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.

Also try doing the request using curl and/or wget from the command line. These give you a lot more information, plus the ability to set request headers.

Answer:

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.

use the following to avoid that..

if (responseCode == 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));

        }

Answer:

Although its quite late but may help someone else
i used this code to display complete LONG string. Found somewhere on same site

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 🙂