我知道这听起来很奇怪,但我会解释
我已经成功制作了一个 BufferedReader ,一旦应用程序打开就会从在线文本文件中读取文本,但问题是我第一次打开我的应用程序(在模拟器中) 它记录文本Hello Good World
。关闭应用程序(不暂停)并更改服务器中的文本后,让我们说Hello Good World 2
。我打开应用程序(在模拟器中)< / strong>并且它记录Hello Good World
。我尝试重启我的应用程序几次,但它仍记录相同的事情。
证明正在缓存在线文字
当我从谷歌浏览器打开网址时,我看到文字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;
}
}
答案 0 :(得分: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();
}