package com.whilchy.sunshine.app;

import android.support.v4.app.Fragment;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ListView;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;


public class MainActivity extends ActionBarActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        if (savedInstanceState == null) {
            getSupportFragmentManager().beginTransaction()
                    .add(R.id.container, new PlaceholderFragment())
                    .commit();
        }
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

    /**
     * A placeholder fragment containing a simple view.
     */
    public static class PlaceholderFragment extends Fragment {

        public PlaceholderFragment() {
        }

        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                                 Bundle savedInstanceState) {
            // Dummy data
            String[] data = {
                    "Mon 6/23 - Sunny - 31/17",
                    "Tue 6/24 - Foggy - 21/8",
                    "Wed 6/25 - Cloudy - 22/17",
                    "Thurs 6/26 - Rainy - 18/11",
                    "Fri 6/27 - Foggy - 21/10",
                    "Sat 6/28 - TRAPPED IN WEATHERSTATION - 23/18",
                    "Sun 6/29 - Sunny - 20/7"
            };

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

            // Now we create the Array Adapter and pass it the dummy data we've just created
            // we will use it to populate the ListView
            ArrayAdapter<String> forecastAdapter = new ArrayAdapter<String>(
                    getActivity(),
                    R.layout.list_item_forecast,
                    R.id.list_item_forecast_textview,
                    weekForecast
            );

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

            ListView forecastListView = (ListView) rootView.findViewById(R.id.listview_forecast);
            forecastListView.setAdapter(forecastAdapter);

            return rootView;
        }
    }
}

@@ -143,16 +143,41 @@
 Bundle;%0A
+import android.util.Log;%0A
 import a
@@ -386,16 +386,272 @@
 tView;%0A%0A
+import org.apache.http.HttpConnection;%0A%0Aimport java.io.BufferedReader;%0Aimport java.io.IOException;%0Aimport java.io.InputStream;%0Aimport java.io.InputStreamReader;%0Aimport java.net.HttpURLConnection;%0Aimport java.net.MalformedURLException;%0Aimport java.net.URL;%0A
 import j
@@ -3348,24 +3348,2022 @@
 tAdapter);%0A%0A
+            // Connects to OpenWeatherMaps API to retrieve the data%0A            HttpURLConnection urlConnection = null;%0A            BufferedReader reader = null;%0A%0A            // Will contains the JSON response as String%0A            String forecastJsonString = null;%0A%0A            try %7B%0A                // URL for the OpenWeatherMap query%0A                // More info at http://openweathermap.org/API%0A                URL url = new URL(%22http://api.openweathermap.org/data/2.5/forecast/daily?q=Oliveira+de+Azemeis&mode=json&units=metric&cnt=7%22);%0A%0A                // Creates the GET request to OpenWeatherMap%0A                urlConnection = (HttpURLConnection) url.openConnection();%0A                urlConnection.setRequestMethod(%22GET%22);%0A                urlConnection.connect();%0A%0A                // Read the input stream into a stream%0A                InputStream inputStream = urlConnection.getInputStream();%0A                StringBuffer buffer = new StringBuffer();%0A                if(buffer == null) %7B%0A                    return null;    // No buffer%0A                %7D%0A%0A                reader = new BufferedReader(new InputStreamReader(inputStream));%0A%0A                String line;%0A                while ((line = reader.readLine()) != null) %7B%0A                    buffer.append(line + %22%5Cn%22);%0A                %7D%0A%0A                if(buffer.length() == 0) %7B%0A                    return null;    // Stream empty%0A                %7D%0A                forecastJsonString = buffer.toString();%0A%0A            %7D catch (MalformedURLException e) %7B%0A                Log.e(String.format(%22Malformed URL: %5B%25s%5D%22, e.getMessage()), %22Error%22, e);%0A                // If we cannot wasn't successfull there's no point in attempting parse it%0A                return null;%0A            %7D catch (IOException e) %7B%0A                Log.e(String.format(%22Connection Error: %5B%25s%5D%22, e.getMessage()), %22Error%22, e);%0A                // If we cannot wasn't successfull there's no point in attempting parse it%0A                return null;%0A            %7D%0A%0A
             

Add the network call