代码一次又一次地请求雅虎天气服务

问题描述:

我开发了一个Android应用程序,它使用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和再次在活动之间切换。

+1

调查您的数据请求Android服务。 – 2013-03-10 06:44:43

+0

@MorrisonChang我该怎么做,请给我一些提示,如果? – 2013-03-10 06:55:19

我看着你的代码。

您正在拨打onResume()调用getWeather(null, nameOfCity);。当您的活动再次恢复时,将调用onResume()方法。

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

+0

朋友首先感谢您对我的问题的宝贵考虑,我试着根据您对'OnCreate()'中的'getWeather(null,nameOfCity)'的建议,但仍然是应用程序一再向服务器请求。 – 2013-03-10 06:52:23

+0

@ArshadAliArshay从onResume()中删除这个'getWeather(null,nameOfCity);'.. – 2013-03-10 06:53:15

+0

是的,我已经从'onResume()''中删除'getWeather(null,nameOfCity);'但仍然是同样的问题! – 2013-03-10 06:54:45