-
Notifications
You must be signed in to change notification settings - Fork 0
/
GetWeatherFromZip.java
38 lines (31 loc) · 1.1 KB
/
GetWeatherFromZip.java
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
import java.io.IOException;
import java.net.URI;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
public class GetWeatherFromZip {
// Get API key at www.interzoid.com/register
public static void main(String[] args) {
// create the request with the API end point and parameters
HttpRequest request = HttpRequest.newBuilder()
.uri(URI.create("https://api.interzoid.com/getweatherzipcode?zip=94111"))
.header("content-type", "application/json")
.header("x-api-key", "YOURAPIKEY")
.method("GET", HttpRequest.BodyPublishers.noBody())
.build();
// initialize the response
HttpResponse<String> response = null;
// use the client to send the request
try {
response = HttpClient.newHttpClient().send(request, HttpResponse.BodyHandlers.ofString());
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
// print the response to the console
System.out.println(response.body());
}
}