HTTP调用返回陈旧数据

时间:2015-10-27 04:05:56

标签: java android http

我正在构建一个应用程序,该应用程序从网站以设定的间隔检索数据,并且我正在检索陈旧数据。具体来说,我从NFL比赛中拉分数,但每当分数改变时,我的应用程序在返回旧分数和新分数之间交替。最终,它会稳定并且只返回新的分数。

如何阻止我的应用程序返回陈旧数据?

public class MainActivity extends Activity {

private String webSourceCode;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

   new MyAsyncTask().execute(); 

}

private class MyAsyncTask extends AsyncTask<Void, Void, List<Integer>> {
    protected List<Integer> doInBackground(Void... params) {
        StringBuilder sb = new StringBuilder();
        try {
            URL nfl = new URL("http://www.cbssports.com/nfl/scoreboard");
            BufferedReader in = new BufferedReader(
                    new InputStreamReader(
                            nfl.openStream()));
            String inputLine;

            while ((inputLine = in.readLine()) != null) {
                sb.append(inputLine);
            }
            in.close();
        } 
        catch (MalformedURLException e) {
            e.printStackTrace();
        } 
        catch (IOException e) {
            e.printStackTrace();
        }
        webSourceCode = sb.toString();

        return parseScores(webSourceCode);
    }
    protected void onPostExecute(List<Integer> listScores) {

        updateViews(listScores);
        //recursion
        new MyAsyncTask().execute();
    }
}

}

1 个答案:

答案 0 :(得分:0)

GET调用可以由服务器或中介缓存。您可以传递动态查询参数,以确保中间/服务器不会发回缓存的响应。这样的事情会起作用:

URL nfl = new URL("http://www.cbssports.com/nfl/scoreboard?nocache="+new Date().getTime());