簡體   English   中英

BufferedReader讀取在線文本文件而不在文件中緩存文本?

[英]BufferedReader reading online text file without caching text in file?

我知道這聽起來很奇怪,但我會解釋

我已經成功制作了一個BufferedReader ,它將在打開應用程序后從在線文本文件中讀取文本,但是問題是我第一次打開我的應用程序(在模擬器中)會記錄文本Hello Good World 。關閉應用程序后(不暫停)並更改服務器中的文本,例如Hello Good World 2 World2。我打開該應用程序(在模擬器中) ,它記錄了Hello Good World 。我嘗試重新啟動應用程序幾次,但仍然記錄了同樣的事情。

證明在線文本已被緩存

當我從Google Chrome瀏覽器打開URL時,我看到了文本Hello Good World刷新了頁面並顯示Hello Good World 2 World2。現在,當我啟動我的應用程序(來自Emulator)時,它顯示了Hello Good World 2

我的代碼:

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;
}   


           }   

如果使用URLConnection ,則可以使用useCaches
看一下該鏈接。 樣品:

  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();
       }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM