-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathForecastActivity.java
More file actions
238 lines (178 loc) · 7.56 KB
/
ForecastActivity.java
File metadata and controls
238 lines (178 loc) · 7.56 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
package com.example.kurtiscc.weatherapp;
import android.net.Uri;
import android.os.AsyncTask;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import com.jjoe64.graphview.GraphView;
import com.jjoe64.graphview.series.DataPoint;
import com.jjoe64.graphview.series.LineGraphSeries;
import com.squareup.picasso.Picasso;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.ResponseHandler;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.BasicResponseHandler;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.io.IOException;
import java.net.URI;
public class ForecastActivity extends ActionBarActivity {
public EditText zipCode_forecast;
private Button get_forecast_button;
private TextView day_one, day_two, day_three;
private ImageView icon_one, icon_two, icon_three;
private TextView high_low_one, high_low_two, high_low_three;
public String[] days = new String[4];
public String[] icons = new String[4];
public String[] highLow = new String[4];
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_forecast);
get_forecast_button = (Button) findViewById(R.id.get_forecast);
//ForecastTV = (TextView) findViewById(R.id.ForecastTV);
zipCode_forecast = (EditText) findViewById(R.id.zipCode_forecast);
day_one = (TextView) findViewById(R.id.day_one);
day_two = (TextView) findViewById(R.id.day_two);
day_three = (TextView) findViewById(R.id.day_three);
//day_four = (TextView) findViewById(R.id.day_four);
icon_one = (ImageView) findViewById(R.id.icon_one);
icon_two = (ImageView) findViewById(R.id.icon_two);
icon_three = (ImageView) findViewById(R.id.icon_three);
// icon_four = (ImageView) findViewById(R.id.icon_four);
high_low_one = (TextView) findViewById(R.id.high_low_one);
high_low_two = (TextView) findViewById(R.id.high_low_two);
high_low_three = (TextView) findViewById(R.id.high_low_three);
//high_low_four = (TextView) findViewById(R.id.high_low_four);
}
@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_forecast, 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();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
public void DisplayForecastJSON() {
//ForecastTV.setText(title);
day_one.setText(days[0]);
day_two.setText(days[1]);
day_three.setText(days[2]);
//day_four.setText(days[3]);
Uri uri = Uri.parse(icons[0]);
Picasso.with(ForecastActivity.this)
.load(uri)
.resize(100,100)
.centerCrop()
.into(icon_one);
Uri uri1 = Uri.parse(icons[1]);
Picasso.with(ForecastActivity.this)
.load(uri1)
.resize(100,100)
.centerCrop()
.into(icon_two);
Uri uri2 = Uri.parse(icons[2]);
Picasso.with(ForecastActivity.this)
.load(uri2)
.resize(100,100)
.centerCrop()
.into(icon_three);
high_low_one.setText(highLow[0]);
high_low_two.setText(highLow[1]);
high_low_three.setText(highLow[2]);
// high_low_four.setText(highLow[3]);
}
public void onClick_forecast(View v) {
switch (v.getId()) {
case R.id.get_forecast:
String zipcode = String.valueOf(zipCode_forecast.getText());
GetForecast get_forecast_stuff = new GetForecast(zipcode);
get_forecast_stuff.execute();
zipCode_forecast.setText("");
break;
default:
return;
}
}
public class GetForecast extends AsyncTask<Void, Void, Boolean> {
JSONObject jsonObject = null;
String zipcode;
public GetForecast(String zipcode) {
this.zipcode = zipcode;
}
@Override
protected Boolean doInBackground(Void... params) {
String postURL = "http://api.wunderground.com/api/011531b65971abde/forecast10day/q/" + zipcode + ".json";
try {
//Instantiates an HttpClient
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost(postURL);
try {
ResponseHandler<String> responseHandler = new BasicResponseHandler();
// Executes HTTP Post request and puts response into responseBody
String responseBody = httpclient.execute(httppost, responseHandler);
jsonObject = new JSONObject(responseBody);
Log.v("Response", jsonObject.toString());
return true;
} catch (ClientProtocolException e) {
return false;
} catch (IOException e) {
return false;
}
} catch (Throwable t) {
return false;
}
}
@Override
protected void onPostExecute(final Boolean success) {
if (success) {
//Parse JSON Object here
try {
JSONArray jsonDays = jsonObject.getJSONObject("forecast").getJSONObject("simpleforecast").getJSONArray("forecastday");
// Log.v("Array Length", String.valueOf(forecastArray.length()));
for (int i = 0; i < 4; i++) {
days[i] = jsonDays.getJSONObject(i).getJSONObject("date").getString("weekday_short");
icons[i] = jsonDays.getJSONObject(i).getString("icon_url");
String highLows = "High: "
+ jsonDays.getJSONObject(i).getJSONObject("high").getString("fahrenheit")
+ " Low: "
+ jsonDays.getJSONObject(i).getJSONObject("low").getString("fahrenheit");
highLow[i] = highLows;
//Log.v("Title", title);
}
DisplayForecastJSON();
}
catch (JSONException jse) {
Log.e("error", "Error parsing JSON", jse);
return;
}
} else {
Log.i("error",
"String returned was Null, check doInBackground for errors");
}
}
}
}