-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWeatherAPI.java
More file actions
76 lines (73 loc) · 2.7 KB
/
Copy pathWeatherAPI.java
File metadata and controls
76 lines (73 loc) · 2.7 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
/*
* Name: Kota Naga Tejaswi LNU
* Class: 2336
* Assignment: Rest API Project
*/
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.Scanner;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
public class WeatherAPI
{
//static variables
static String uCity;
static StringBuilder result;
//main method
public static void main(String[] args)
{
try {
//calls to the api
String weatherURL = "http://api.openweathermap.org/data/2.5/weather?q=" + uCity + "&APPID=2045126796536689aa40d7f7d49fd496"; //please get your own token to use from API.Openweathermap
//Json Response
result = new StringBuilder();
URL url = new URL(weatherURL);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("GET");
BufferedReader rd = new BufferedReader(new InputStreamReader(conn.getInputStream()));
String line;
//reads the output
while ((line = rd.readLine()) != null) {
result.append(line);
}
rd.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
//Minimum Temperature
static double parseMinTemp(String json){
JsonElement jelement = new JsonParser().parse(json);
JsonObject MasterWeatherObject = jelement.getAsJsonObject();
JsonObject mainObject = MasterWeatherObject.getAsJsonObject("main");
double minTemp = mainObject.get("temp_min").getAsDouble();
double minTempF = minTemp * 9/5 - 459.67;
//Double minT = minTempF;
return minTempF;
}
//Maximum Temperature
static double parseMaxTemp(String json){
JsonElement jelement = new JsonParser().parse(json);
JsonObject MasterWeatherObject = jelement.getAsJsonObject();
JsonObject mainObject = MasterWeatherObject.getAsJsonObject("main");
double maxTemp = mainObject.get("temp_max").getAsDouble();
double maxTempF = maxTemp * 9/5 - 459.67;
//Double minT = minTempF;
return maxTempF;
}
//Current Temperature
static double parseTemp(String json){
JsonElement jelement = new JsonParser().parse(json);
JsonObject MasterWeatherObject = jelement.getAsJsonObject();
JsonObject mainObject = MasterWeatherObject.getAsJsonObject("main");
double temp = mainObject.get("temp").getAsDouble();
double tempF = temp * 9/5 - 459.67;
//Double minT = minTempF;
return tempF;
}
}