1

我知道这听起来很奇怪,但我会解释

我已经成功制作了一个BufferedReader,它会在打开应用程序后从在线文本文件中读取文本,但问题是我第一次打开我的应用程序(在模拟器中)它会记录文本Hello Good World。关闭应用程序后(不暂停)并将服务器中的文本更改为假设Hello Good World 2。我打开应用程序(在模拟器中)并记录Hello Good World。我尝试重新启动我的应用程序几次,但它仍然记录相同的内容。

在线文本被缓存的证明

当我从谷歌浏览器打开 URL 时,我看到Hello Good World刷新页面的文本并显示Hello Good World 2。现在,当我启动我的应用程序(从模拟器)时,它显示了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;
}   


           }   
4

1 回答 1

1

如果您使用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();
       }
于 2013-08-29T11:21:00.437 回答