Sruit A.Suk
Sruit A.Suk

Reputation: 7263

StringBuilder lost data when convert to String

here what's the problem

I have problem when I tried to get String from my StringBuilder

BufferedReader reader = new BufferedReader(new InputStreamReader(response.getEntity().getContent()), 128 * 1024);
StringBuilder dataResponseSB = new StringBuilder();
String line ;
while ((line = reader.readLine()) != null) {
    dataResponseSB.append(line);
    if (DataFactory.DEBUG_MODE) {
        // all data here are complete
        Log.i("===LoadDataActivity","line: "+line);
    }
}
String rawdata = new String(dataResponseSB); // dataResponseSB.toString(); also not work
if (DataFactory.DEBUG_MODE) {
    // data here are lost
    Log.i("===LoadDataActivity","rawdata: "+rawdata);
}

(-) I receive a huge data from BufferedReader .readLine()

(-) I use Log to check and sure that I got about 5 line of 8000 Buffer Size per line and I am very sure that I have receive all data properly

(1) I append each line to StringBuilder Here

(-) after I append all the line to StringBuilder

(2) I try to convert it back to String

(-) Now, the problem, the when I check to new String here, the data have only 8192 (it should contain at least 30,000 or more)

What is the problem ? I am not sure it lost when it append to StringBuilder(1) or it lost when it convert back to String (2)


I add the code that I have tried below here ,, I have tried both UTF8 and without UTF8

            HttpParams params = new BasicHttpParams();
            HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1);
            //params.setParameter(CoreProtocolPNames.PROTOCOL_VERSION, );
            params.setParameter(CoreConnectionPNames.SOCKET_BUFFER_SIZE, 128 * 1024);
            HttpClient client = new DefaultHttpClient(params);


        //  HttpClient client = new DefaultHttpClient(new BasicHttpParams());
            HttpPost httppost = new HttpPost(DataFactory.REQUEST_API_URL + "?id=" + DataFactory.USER_ID );
            // Depends on your web service

            HttpConnectionParams.setConnectionTimeout(client.getParams(), 10000); //Timeout Limit
            HttpConnectionParams.setSocketBufferSize(client.getParams(), 128 * 1024);
            HttpResponse response = client.execute(httppost);  
            //response.setParams(client.getParams().setParameter(CoreConnectionPNames.SOCKET_BUFFER_SIZE, 128 * 1024));

            //String rawdata = IOUtils.toString(response.getEntity().getContent(), "UTF-8");
           // String rawdata = EntityUtils.toString(response.getEntity());
            String rawdata = getResponseBody(response.getEntity());

            //Scanner s = new Scanner(response.getEntity().getContent()).useDelimiter("\\A");
            //String rawdata = s.hasNext() ? s.next() : "";


            /*

            //BufferedReader reader = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
            // ===================

            BufferedReader reader = new BufferedReader(new InputStreamReader(response.getEntity().getContent()), 128 * 1024);
            StringBuilder dataResponseSB = new StringBuilder();
            String line ;
            while ((line = reader.readLine()) != null) {
                dataResponseSB.append(line);
                if (DataFactory.DEBUG_MODE) {
                    Log.i("===LoadDataActivity","line: "+line);
                }
            }
            dataResponseSB.trimToSize();
            String rawdata = new String(dataResponseSB);

            /*
            InputStreamReader reader = new InputStreamReader(response.getEntity().getContent());
            StringBuffer sb = new StringBuffer();
            int c;
            while ((c = reader.read()) != -1) {
                sb.append((char)c);
                if (DataFactory.DEBUG_MODE) {
                    //Log.i("===LoadDataActivity","line: "+line);
                }
            }
            */

Upvotes: 4

Views: 2155

Answers (3)

Jon Skeet
Jon Skeet

Reputation: 1499890

I'm pretty sure this is the problem:

Log.i("===LoadDataActivity","rawdata: "+rawdata);

You're assuming that a log entry can include all of your data - I believe each log entry is limited to 8192 characters.

I suggest you log rawdata.length() and you'll see that it's actually got all of the data - it's just logging it that's failing.

Upvotes: 4

Hareshkumar Chhelana
Hareshkumar Chhelana

Reputation: 24848

// Try this way,hope this will help you to solve your problem...

StringBuilder buffer = new StringBuilder();

BufferedReader reader = new BufferedReader(new InputStreamReader(response.getEntity().getContent(), HTTP.UTF_8));

String line = null;
try {
    while ((line = reader.readLine()) != null) {
            buffer.append(line);
    }

} finally {
    instream.close();
    reader.close();
}
System.out.println("Buffer : " + buffer.toString());

Upvotes: 1

Akash Moradiya
Akash Moradiya

Reputation: 3322

try this,,

public String getResponseBody(final HttpEntity entity) throws IOException, ParseException {

        if (entity == null) {
            throw new IllegalArgumentException("HTTP entity may not be null");
        }

        InputStream instream = entity.getContent();

        if (instream == null) {
            return "";
        }

        if (entity.getContentLength() > Integer.MAX_VALUE) {
            throw new IllegalArgumentException(

                    "HTTP entity too large to be buffered in memory");
        }

        StringBuilder buffer = new StringBuilder();

        BufferedReader reader = new BufferedReader(new InputStreamReader(instream, HTTP.UTF_8));

        String line = null;
        try {
            while ((line = reader.readLine()) != null) {
                buffer.append(line);
            }

        } finally {
            instream.close();
            reader.close();
        }
        System.out.println("GEN END : " + Calendar.getInstance().getTimeInMillis());
        return buffer.toString();

    }

Upvotes: 1

Related Questions