0

This is the first time I am using HttpURLConnection in Android.

URL url = null;
HttpURLConnection con = null;
BufferedReader in = null;
try {
    url = new URL(companyInfo.getCompanyCurrent());
    Log.i(Constant.TAG, "URL:" + url.toString());
    con = (HttpURLConnection) url.openConnection();
    con.setRequestMethod("GET");
    con.setRequestProperty("connection", "close");
    con.setUseCaches(false);
    con.setDefaultUseCaches(false);
    con.setRequestProperty("Cache-Control", "no-cache");
    con.setRequestProperty("Cache-Control", "no-store");
    con.setRequestProperty( "User-agent", "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.4 (KHTML, like Gecko) Chrome/22.0.1229.94 Safari/537.4" );
    int responseCode = con.getResponseCode();
    ....

So as you see, I have made setUseCaches and setDefaultUseCaches as false and from some of the suggestions on the internet, tried setting "Cache control" as well. The problem is that its still caching results. It refreshes periodically.

  • Is there a way delete the cached results?
  • Is there a way set cached refresh time?

On a side note I found that if user agent is not set explicitly, for some android versions the desktop URL is hit and for others mobile URL is hit. Any particular reason for that?

Let'sRefactor
  • 3,303
  • 4
  • 27
  • 43
D-D
  • 954
  • 2
  • 12
  • 27
  • i didn't get how you assert that the response is being cached – nandsito Nov 01 '16 at 16:14
  • Say i hit an URL at 4PM it gives me some values. When I hit back at 4.05Pm it gives me the same results, where as in the server the value has been updated. Again when I hit it after some time, I get the updated value. – D-D Nov 01 '16 at 16:32
  • the origin server may be caching the response. Setting `Cache-Control: no-cache` in the request doesn't affect how the origin server caches responses: http://stackoverflow.com/a/14544664/2657100 – nandsito Nov 01 '16 at 16:43

0 Answers0