SamJ
SamJ

Reputation: 423

BufferedReader reading online text file without caching text in file?

I know this sounds weird but i'll explain

I've successfully made a BufferedReader that will read text from an online text file once the application is opened but the problem is that the first time i open my app (In Emulator) it Logs the text Hello Good World.After closing the app(Not Pausing) and changing the text in the server to let's say Hello Good World 2.I open the app (In Emulator) and it Logs Hello Good World.I try Restarting my app a couple of times but it still logs the same thing.

Proof that the online text is being cached

When i opened the URL from Google Chrome i saw the text Hello Good World i refreshed the page and it showed Hello Good World 2.Now when i launched my app (From Emulator )it displayed Hello Good World 2.

My code:

public class checkRemoteFile extends AsyncTask<Void, Integer, String>{



@Override
protected String doInBackground(Void... params) {
    Log.i("Async","Started");

    String a = "http://www.standarized.com/ads/Test.txt" ;

    StringBuffer result = new StringBuffer();
    try{
        URL url = new URL(a);

        InputStreamReader isr  = new InputStreamReader(url.openStream());

        BufferedReader in = new BufferedReader(isr);

        String inputLine;

        while ((inputLine = in.readLine()) != null){
            result.append(inputLine);

        }
        txtFile = result.toString();
        Log.i("Buffer", txtFile);
        in.close();
        isr.close();
    }catch(Exception ex){
       // result = new StringBuffer("TIMEOUT");
        Log.i("Buffer", ex.toString());

    }
    Log.i("GetStringBuffer(result)", result.toString());


    return null;
}   


           }   

Upvotes: 1

Views: 998

Answers (1)

Dyna
Dyna

Reputation: 2305

If you use URLConnection you can use useCaches.
Take a look at that link. sample:

  URL url = new URL("http://www.standarized.com/ads/Test.txt");
       URLConnection urlConnection = url.openConnection();
       urlConnection.setUseCaches(false);
         try {
           // Read all the text returned by the server
   BufferedReader in = new BufferedReader(new InputStreamReader(
            urlConnection.getInputStream()));
    String str;
    while ((str = in.readLine()) != null) {
        // str is one line of text; readLine() strips the newline
        // character(s)
        inputLine = inputLine + str;
    }

        finally {
         in.close();
       }

Upvotes: 1

Related Questions