【问题标题】:Code is requesting the yahoo weather service again and again代码一次又一次地请求雅虎天气服务
【发布时间】:2013-03-10 06:35:26
【问题描述】:

我开发了一个安卓应用程序,它使用Yahoo! Weather API 获取天气信息,因为Yahoo! Weather API 允许每小时仅请求10 to 20 次服务。当我在它们之间来回切换时,我的应用程序有两个活动我的应用程序一次又一次地请求天气服务我的代码是:

@Override
protected void onResume() {
    // TODO Auto-generated method stub      
    String nameOfCity= pref.getString("cityname", cityNameInput.getText().toString());      
    getWeather(null, nameOfCity);// this the AsyncTask which is used to request Yahoo! Weather service.
    cityNameInput.setText(nameOfCity);
    super.onResume();
    Log.d("My_TAG", "in onResume()");
}

@Override
protected void onPause() {
    // TODO Auto-generated method stub      
    editor = pref.edit();
    if(cityNameInput.getText().toString().equals("")){
        cityNameInput.setText(yahooWeatherInfo.getLocationCity());
    }else{
        editor.putString("cityname", cityNameInput.getText().toString());
    }
    editor.commit();
    super.onPause();
    Log.d("MY_TAG", "in onPause()");
}

现在请告诉我如何限制它不要在活动之间一次又一次地请求Yahoo! Weather Service

【问题讨论】:

  • 考虑让您的数据请求成为 Android 服务。
  • @MorrisonChang 我该怎么做请给我一些提示?

标签: android android-activity activity-lifecycle yahoo-weather-api


【解决方案1】:

我看了你的代码。

您在onResume() 中呼叫getWeather(null, nameOfCity);。当您的活动再次恢复时,将调用此 onResume() 方法。

例如:A ---> B 然后从 B 活动按回,然后将调用 A 活动 onResume() 方法并向服务器发出请求。所以在onCreate(bundle) 中调用这个方法`getWeather(null, nameOfCity);。创建活动时只调用一次。

【讨论】:

  • 朋友首先感谢您对我的问题的宝贵考虑,我已经按照您对OnCreate()中的getWeather(null, nameOfCity);的建议进行了尝试,但应用程序仍然一次又一次地向服务器请求。
  • 是的,我已经从 onResume() 中删除了 getWeather(null, nameOfCity); .. 但仍然是同样的问题!
  • @ArshadAliArshay 您需要在代码错误的某个位置调试应用程序,这些错误会继续进行服务器调用。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2016-03-14
  • 1970-01-01
  • 1970-01-01
  • 2013-07-06
  • 2012-08-16
  • 2013-02-09
  • 2014-11-20
相关资源
最近更新 更多