3d687974fd39433cb11a6fe20fb397bc0b9de4fb
[Sunshine.git] / app / src / main / java / uk / me / njae / sunshine / MainActivity.java
1 package uk.me.njae.sunshine;
2
3 import android.support.v7.app.ActionBarActivity;
4 import android.support.v7.app.ActionBar;
5 import android.support.v4.app.Fragment;
6 import android.os.Bundle;
7 import android.util.Log;
8 import android.view.LayoutInflater;
9 import android.view.Menu;
10 import android.view.MenuItem;
11 import android.view.View;
12 import android.view.ViewGroup;
13 import android.os.Build;
14 import android.widget.ArrayAdapter;
15 import android.widget.ListView;
16
17 import java.io.BufferedReader;
18 import java.io.IOException;
19 import java.io.InputStream;
20 import java.io.InputStreamReader;
21 import java.net.HttpURLConnection;
22 import java.net.URL;
23
24
25 import java.util.ArrayList;
26 import java.util.List;
27 import java.util.Arrays;
28
29
30 public class MainActivity extends ActionBarActivity {
31
32 @Override
33 protected void onCreate(Bundle savedInstanceState) {
34 super.onCreate(savedInstanceState);
35 setContentView(R.layout.activity_main);
36 if (savedInstanceState == null) {
37 getSupportFragmentManager().beginTransaction()
38 .add(R.id.container, new PlaceholderFragment())
39 .commit();
40 }
41 }
42
43
44 @Override
45 public boolean onCreateOptionsMenu(Menu menu) {
46 // Inflate the menu; this adds items to the action bar if it is present.
47 getMenuInflater().inflate(R.menu.main, menu);
48 return true;
49 }
50
51 @Override
52 public boolean onOptionsItemSelected(MenuItem item) {
53 // Handle action bar item clicks here. The action bar will
54 // automatically handle clicks on the Home/Up button, so long
55 // as you specify a parent activity in AndroidManifest.xml.
56 int id = item.getItemId();
57 if (id == R.id.action_settings) {
58 return true;
59 }
60 return super.onOptionsItemSelected(item);
61 }
62
63 /**
64 * A placeholder fragment containing a simple view.
65 */
66 public static class PlaceholderFragment extends Fragment {
67
68 public PlaceholderFragment() {
69 }
70
71 @Override
72 public View onCreateView(LayoutInflater inflater, ViewGroup container,
73 Bundle savedInstanceState) {
74 View rootView = inflater.inflate(R.layout.fragment_main, container, false);
75
76 String[] forecastArray = {
77 "Today - Sunny - 10/10",
78 "Tomorrow - Cloudy - 11/11",
79 "Tuesday - Snow - 12/12",
80 "Wednesday - Rain - 13/13",
81 "Thursday - Hail - 14/14",
82 "Friday - Scorchio - 15/15",
83 "Saturday - Fog - 16/16"
84 };
85
86 List<String> weekForecast = new ArrayList<String>(
87 Arrays.asList(forecastArray));
88 ArrayAdapter<String> forecastAdapter = new ArrayAdapter<String>(
89 getActivity(),
90 R.layout.list_item_forecast,
91 R.id.list_item_forecast_textview,
92 weekForecast
93 );
94 ListView list_view = (ListView) rootView.findViewById(R.id.listview_forecast);
95 list_view.setAdapter(forecastAdapter);
96
97 // These two need to be declared outside the try/catch
98 // so that they can be closed in the finally block.
99 HttpURLConnection urlConnection = null;
100 BufferedReader reader = null;
101
102 // Will contain the raw JSON response as a string.
103 String forecastJsonStr = null;
104
105 try {
106 // Construct the URL for the OpenWeatherMap query
107 // Possible parameters are avaiable at OWM's forecast API page, at
108 // http://openweathermap.org/API#forecast
109 URL url = new URL("http://api.openweathermap.org/data/2.5/forecast/daily?q=94043&mode=json&units=metric&cnt=7");
110
111 // Create the request to OpenWeatherMap, and open the connection
112 urlConnection = (HttpURLConnection) url.openConnection();
113 urlConnection.setRequestMethod("GET");
114 urlConnection.connect();
115
116 // Read the input stream into a String
117 InputStream inputStream = urlConnection.getInputStream();
118 StringBuffer buffer = new StringBuffer();
119 if (inputStream == null) {
120 // Nothing to do.
121 forecastJsonStr = null;
122 }
123 reader = new BufferedReader(new InputStreamReader(inputStream));
124
125 String line;
126 while ((line = reader.readLine()) != null) {
127 // Since it's JSON, adding a newline isn't necessary (it won't affect parsing)
128 // But it does make debugging a *lot* easier if you print out the completed
129 // buffer for debugging.
130 buffer.append(line + "\n");
131 }
132
133 if (buffer.length() == 0) {
134 // Stream was empty. No point in parsing.
135 forecastJsonStr = null;
136 }
137 forecastJsonStr = buffer.toString();
138 } catch (IOException e) {
139 Log.e("PlaceholderFragment", "Error ", e);
140 // If the code didn't successfully get the weather data, there's no point in attemping
141 // to parse it.
142 forecastJsonStr = null;
143 } finally{
144 if (urlConnection != null) {
145 urlConnection.disconnect();
146 }
147 if (reader != null) {
148 try {
149 reader.close();
150 } catch (final IOException e) {
151 Log.e("PlaceholderFragment", "Error closing stream", e);
152 }
153 }
154 }
155
156 return rootView;
157 }
158 }
159 }