Enve Enve - 3 months ago 11x
Java Question

HttpURLConnection keeping cache

I am currently using this code to get data from server

public static String getResponse(String URL) throws IOException{

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_string = response.toString();


return response_string;
catch(Exception e){
throw new IOException();


But it looks like it is keeping cache, maybe not I am not sure, but if I change the data on the server and reopen the activity it's still remains the same on application. I have been using
before which was working good, but because it is deprecated since
API 22
I changed it to
. So is there any way to fix this?


You can see if the cache option is activated by default using:

getDefaultUseCaches(); //or

As seen here in the documentation.

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

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

As seen here on the same page.

If yet you can't solve your issue,