Reputation: 4681
I have recently switched my app from using AndroidHTTPClient to using HttpURLConnection. I am trying to get the caching capability of HttpURLConnection to work, and am having no success.
Here is my code for doing the Http request
URL url = new URL(currentURL);
ResultAndUrl result = null;
final HttpURLConnection httpUrlConnection = (HttpURLConnection) urll.openConnection();
httpUrlConnection.setUseCaches(true);
try {
InputStream in = new BufferedInputStream(httpUrlConnection.getInputStream());
result = new ResultAndUrl();
result.result = convertStreamToString(in);
result.url = currentURL;
} catch (final Exception e) {
try {
Log.e("NETWORK", e.getMessage());
} catch (Exception ee) {
}
} finally {
try {
httpUrlConnection.disconnect();
} catch (Exception e) {
}
}
Also in my Application class I have
public void enableHttpResponseCache() {
final long httpCacheSize = 10 * 1024 * 1024; // 10 MiB
final File httpCacheDir = new File(getCacheDir(), "http");
try {
Class.forName("android.net.http.HttpResponseCache")
.getMethod("install", File.class, long.class)
.invoke(null, httpCacheDir, httpCacheSize);
} catch (Exception httpResponseCacheNotAvailable) {
try{
HttpResponseCache.install(httpCacheDir, httpCacheSize); // Library that implements HttpResponseCache for pre-ICS phones
} catch(Exception e){
}
}
}
However, even for ICS phones, there are no cache-hits ever happening, and it doesn't seem like caching is doing anything at all. Can you please tell me if I am doing something wrong, or missing something?
Upvotes: 1
Views: 1434
Reputation: 6289
The 'update' method in the above link is example code using http thread pool, https, and http posts. So, in addition to suggesting the links in the above comment, i've also used them in android.
some more code (the runnable from HttpConnectionPost class:
public void run() {
// Log.d(TAG, "run ENTRY instance" +entry.toString());
handler.sendMessage(Message.obtain(handler, HttpConnection.DID_START));
HttpParams params = new BasicHttpParams();
HttpConnectionParams.setConnectionTimeout(params, 40 * 1000);
HttpConnectionParams.setSoTimeout(params, 20 * 1000);
HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1);
httpClient = new DefaultHttpClient(MyConnectionManager.getInstance(), params);
// HttpConnectionParams.setSoTimeout(httpClient.getParams(), 25000);
httpClient.addRequestInterceptor(new HttpRequestInterceptor() {
public void process(
final HttpRequest request,
final HttpContext context) throws HttpException, IOException {
if (!request.containsHeader("Authorization")) {
request.addHeader("Authorization", "OAuth " +ClientCustomSSL.getInstance().getToken());
}
}
});
try {
HttpResponse response = null;
switch (method) {
case POST:
HttpPost httpPost = new HttpPost(url);
if (data != null){
System.out.println(" post data not null ");
httpPost.setEntity(new StringEntity(data));
}
if (entry != null){
ContentProducer cp = new ContentProducer() {
public void writeTo(OutputStream outstream) throws IOException {
ExtensionProfile ep = new ExtensionProfile();
ep.addDeclarations(entry);
XmlWriter xmlWriter = new XmlWriter(new OutputStreamWriter(outstream, "UTF-8"));
entry.generate(xmlWriter, ep);
xmlWriter.flush();
}
};
httpPost.setEntity(new EntityTemplate(cp));
}
httpPost.addHeader("GData-Version", "2");
httpPost.addHeader("X-HTTP-Method-Override", "PATCH");
httpPost.addHeader("If-Match", "*");
httpPost.addHeader("Content-Type", "application/xml");
response = httpClient.execute(httpPost);
break;
}
if (method < BITMAP)
processEntity(response.getEntity());
} catch (Exception e) {
handler.sendMessage(Message.obtain(handler,
HttpConnection.DID_ERROR, e));
}
MyConnectionManager.getInstance().didComplete(this);
}
Upvotes: 1