json - How to Compress a JSONObject send it over Http in Android? -


I JSONObject I am sending a Android client to my web server using 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; Integer TIMEOUT_MILLISEC = 10000; // = 10 seconds HTTPPRAMPPRMS = new basic HTTPPARM (); HttpConnectionParams.setConnectionTimeout (httpParams, TIMEOUT_MILLISEC); HTTP connection paras. Sittimeout (HPPARM, TIMEOUT_MILLISEC); HTTP Client Client = New Default HTTP Client (HPPARM); HTTP post request = new HTTP post (server URL); Request.setEntity (New ByteArrayEntity (postMessage.toString () getBytes ("UTF8")).); HttpResponse response = client.execute (request);   

Question

JSONObject best before compressing send to the server and how To compression it on the server (I am using Java Servlets )?

If you are using Gingerbread or later HttpURLConnection automatically if it says as gzip compression :

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

Accept-Encoding :. Do Gzip

Your server will need to gzip compression handle

Edit:

Edit 2: Using DefaultHttpClient

  Private static final string HEADER_ACCEPT_ENCODING = "Accept-Encoding" gzip compression 
; Private static final string ENCODING_GZIP = "gzip"; Last DefaultHttpClient Client = New DefaultHttpClient (Manager, Parameter); {Request.containsHeader (HEADER_ACCEPT_ENCODING)} {request.addHeader (HEADER_ACCEPT_ENCODING, ENCODING_GZIP) • Client.addRequestInterceptor (New HttpRequestInterceptor () {Public Zero Process (HttpRequest Request, HttpContext Reference) {// Add header to accept gzip content (request.containsHeader (HEADER_ACCEPT_ENCODING) ;}}}); client.addResponseInterceptor (New HttpResponseInterceptor () {public void process (HttpResponse response, HttpContext context) {// gzip last HttpEntity institution any responses fluff = response.getEntity (compressed with); final header encoding = entity.getContentEncoding (); If (for the encoding = null) {{HeaderElement element: encoding.getElements ()) {if (element.getName () equalsIgnoreCase (ENCODING_GZIP)) {response.setEntity (New InflatingEntity (response.getEntity ()); break ;}}}}}}); Edit 3: Here's another Stackoverflow question about gzipping of posted content here. You will be required to gzip the data manually before posting it since the normal http / gzip operation server is sending the Gzipped content to the client.

Comments