Skip to content

Commit 4469bff

Browse files
committed
Weather App
Tells about weather status of Location as per the location entered by user and Toast friendlly comment whether to go out or its rainy out or sunny out
1 parent 114c6a8 commit 4469bff

File tree

1 file changed

+164
-0
lines changed

1 file changed

+164
-0
lines changed

weatherapp/MainActivity.java

+164
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,164 @@
1+
package com.lenovo.weatherapp;
2+
3+
import android.content.Context;
4+
import android.os.AsyncTask;
5+
import android.support.v7.app.AppCompatActivity;
6+
import android.os.Bundle;
7+
import android.util.Log;
8+
import android.view.View;
9+
import android.view.inputmethod.InputMethodManager;
10+
import android.widget.EditText;
11+
import android.widget.TextView;
12+
import android.widget.Toast;
13+
14+
import org.json.JSONArray;
15+
import org.json.JSONException;
16+
import org.json.JSONObject;
17+
18+
import java.io.InputStream;
19+
import java.io.InputStreamReader;
20+
import java.io.UnsupportedEncodingException;
21+
import java.net.HttpURLConnection;
22+
import java.net.URL;
23+
import java.net.URLEncoder;
24+
25+
public class MainActivity extends AppCompatActivity {
26+
//http://api.openweathermap.org/data/2.5/weather?q=london&APPID=bdb2e16603e00424937aed3aed6fb1a0
27+
28+
EditText cityName;
29+
TextView resultTextView;
30+
31+
public void findWeather(View view) {
32+
33+
Log.i("cityName", cityName.getText().toString());
34+
35+
InputMethodManager mgr = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
36+
mgr.hideSoftInputFromWindow(cityName.getWindowToken(), 0);
37+
38+
try {
39+
String encodedCityName = URLEncoder.encode(cityName.getText().toString(), "UTF-8");
40+
41+
DownloadTask task = new DownloadTask();
42+
task.execute("http://api.openweathermap.org/data/2.5/weather?q=" + encodedCityName+"&APPID=bdb2e16603e00424937aed3aed6fb1a0");
43+
44+
45+
} catch (UnsupportedEncodingException e) {
46+
47+
e.printStackTrace();
48+
49+
Toast.makeText(getApplicationContext(), "Could not find weather", Toast.LENGTH_LONG);
50+
51+
}
52+
53+
54+
55+
}
56+
57+
58+
59+
@Override
60+
protected void onCreate(Bundle savedInstanceState) {
61+
super.onCreate(savedInstanceState);
62+
setContentView(R.layout.activity_main);
63+
64+
cityName = (EditText) findViewById(R.id.cityName);
65+
resultTextView = (TextView) findViewById(R.id.resultTextView);
66+
}
67+
68+
public class DownloadTask extends AsyncTask<String, Void, String> {
69+
70+
@Override
71+
protected String doInBackground(String... urls) {
72+
73+
String result = "";
74+
URL url;
75+
HttpURLConnection urlConnection = null;
76+
77+
try {
78+
url = new URL(urls[0]);
79+
80+
urlConnection = (HttpURLConnection) url.openConnection();
81+
82+
InputStream in = urlConnection.getInputStream();
83+
84+
InputStreamReader reader = new InputStreamReader(in);
85+
86+
int data = reader.read();
87+
88+
while (data != -1) {
89+
90+
char current = (char) data;
91+
92+
result += current;
93+
94+
data = reader.read();
95+
96+
}
97+
98+
return result;
99+
100+
} catch (Exception e) {
101+
102+
Log.i("Could not find weather","");
103+
resultTextView.setText(View.FIND_VIEWS_WITH_TEXT);
104+
}
105+
106+
return null;
107+
}
108+
109+
@Override
110+
protected void onPostExecute(String result) {
111+
super.onPostExecute(result);
112+
113+
try {
114+
115+
String message = "";
116+
117+
JSONObject jsonObject = new JSONObject(result);
118+
119+
String weatherInfo = jsonObject.getString("weather");
120+
121+
Log.i("Weather content", weatherInfo);
122+
123+
JSONArray arr = new JSONArray(weatherInfo);
124+
125+
for (int i = 0; i < arr.length(); i++) {
126+
127+
JSONObject jsonPart = arr.getJSONObject(i);
128+
129+
String main = "";
130+
String description = "";
131+
132+
main = jsonPart.getString("main");
133+
description = jsonPart.getString("description");
134+
135+
if (main != "" && description != "") {
136+
137+
message += main + ": " + description + "\r\n";
138+
139+
}
140+
141+
}
142+
143+
if (message != "") {
144+
145+
resultTextView.setText(message);
146+
147+
} else {
148+
149+
Toast.makeText(getApplicationContext(), "Could not find weather", Toast.LENGTH_LONG);
150+
151+
}
152+
153+
154+
} catch (JSONException e) {
155+
156+
Toast.makeText(getApplicationContext(), "Could not find weather", Toast.LENGTH_LONG);
157+
158+
}
159+
160+
161+
162+
}
163+
}
164+
}

0 commit comments

Comments
 (0)