Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

HttpURLConnection keeping cache

I am currently using this code to get data from 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 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 HttpClient before which was working good, but because it is deprecated since API 22 I changed it to HttpURLConnection. So is there any way to fix this?

like image 985
Enve Avatar asked Aug 05 '15 22:08

Enve


1 Answers

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

getDefaultUseCaches(); //or
getUseCaches();

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.

like image 77
George Avatar answered Oct 09 '22 19:10

George