2016-09-01 11 views
1
 protected String[] doInBackground(String... params) { 
     String format = "json"; 
     String units = "metric"; 
     int numDays = 10; 

     try { 
      // Construct the URL for the OpenWeatherMap query 
      // Possible parameters are avaiable at OWM's forecast API page, at 
      // http://openweathermap.org/API#forecast 
      final String FORECAST_BASE_URL = 
        "http://api.openweathermap.org/data/2.5/forecast/daily?"; 
      final String QUERY_PARAM = "q"; 
      final String QUERY=" "; 
      final String FORMAT_PARAM = "mode"; 
      final String UNITS_PARAM = "units"; 
      final String DAYS_PARAM = "cnt"; 
      final String APPID_PARAM = "APPID"; 

      Uri builtUri = Uri.parse(FORECAST_BASE_URL).buildUpon() 
        .appendQueryParameter(QUERY_PARAM, params[0]) 
        .appendQueryParameter(FORMAT_PARAM, format) 
        .appendQueryParameter(UNITS_PARAM, units) 
        .appendQueryParameter(DAYS_PARAM, Integer.toString(numDays)) 
        .appendQueryParameter(APPID_PARAM, BuildConfig.OPEN_WEATHER_MAP_API_KEY) 
        .build(); 

      URL url = new URL(builtUri.toString()); 

      Log.v(LOG_TAG, "Built URI " + builtUri.toString()); 

這是我使用的建設URI的基本功能......現在我想改變這行:」 .appendQueryParameter(DAYS_PARAM,Integer.toString(NUMDAYS) )「 Bsically我想要的是天數應該由用戶手動輸入我的設置,即共享偏好文件。關於Android和異步類的URI建設者

   <?xml version="1.0" encoding="utf-8"?> 
       <PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android" 
            android:layout_width="match_parent" 
            android:layout_height="match_parent"> 

     <EditTextPreference 
      android:title="@string/pref_location_label" 
      android:key="@string/pref_location_key" 
      android:defaultValue="@string/pref_location_default" 
      android:inputType="text" 
      android:singleLine="true" /> 

      <EditTextPreference 
      android:title="@string/pref_numdays_label" 
      android:key="@string/pref_numdays_key" 
      android:defaultValue="@string/pref_numdays_default" 
      android:inputType="text" 
      android:singleLine="true" /> 

現在,你可以看到:「.appendQueryParameter(QUERY_PARAM,則params [0])」這條線是用來追加位置設置我從用戶那裏得到同樣地,我想利用輸入的天NUM和追加 「.appendQueryParameter(DAYS_PARAM,Integer.toString(NUMDAYS))」

這是代碼的一部分,其中i調用的方法:

private void updateWeather() 
    { 
     FetchWeatherTask weatherTask = new FetchWeatherTask(); 
     SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getActivity()); 

     String location = prefs.getString(getString(R.string.pref_location_key), 
       getString(R.string.pref_location_default)); 

     String num=prefs.getString(getString(R.string.pref_numdays_key),getString(R.string.pref_numdays_default)); 

     weatherTask.execute(location); 

    } 

所以我的問題是:像我傳遞「位置「並將其用作」params [0],同時構建我的URI,同樣如何傳遞「numofdays」來追加同時建立我的URI ..ihope你明白的問題,請評論..所以基本上問題歸結爲我如何使用其他用戶輸入的偏好建立我的URI

回答

0

如果我正確理解你的問題,你問如何通過可變參數「String ...」將更多參數傳遞給你的asynctask。

要做到這一點,你可以通過多個字符串以逗號分隔的,像這樣:weatherTask.execute(location, num),然後引用它作爲param[0]param[1]

https://stackoverflow.com/a/9901943/5527154

這裏是Java有關的可變參數一些更多的信息:Java variable number or arguments for a method

注意: 在更高的水平,我會建議檢查改裝來處理調用這個API,而不是通過一個AsyncTask它,它具有不同步的所有優點,但更容易處理。回答

http://square.github.io/retrofit/

+0

先生感謝名單我已經嘗試過這一點,但它開始讓錯誤在幹什麼後臺方法 我做到了,如:.appendQueryParameter(DAYS_PARAM帕拉姆[1]) 和updateweather() weatherTask .execute(位置,NUM); 我也從doInbackground中刪除了它,但它不工作:int numDays = 10; –

+0

你會得到什麼錯誤? –

+0

它給出錯誤...執行doiNbACKGROUND時出錯() –