File tree 1 file changed +38
-0
lines changed
1 file changed +38
-0
lines changed Original file line number Diff line number Diff line change
1
+ import java .io .BufferedReader ;
2
+ import java .io .IOException ;
3
+ import java .io .InputStreamReader ;
4
+ import java .net .HttpURLConnection ;
5
+ import java .net .URL ;
6
+
7
+ public class WeatherApp {
8
+
9
+ public static void main (String [] args ) {
10
+ String apiKey = "YOUR_API_KEY" ; // Replace with your OpenWeatherMap API key
11
+ String city = "New York" ; // Replace with the desired city
12
+
13
+ try {
14
+ String apiUrl = "http://api.openweathermap.org/data/2.5/weather?q=" + city + "&appid=" + apiKey ;
15
+ URL url = new URL (apiUrl );
16
+
17
+ HttpURLConnection connection = (HttpURLConnection ) url .openConnection ();
18
+ connection .setRequestMethod ("GET" );
19
+
20
+ BufferedReader reader = new BufferedReader (new InputStreamReader (connection .getInputStream ()));
21
+ String line ;
22
+ StringBuilder response = new StringBuilder ();
23
+
24
+ while ((line = reader .readLine ()) != null ) {
25
+ response .append (line );
26
+ }
27
+ reader .close ();
28
+
29
+ // Parse and display the weather information
30
+ String weatherData = response .toString ();
31
+ System .out .println ("Weather Data:" );
32
+ System .out .println (weatherData );
33
+
34
+ } catch (IOException e ) {
35
+ e .printStackTrace ();
36
+ }
37
+ }
38
+ }
You can’t perform that action at this time.
0 commit comments