1

我開發了一個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

回答

1

我看着你的代碼。

您正在撥打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