-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMainActivity.java
More file actions
207 lines (157 loc) · 6.85 KB
/
MainActivity.java
File metadata and controls
207 lines (157 loc) · 6.85 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
package com.example.kurtiscc.weatherapp;
import android.content.Intent;
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.squareup.picasso.Picasso;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.ResponseHandler;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.BasicResponseHandler;
import org.apache.http.impl.client.DefaultHttpClient;
import org.json.JSONException;
import org.json.JSONObject;
import java.io.IOException;
public class MainActivity extends ActionBarActivity {
public final static String EXTRA_MESSAGE = "com.example.kurtiscc.weatherapp.MESSAGE";
private Button get_weather_button;
private TextView WeatherTV, TempTV, humidityTV, feels_likeTV, LocationTV;
public String weather, temp, feelsLike, humidity, wind, icon, iconURL, location_string;
public EditText zipCode;
public ImageView iconIV;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
get_weather_button = (Button) findViewById(R.id.get_weather);
WeatherTV = (TextView) findViewById(R.id.WeatherTV);
TempTV = (TextView) findViewById(R.id.TempTV);
zipCode = (EditText) findViewById(R.id.zipCode);
iconIV = (ImageView) findViewById(R.id.iconIV);
humidityTV = (TextView) findViewById(R.id.humidityTV);
feels_likeTV = (TextView) findViewById(R.id.feels_likeTV);
LocationTV = (TextView) findViewById(R.id.LocationTV);
}
public void DisplayJSON() {
WeatherTV.setText(weather);
TempTV.setText(temp);
humidityTV.setText(humidity);
feels_likeTV.setText(feelsLike);
LocationTV.setText(location_string);
Uri uri = Uri.parse(iconURL);
Picasso.with(MainActivity.this)
.load(uri)
.resize(150,150)
.centerCrop()
.into(iconIV);
}
@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();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
public void onClick(View v) {
switch (v.getId()) {
case R.id.get_weather:
String zipcode = String.valueOf(zipCode.getText());
GetWeather get_weather_stuff = new GetWeather(zipcode);
get_weather_stuff.execute();
zipCode.setText("");
break;
default:
return;
}
}
public void getForecast(View v) {
Intent intent = new Intent(this, ForecastActivity.class);
Button getForecast = (Button) findViewById(R.id.get_forecast);
String message = getForecast.getText().toString();
intent.putExtra(EXTRA_MESSAGE, message);
startActivity(intent);
}
public void getGraph(View v) {
Intent intent = new Intent(this, GraphActivity.class);
Button getGraph = (Button) findViewById(R.id.get_graph);
String message = getGraph.getText().toString();
intent.putExtra(EXTRA_MESSAGE, message);
startActivity(intent);
}
public class GetWeather extends AsyncTask<Void, Void, Boolean> {
JSONObject jsonObject = null;
String zipcode;
public GetWeather (String zipcode) {
this.zipcode = zipcode;
}
@Override
protected Boolean doInBackground(Void... params) {
String postURL = "http://api.wunderground.com/api/011531b65971abde/conditions/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 {
JSONObject response = jsonObject.getJSONObject("current_observation");
JSONObject location = response.getJSONObject("display_location");
location_string = location.get("full").toString();
weather = response.get("weather").toString();
temp = response.get("temperature_string").toString();
feelsLike = response.get("feelslike_string").toString();
humidity = response.get("relative_humidity").toString();
wind = response.get("wind_string").toString();
icon = response.get("icon").toString();
iconURL = response.get("icon_url").toString();
DisplayJSON();
Log.v("Weather", weather);
Log.v("Temperature", temp);
} catch (JSONException e) {
Log.v("Error", e.toString());
}
}
else {
}
}
}
}