0
        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

Nick Yelito
  • 77
  • 1
  • 10

1 Answers1

0

If I am understanding your question correctly, you are asking how to pass more parameters to your asynctask through the variable arguments "String..." you have setup.

To do so, you can just pass more Strings comma delimited like so: weatherTask.execute(location, num) and then reference it as param[0] or param[1]

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

Here is some more info about varargs in Java: Java variable number or arguments for a method

Note: At a higher level I would suggest checking out Retrofit to handle calling this API instead of doing it through an AsyncTask, it has all of the same benefits of asynchronicity but is much easier to deal with.

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

Community
  • 1
  • 1
Nick Yelito
  • 77
  • 1
  • 10
  • sir thanx for answering i already tried this but it starts giving errors in doIn background method I did it like : .appendQueryParameter(DAYS_PARAM, param[1]) and in updateweather() weatherTask.execute(location,num); I also removed this from doInbackground but its not working: int numDays = 10; – Yashaswi Bhardwaj Sep 01 '16 at 20:06
  • it gives error ...error in executing doiNbACKGROUND() – Yashaswi Bhardwaj Sep 03 '16 at 17:48