1 package uk
.me
.njae
.sunshine
;
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
;
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
;
25 import java
.util
.ArrayList
;
26 import java
.util
.List
;
27 import java
.util
.Arrays
;
30 public class MainActivity
extends ActionBarActivity
{
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())
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
);
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
) {
60 return super.onOptionsItemSelected(item
);
64 * A placeholder fragment containing a simple view.
66 public static class PlaceholderFragment
extends Fragment
{
68 public PlaceholderFragment() {
72 public View
onCreateView(LayoutInflater inflater
, ViewGroup container
,
73 Bundle savedInstanceState
) {
74 View rootView
= inflater
.inflate(R
.layout
.fragment_main
, container
, false);
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"
86 List
<String
> weekForecast
= new ArrayList
<String
>(
87 Arrays
.asList(forecastArray
));
88 ArrayAdapter
<String
> forecastAdapter
= new ArrayAdapter
<String
>(
90 R
.layout
.list_item_forecast
,
91 R
.id
.list_item_forecast_textview
,
94 ListView list_view
= (ListView
) rootView
.findViewById(R
.id
.listview_forecast
);
95 list_view
.setAdapter(forecastAdapter
);
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;
102 // Will contain the raw JSON response as a string.
103 String forecastJsonStr
= null;
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");
111 // Create the request to OpenWeatherMap, and open the connection
112 urlConnection
= (HttpURLConnection
) url
.openConnection();
113 urlConnection
.setRequestMethod("GET");
114 urlConnection
.connect();
116 // Read the input stream into a String
117 InputStream inputStream
= urlConnection
.getInputStream();
118 StringBuffer buffer
= new StringBuffer();
119 if (inputStream
== null) {
121 forecastJsonStr
= null;
123 reader
= new BufferedReader(new InputStreamReader(inputStream
));
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");
133 if (buffer
.length() == 0) {
134 // Stream was empty. No point in parsing.
135 forecastJsonStr
= null;
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
142 forecastJsonStr
= null;
144 if (urlConnection
!= null) {
145 urlConnection
.disconnect();
147 if (reader
!= null) {
150 } catch (final IOException e
) {
151 Log
.e("PlaceholderFragment", "Error closing stream", e
);