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());
This is the basic function i am using for building the URI ...now i want to change the line : ".appendQueryParameter(DAYS_PARAM, Integer.toString(numDays))" Bsically what i am trying is num of days should be manually entered by the user in my settings ie shared preference file.
<?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" />
Now,as you can see : ".appendQueryParameter(QUERY_PARAM, params[0])" this line is used to append location settings i get from user similarly i want to get the use entered num of days and append the ".appendQueryParameter(DAYS_PARAM, Integer.toString(numDays))"
This is the part of code where i call the method:
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);
}
So my question is: like i am passing "location" and using it as "params[0] while building my URI similarly how can i pass "numofdays" to append while building my URI..ihope you understand the question please comment ..So basically question comes down to how can i use other user entered preference in building up of my URI