1

In my android application am reading text file which is in server. Am using below method.

private String getUrlContents(String UrlOfFile)
{
    StringBuilder content = new StringBuilder();

    try
    {
        URL url = new URL(UrlOfFile);
        URLConnection urlConnection = url.openConnection();
        BufferedReader bufferedReader = new BufferedReader(new 
        InputStreamReader(urlConnection.getInputStream()));

        String line;

        while ((line = bufferedReader.readLine()) != null)
        {
            content.append(line + "\n");
        }
        bufferedReader.close();
    }
    catch(Exception e)
    {
      Log.d(TAG,"Exception    >>>"+Log.getStackTraceString(e));
    }
    return content.toString();
}

But I am getting previous value of text file. When I once do clear data of app its able to get actual value.
Please help me to solve this issue.

  • At least here your not clearing content. Your only continuing to append to it. Make sure its being cleared. – CmosBattery Jun 06 '17 at 05:38
  • @Sudarshan H. S. What do you mean by previous value? – Robo Mop Jun 06 '17 at 05:43
  • @CoffeehouseCoder when i change text file content of server am getting older value in my code only. – Sudarshan H. S. Jun 06 '17 at 05:48
  • @CmosBattery , I tried clearing content by content.setLength(0) , but not helping . – Sudarshan H. S. Jun 06 '17 at 05:50
  • What happens when you load the contents of the URL in a browser? I suspect this might be a server caching issue, not a client-side issue. – Markus Fischer Jun 06 '17 at 06:01
  • If you have two urls/ two files then either your data holder is not changing, or the url is returning same data, or your not changing the url. Not all code is shown. So you should also check what UrlOfFile is each time the method runs at the URL line and if the line variable has different content if different url during debug. – CmosBattery Jun 06 '17 at 06:04
  • @CmosBattery there is no problem from server side. I saw value in browser It is giving me correct value. Even I'll get correct value in app once I do clear data of app. – Sudarshan H. S. Jun 06 '17 at 06:21

1 Answers1

0

My problem was due to returning of cached response. Issue solved by urlConnection.setUseCaches(false);

Thanks to all responds,

How to prevent Android from returning a cached response to my HTTP Request?

Adding header for HttpURLConnection