HttpURLConnection cache storage

I am currently using this code to retrieve data from a server

public static String getResponse(String URL) throws IOException{ try{ String response_string; StringBuilder response = new StringBuilder(); URL url = new URL(URL); HttpURLConnection httpconn = (HttpURLConnection) url.openConnection(); if (httpconn.getResponseCode() == HttpURLConnection.HTTP_OK){ BufferedReader input = new BufferedReader(new InputStreamReader(httpconn.getInputStream())); String strLine = null; while ((strLine = input.readLine()) != null){ response.append(strLine); } input.close(); response_string = response.toString(); } httpconn.disconnect(); return response_string; } catch(Exception e){ throw new IOException(); } } 

But it looks like it stores the cache, maybe I'm not sure, but if I change the data on the server and open the activity again, it will still remain the same on the application. I used HttpClient , which worked well before, but since it is deprecated since API 22 I changed it to HttpURLConnection . So can this be fixed?

+4
source share
1 answer

You can find out if the cache option is enabled by default using:

 getDefaultUseCaches(); //or getUseCaches(); 

As you can see here in the documentation.

If you find your problem there, you can simply change it using

 setDefaultUseCaches(boolean newValue) //or setUseCaches(boolean newValue) // Uses a flag (see documentation) 

As seen here.

+7
source

All Articles