package com.example.android.sunshine.app;

import android.net.Uri;
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.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;
import java.util.Arrays;

public class ForecastFragment extends Fragment {

    private ArrayAdapter<String> mForecastAdapter;

    public ForecastFragment() {
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

        View rootView = inflater.inflate(R.layout.fragment_main, container, false);

        String[] weatherData = new String[] {
                "Today - Sunny - 88/63",
                "Tomorrow - Foggy - 70/46",
                "Weds - Cloudy - 72/63",
                "Thurs - Rainy - 64/51",
                "Fri - Foggy - 70/46",
                "Sat - Sunny - 76/68",
                "Sun - Asteroids - 99/00"
        };

        ArrayList<String> weekForecast = new ArrayList<>(Arrays.asList(weatherData));

        mForecastAdapter =
                new ArrayAdapter<String>(
                        getActivity(),
                        R.layout.list_item_forecast,
                        R.id.list_item_forecast_textView,
                        weekForecast);

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

        listView.setAdapter(mForecastAdapter);

        Uri.Builder builder = new Uri.Builder();
        builder.scheme("http")
                .authority("api.openweathermap.org")
                .appendPath("data")
                .appendPath("2.5")
                .appendPath("forecast")
                .appendPath("daily")
                .appendQueryParameter("q", "94043")
                .appendQueryParameter("units", "metric")
                .appendQueryParameter("cnt", "7")
                .appendQueryParameter("appid", "cc94715e94287e49ed67f30b455b4761");
        String urlStr = builder.build().toString();

        new FetchWeatherTask().execute(urlStr);

        return rootView;
    }

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

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

        @Override
        protected String doInBackground(String... strings) {
            HttpURLConnection urlConnection = null;
            BufferedReader reader = null;
            String forecastJsonStr = null;
            try {
                URL url = new URL(strings[0]);
                urlConnection = (HttpURLConnection) url.openConnection();
                urlConnection.setRequestMethod("GET");
                urlConnection.connect();
                InputStream is = urlConnection.getInputStream();
                StringBuffer buffer = new StringBuffer();
                if (is == null) {
                    forecastJsonStr = null;
                }
                reader = new BufferedReader(new InputStreamReader(is));
                String line;
                while ((line = reader.readLine()) != null) {
                    buffer.append(line + "\n");
                }
                if (buffer.length() == 0) {
                    forecastJsonStr = null;
                }
                forecastJsonStr = buffer.toString();
            } catch (IOException e) {
                Log.e(LOG_TAG, "Error ", e);
                forecastJsonStr = null;
            } finally {
                if (urlConnection != null) {
                    urlConnection.disconnect();
                }
                if (reader != null) {
                    try {
                        reader.close();
                    } catch (final IOException e) {
                        Log.e(LOG_TAG, "Error closing stream", e);
                    }
                }
            }
            return forecastJsonStr;
        }
    }
}

@@ -1769,24 +1769,591 @@
 tAdapter);%0A%0A
+        String postcode = %2294043%22;%0A%0A        new FetchWeatherTask().execute(postcode);%0A%0A        return rootView;%0A    %7D%0A%0A    private class FetchWeatherTask extends AsyncTask%3CString, Void, String%3E %7B%0A%0A        private final String COUNT = %227%22;%0A        private final String UNITS = %22metric%22;%0A        private final String LOG_TAG = FetchWeatherTask.class.getSimpleName();%0A        private final String APP_ID = %22cc94715e94287e49ed67f30b455b4761%22;%0A%0A        @Override%0A        protected String doInBackground(String... strings) %7B%0A%0A            String postcode = strings%5B0%5D;%0A%0A    
         Uri.
@@ -2385,24 +2385,29 @@
 .Builder();%0A
+%0A    
         buil
@@ -2425,16 +2425,20 @@
 %22http%22)%0A
+    
         
@@ -2490,32 +2490,36 @@
                 
+    
 .appendPath(%22dat
@@ -2530,32 +2530,36 @@
                 
+   
+ 
 .appendPath(%222.5
@@ -2561,16 +2561,20 @@
 (%222.5%22)%0A
+    
         
@@ -2613,32 +2613,36 @@
                 
+   
+ 
 .appendPath(%22dai
@@ -2646,16 +2646,20 @@
 daily%22)%0A
+    
         
@@ -2697,17 +2697,22 @@
 q%22, 
-%2294043%22)%0A
+postcode)%0A    
     
@@ -2758,18 +2758,17 @@
 s%22, 
-%22metric%22)%0A
+UNITS)%0A  
     
@@ -2771,32 +2771,34 @@
                 
+  
 .appendQueryPara
@@ -2814,13 +2814,19 @@
 t%22, 
-%227%22)%0A
+COUNT)%0A    
     
@@ -2872,45 +2872,22 @@
 d%22, 
-%22cc94715e94287e49ed67f30b455b4761%22);%0A
+APP_ID);%0A%0A    
     
@@ -2939,326 +2939,8 @@
 );%0A%0A
-        new FetchWeatherTask().execute(urlStr);%0A%0A        return rootView;%0A    %7D%0A%0A    private class FetchWeatherTask extends AsyncTask%3CString, Void, String%3E %7B%0A%0A        private final String LOG_TAG = FetchWeatherTask.class.getSimpleName();%0A%0A        @Override%0A        protected String doInBackground(String... strings) %7B%0A
     

Move uri parameters to constants