0

Receive compressed json with Android app

Posted July 18th, 2011. Filed under Applications

There are times that you want to query a server from your android application and bring back JSON to process, at times this JSON can be large and you want to make the call as quick as possible. The easiest way to do this is to allow the server to compress your JSON response before sending it to you which will extremely reduce the size of the data being sent to the phone. Here is a quick sample of calling a webservice that returns compressed JSON and processing the result

 

public void getUpdates() {
 
    HttpClient httpclient = new DefaultHttpClient();
    HttpGet httpget = new HttpGet("http://yourservice.com/api/stores.json");
 
    // you have to tell the server that you can handle zipped response
    httpget.addHeader("Accept-Encoding", "gzip");
 
    HttpResponse response;
 
    try {
        response = httpclient.execute(httpget);
        HttpEntity entity = response.getEntity();
        if (entity != null) {
            InputStream instream = entity.getContent();
            String result;
            // check if we got a compressed resonse back
            if (entity.getContentEncoding() != null && "gzip".equalsIgnoreCase(entity.getContentEncoding().getValue())){
                result =  uncompressInputStream(instream);
            } else {
                result = convertStreamToString(instream);
            }
 
            ObjectMapper mapper = new ObjectMapper(); 
            mapper.getSerializationConfig().setDateFormat(sdf);
            JsonStoreResult jsonResult = mapper.readValue(result, JsonStoreResult.class);
 
            for (JsonStore e : jsonResult.getStores()) {
                // store this object or do something with it
 
            }
        }
    } catch (ClientProtocolException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}
 
private String uncompressInputStream(InputStream inputStream) throws IOException {
    StringBuilder value = new StringBuilder();
 
    GZIPInputStream gzipIn = null;
    InputStreamReader inputReader = null;
    BufferedReader reader = null;
 
    try {
        gzipIn = new GZIPInputStream(inputStream);
        inputReader = new InputStreamReader(gzipIn, "UTF-8");
        reader = new BufferedReader(inputReader);
 
        String line = "";
        while ((line = reader.readLine()) != null) {
            value.append(line + "\n");
        }
    } finally {
        try {
            if (gzipIn != null) {
                gzipIn.close();
            }
 
            if (inputReader != null) {
                inputReader.close();
            }
 
            if (reader != null) {
                reader.close();
            }
 
        } catch (IOException io) {
 
            io.printStackTrace();
        }
 
    }
 
    return value.toString();
}
 
private String convertStreamToString(InputStream is) {
   BufferedReader reader = new BufferedReader(new InputStreamReader(is));
   StringBuilder sb = new StringBuilder();
 
   String line = null;
   try {
       while ((line = reader.readLine()) != null) {
           sb.append(line + "\n");
       }
   } catch (IOException e) {
       e.printStackTrace();
   } finally {
       try {
           is.close();
       } catch (IOException e) {
           e.printStackTrace();
       }
   }
   return sb.toString();
}

Post to Twitter Tweet This Post

If you have enjoyed this entry. Please feel free to bookmark it using your favorite social bookmarking site

Leave a Comment