0

I'm building an android application where I have to read a big chunk of information from a server.

The server sends the information as a JSONArray and everything is fine on the server side.

However the problem comes when I'm trying to receive this JSONArray as it seems to be too large for me to receive?

I connect to the server using HttpClient like this:

HttpParams httpParams = new BasicHttpParams();
int timeoutConnection = 20000;
HttpConnectionParams.setConnectionTimeout(httpParams, timeoutConnection);
int timeoutSocket = 20000;
HttpConnectionParams.setSoTimeout(httpParams, timeoutSocket);

HttpClient client = new DefaultHttpClient(httpParams);
HttpGet request = new HttpGet();
request.setURI(url);

HttpResponse response = null;
try {
    response = client.execute(request);
} catch (ClientProtocolException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
} catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
}

BufferedReader in = null;
if (response != null) {
    in = new BufferedReader(new InputStreamReader(response.getEntity().getContent()), 100000);
}

return in;

And I simply try to read what's in the BufferedReader with the readLine() method, however what I get back is not the full content of the response. I know this because I tried accessing the same content with a web browser and it returns the full content.

The response from the server is on a single line (might affect it, I don't know)

I'm able to read 4kb and I need to read a whopping 141kb (so I'm far off)..

My personal guess is that somewhere in my code there is a size limit.

Extra I read from the BufferedReader like this:

BufferedReader in = getResponse(new URI(params[0]));
// Read from the received buffer
String s;
StringBuilder sb = new StringBuilder(10000);

if(in != null){
    while ((s = in.readLine()) != null){
        sb.append(s);
    }
}

Log.d("Problem: ", sb.toString());

EDIT: The response does not differ, it's always the same length

EDIT2: It seems that it was Log.d() that had a limit in print length! There was no problem to begin with... sorry!

David Ekermann
  • 87
  • 3
  • 13

2 Answers2

1

Use Gzipped JSON in HTTP Request

Checkout this article: http://arnab.ch/blog/2012/09/android-how-to-send-gzipped-json-in-http-request/

AAV
  • 3,785
  • 8
  • 32
  • 59
  • It may reduce the size and improve performance as stated in the article, but I can't see how it would resolve my problem. – David Ekermann Jun 12 '14 at 21:54
  • Sorry for taking your time it seems that it was Log.d() that had a limit in print length. Thanks for the information though I will look in to Gzip later on to improve the performance. – David Ekermann Jun 13 '14 at 20:14
1

Instead of reading the InputStream just directly convert it to String using EntityUtils.toString

example:

 try {
    response = client.execute(request);
    HttpEntity resEntityGet = responseGet.getEntity(); 

    final String result = EntityUtils.toString(resEntityGet); //result is your json response

} catch (ClientProtocolException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
} catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
}
Rod_Algonquin
  • 26,074
  • 6
  • 52
  • 63