8

I am sending a JSONObject to my Webserver from an Android client using the code from this example. Reproducing code here

import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.params.BasicHttpParams;
import org.apache.http.params.HttpConnectionParams;
import org.apache.http.params.HttpParams;

int TIMEOUT_MILLISEC = 10000;  // = 10 seconds
HttpParams httpParams = new BasicHttpParams();
HttpConnectionParams.setConnectionTimeout(httpParams, TIMEOUT_MILLISEC);
HttpConnectionParams.setSoTimeout(httpParams, TIMEOUT_MILLISEC);
HttpClient client = new DefaultHttpClient(httpParams);

HttpPost request = new HttpPost(serverUrl);
request.setEntity(new ByteArrayEntity(
    postMessage.toString().getBytes("UTF8")));
HttpResponse response = client.execute(request);

My Question

How to do best compression on JSONObject before sending it to server and how to decompress it on Server (I am using Java Servlets)?

Community
  • 1
  • 1
Gaurav Agarwal
  • 18,754
  • 29
  • 105
  • 166
  • Is the amount of data so large that you really need to use compression? – Squonk Jul 09 '12 at 20:53
  • @Squonk I may need to do some analysis to quantify the size of data I am sending across the server but the round trip from client to server and back is taking about 2 minutes 49 seconds. – Gaurav Agarwal Jul 09 '12 at 21:38
  • Have you looked at http://developer.android.com/reference/java/util/zip/GZIPOutputStream.html? You'd probably need to use `HttpURLConnection` rather than `DefaultHttpClient` however. – Squonk Jul 09 '12 at 22:05

1 Answers1

14

According to this http://android-developers.blogspot.com/2011/09/androids-http-clients.html If you are using Gingerbread or later HttpURLConnection automatically adds gzip compression:

In Gingerbread, we added transparent response compression. HttpURLConnection will automatically add this header to outgoing requests, and handle the corresponding response:

Accept-Encoding: gzip

Your webserver would then need to handle gzip compression.

Edit:
Serve Gzipped content with Java Servlets

Edit 2:
Gzip compression using DefaultHttpClient Enabling GZip compression with HttpClient

private static final String HEADER_ACCEPT_ENCODING = "Accept-Encoding";
private static final String ENCODING_GZIP = "gzip";

final DefaultHttpClient client = new DefaultHttpClient(manager, parameters);

client.addRequestInterceptor(new HttpRequestInterceptor() {
  public void process(HttpRequest request, HttpContext context) {
    // Add header to accept gzip content
    if (!request.containsHeader(HEADER_ACCEPT_ENCODING)) {
      request.addHeader(HEADER_ACCEPT_ENCODING, ENCODING_GZIP);
    }
  }
});

client.addResponseInterceptor(new HttpResponseInterceptor() {
  public void process(HttpResponse response, HttpContext context) {
    // Inflate any responses compressed with gzip
    final HttpEntity entity = response.getEntity();
    final Header encoding = entity.getContentEncoding();
    if (encoding != null) {
      for (HeaderElement element : encoding.getElements()) {
        if (element.getName().equalsIgnoreCase(ENCODING_GZIP)) {
          response.setEntity(new InflatingEntity(response.getEntity()));
          break;
        }
      }
    }
  }
});

Edit 3:
Here is another Stackoverflow question regarding the gzipping of post contents GZip POST request with HTTPClient in Java. You will need to manually gzip the data before posting it, since the normal http/gzip operation is the server sending gzipped content to the client.

Community
  • 1
  • 1
CraigTeegarden
  • 8,173
  • 8
  • 38
  • 43
  • Thanks for your response. I am using Android 2.2 and DefaultHTTPClient – Gaurav Agarwal Jul 09 '12 at 21:19
  • Thanks for Edit2. This looks like code for accepting Gzip response from the server. I am looking at compression on the Android Client before sending the data to server. – Gaurav Agarwal Jul 09 '12 at 21:36
  • I have asked a related question http://stackoverflow.com/questions/11414987/what-header-should-be-used-for-sending-gzip-compressed-json-from-android-client – Gaurav Agarwal Jul 10 '12 at 14:00
  • The "gzip" header isnt transmitted to the server; Im using wireshark and I see no trace of "gzip"... – Ted Feb 16 '14 at 14:23
  • The question is asking about compression from Android client to Server, but this answer is about adding Accept-Encoding to enable compression from Server to client. This doesn't answer the question -- or am I missing something? – g . Oct 15 '15 at 14:30
  • I found this file that contained an action example how to compress the request body https://svn.apache.org/repos/asf/activemq/trunk/activemq-http/src/main/java/org/apache/activemq/transport/http/HttpClientTransport.java – Svetoslav Marinov Mar 06 '16 at 06:42