rest data getting cached android

I am using the code below to fetch data from server. When the data is updated on server, I get the old data from this method. When I use the same method in the web browser i get updated data.

Even when I stop the app and start again it reflects old data but when I have cleaned all my tasks using task manager, I get new data.

Is the data being cached on the device as i am making new request each time

String response = null;
InputStream inputStream = null;
HttpURLConnection urlConnection = null;
try {

    URL url = new URL(urlString);
    urlConnection = (HttpURLConnection) url.openConnection();
    urlConnection.setReadTimeout(10000);
    urlConnection.setConnectTimeout(15000);

    if (method == POST) {
        urlConnection.setDoInput(true);
        urlConnection.setDoOutput(true);
        urlConnection.setRequestProperty("Accept", "application/json");
        urlConnection.setRequestMethod("POST");
        OutputStream os = urlConnection.getOutputStream();
        BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(os, "UTF-8"));
        writer.write(params);
        writer.flush();
        writer.close();
        os.close();
    } else {
        urlConnection.setRequestMethod("GET");
    }

    int statusCode = urlConnection.getResponseCode();

    /* 200 represents HTTP OK */
    if (statusCode == HttpURLConnection.HTTP_OK) {
        inputStream = new BufferedInputStream(urlConnection.getInputStream());
        response = convertInputStreamToString(inputStream);
        return response;
    }

I searched the web and found that use cache is on by default, so these two line might help

    urlConnection.setUseCaches(false);
    urlConnection.addRequestProperty("Cache-Control", "no-cache");

Answers


Append some random parameter i.e. current timestamp to the URL then it will treat as fresh request.

Change This

URL url = new URL(urlString);

To

URL url = new URL(urlString+new Date().getTime());

Need Your Help

Specifying the ID during UPDATE on Derby TRIGGER

sql swing derby

I just started learning and reading about TRIGGERs in Derby which I use as Database for a project in Java. I used to have MSSQL as database in the past.

Unicorn says "Error opening file for reading: Permission denied"

ruby-on-rails ruby permissions unicorn

I'm running a rails 3 application running on unicorn, every now and then I get this message: