package com.example.android.sunshine.app;

import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ListView;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;

public class ForecastFragment extends Fragment {

    ArrayAdapter forecastArrayAdapter;

    public ForecastFragment() {
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setHasOptionsMenu(true);
    }

    @Override
    public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
        inflater.inflate(R.menu.forecastfragment, menu);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {

        int id = item.getItemId();
        if (id == R.id.action_refresh) {
            new FetchWeatherTask().execute();
        }

        return super.onOptionsItemSelected(item);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_main, container, false);

        ArrayList<String> forecastArrayList = new ArrayList<>();
        forecastArrayList.add("Mon 31/10 - Sunny 29º/16º");
        forecastArrayList.add("Tue 1/11 - Stormy 30º/18º");
        forecastArrayList.add("Wed 2/11 - Stormy 32º/19º ");
        forecastArrayList.add("Thu 3/11 - Cloudy 30º/21º");
        forecastArrayList.add("Fri 4/11 - Cloudy 24º/10º ");

        forecastArrayAdapter = new ArrayAdapter(
                getActivity(),
                R.layout.list_item_forecast,
                R.id.list_item_forecast_textview,
                forecastArrayList);

        ListView listView = (ListView) rootView.findViewById(R.id.listview_forecast);
        listView.setAdapter(forecastArrayAdapter);

        new FetchWeatherTask().execute();

        return rootView;

    }

    public class FetchWeatherTask extends AsyncTask<Void, Void, String> {

        private final String LOG_TAG = FetchWeatherTask.class.getSimpleName();

        @Override
        protected String doInBackground(Void... params) {

            try {
                return OpenWeatherAPIConnection();
            } catch (IOException e) {
                return "Unable to retrieve data";
            }

        }

        @Override
        protected void onPostExecute(String s) {
            super.onPostExecute(s);
        }

        private String OpenWeatherAPIConnection() throws IOException {
            String forecastJsonStr = null;

            String APPID = BuildConfig.OPEN_WEATHER_MAP_API_KEY;

            HttpURLConnection connection = null;
            try {
                URL url = new URL("http://api.openweathermap.org/data/2.5/forecast/daily?q=94043,US&units=metric&cnt=7&mode=json&APPID=" + APPID);

                connection = (HttpURLConnection) url.openConnection();
                connection.setRequestMethod("GET");
                connection.connect();
                int responseCode = connection.getResponseCode();
                Log.d(LOG_TAG, "Response code: " + responseCode);


                InputStream inputStream = connection.getInputStream();
                //forecastJsonStr = readStream(inputStream);
                if (inputStream != null) {
                    /** If we get response for our request
                     * buffer the data and store it as a String.
                     */
                    StringBuffer buffer = new StringBuffer();
                    BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
                    buffer.append(reader.readLine());
//                    while ((line = reader.readLine()) != null) {
//                        // Since it's JSON, adding a newline isn't necessary (it won't affect parsing)
//                        // But it does make debugging a *lot* easier if you print out the completed
//                        // buffer for debugging.
//                        buffer.append(line + "\n");
//                    }

                    forecastJsonStr = buffer.toString();
                    Log.d(LOG_TAG, forecastJsonStr);
                    reader.close();
                }

            } catch (IOException e) {
                e.printStackTrace();
                Log.e(LOG_TAG, "Error: ", e);
            } finally {
                if (connection != null) {
                    connection.disconnect();
                }

            }
            return forecastJsonStr;
        }
    }

}

@@ -36,16 +36,40 @@
 e.app;%0A%0A
+import android.net.Uri;%0A
 import a
@@ -2958,16 +2958,17 @@
 ption %7B%0A
+%0A
         
@@ -3019,128 +3019,676 @@
     
-String APPID = BuildConfig.OPEN_WEATHER_MAP_API_KEY;%0A%0A            HttpURLConnection connection = null;%0A            try %7B
+HttpURLConnection connection = null;%0A%0A            String postcode = %2294043%22;%0A            String units = %22metric%22;%0A            int numDays = 7;%0A            String responseFormat = %22json%22;%0A            String APPID = BuildConfig.OPEN_WEATHER_MAP_API_KEY;%0A%0A            try %7B%0A                final String FORECAST_BASE_URL = %22http://api.openweathermap.org/data/2.5/forecast%22;%0A                final String PATH = %22daily%22;%0A                final String QUERY_PARAM = %22q%22;%0A                final String UNITS_PARAM = %22units%22;%0A                final String DAYS_PARAM = %22cnt%22;%0A                final String FORMAT_PARAM = %22mode%22;%0A                final String APPID_PARAM = %22APPID%22;
 %0A   
@@ -3705,135 +3705,606 @@
    U
-RL url = new URL(%22http://api.openweathermap.org/data/2.5/forecast/daily?q=94043,US&units=metric&cnt=7&mode=json&APPID=%22 + APPID
+ri uriBuilder = Uri.parse(FORECAST_BASE_URL).buildUpon()%0A                        .appendPath(PATH)%0A                        .appendQueryParameter(QUERY_PARAM, postcode)%0A                        .appendQueryParameter(UNITS_PARAM, units)%0A                        .appendQueryParameter(DAYS_PARAM, Integer.toString(numDays))%0A                        .appendQueryParameter(FORMAT_PARAM, responseFormat)%0A                        .appendQueryParameter(APPID_PARAM, APPID)%0A                        .build();%0A%0A                URL url = new URL(uriBuilder.toString());%0A                Log.d(LOG_TAG, %22URL: %22 + url
 );%0A%0A
@@ -4541,33 +4541,33 @@
             Log.
-d
+v
 (LOG_TAG, %22Respo
@@ -5613,33 +5613,33 @@
             Log.
-d
+v
 (LOG_TAG, foreca
@@ -5631,16 +5631,43 @@
 LOG_TAG,
+ %22Forecast JSON string: %22 +
  forecas

Refactor to build URL with params