I have read many questions on SO about like this, this, this and this.
My problem is the following: I have json data that reside on a server. Tha data are cached and are requested as gzip, so that the whole size is about 110kb. If you try to retrieve this data with firefox (and count the time with firebug) it takes about 3 seconds to download and display.
The very same data take about 10 - 15 seconds (or even more sometimes) to get downloaded and converted to string on my android app. (Note that I am using a Nexus 10 tablet which is one of the fastest in temrs of processing of its kind).
See my code below:
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
// ... nameValuePairs omitted
HttpParams params = new BasicHttpParams();
HttpConnectionParams.setConnectionTimeout(params, 3500);
HttpConnectionParams.setSoTimeout(params, 14000);
HttpConnectionParams.setTcpNoDelay(params, false);
params.setParameter(CoreProtocolPNames.PROTOCOL_VERSION, HttpVersion.HTTP_1_1);
params.setBooleanParameter(CoreProtocolPNames.USE_EXPECT_CONTINUE, true);
HttpClient httpclient = new DefaultHttpClient(params);
HttpPost httppost = new HttpPost(mContext.getResources().getString(R.string.newsUrl));
httppost.addHeader("Accept-Encoding", "gzip");
try {
// Add your data
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs, "UTF-8"));
Log.i(TAG, "NewsFetcher, Request started at: " + (int)(System.currentTimeMillis() / 1000));
// Execute HTTP Post Request
HttpResponse response = httpclient.execute(httppost);
Log.i(TAG, "NewsFetcher, Request ended at: " + (int)(System.currentTimeMillis() / 1000));
if (response.getStatusLine().getStatusCode() == 200 || response.getStatusLine().getStatusCode() == 204 || response.getStatusLine().getStatusCode() == 304 ) {
Header contentEncoding = response.getFirstHeader("Content-Encoding");
String webAppResponse;
if (contentEncoding != null && contentEncoding.getValue().equalsIgnoreCase("gzip")) {
Log.i(TAG, "NewsFetcher, InputStream(gzip) received at : " + (int)(System.currentTimeMillis() / 1000));
webAppResponse = IOUtils.toString(new GZIPInputStream(response.getEntity().getContent()), "UTF-8");
} else {
Log.i(TAG, "NewsFetcher, InputStream(plain) received at : " + (int)(System.currentTimeMillis() / 1000));
webAppResponse = IOUtils.toString(response.getEntity().getContent(), "UTF-8");
}
Log.i(TAG, "NewsFetcher, InputStream ended at : " + (int)(System.currentTimeMillis() / 1000));
// try to detect any error
try {
JSONArray webAppResultJSON = new JSONArray(webAppResponse);
ArrayList<NewsItem> parsedItems = parseJSON(webAppResultJSON);
return new FetchResult(UpdateStatus.FETCH_OK, parsedItems);
// ... exceptions omitted
} catch (Exception e) {
Log.e(TAG, "General Exception 1");
e.printStackTrace();
return new FetchResult(UpdateStatus.FETCH_GENERAL_ERROR, null);
}
} else {
return new FetchResult(UpdateStatus.FETCH_HTTP_ERROR, null);
}
// ... exceptions omitted
} catch (Exception e) {
Log.e(TAG, "General Exception 2");
e.printStackTrace();
return new FetchResult(UpdateStatus.FETCH_GENERAL_ERROR, null);
}
And here are the results from logcat
06-28 10:43:11.486: I/com.package.my.logic.NewsFetcher(3102): NewsFetcher, Request started at: 1372405391
06-28 10:43:17.986: I/com.package.my.logic.NewsFetcher(3102): NewsFetcher, Request ended at: 1372405397
06-28 10:43:17.986: I/com.package.my.logic.NewsFetcher(3102): NewsFetcher, InputStream(gzip) received at : 1372405397
06-28 10:43:30.266: I/com.package.my.logic.NewsFetcher(3102): NewsFetcher, InputStream ended at : 1372405410
If you subtract 1372405391 from 1372405410 it is 19 seconds! Can you suggest any optimization on the code above ?
I have also tried to use HttpURLConnection
instead of HttpPost
but no luck...