forked from bitcocom/JavaTPCProject
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProject01_D.java
More file actions
61 lines (51 loc) · 2.07 KB
/
Project01_D.java
File metadata and controls
61 lines (51 loc) · 2.07 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
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.*;
import org.json.JSONArray;
import org.json.JSONObject;
import org.json.JSONTokener;
public class Project01_D {
public static void main(String[] args) {
//String apiURL="https://naveropenapi.apigw.ntruss.com/map-geocode/v2/geocode?query=";
String client_id = "4ibvf7a7s4";
String client_secret = "pcfd4vM5IVLhIpr1dgfWZyqVDIQZoCD6o3tCkQwx";
BufferedReader io=new BufferedReader(new InputStreamReader(System.in));
try {
System.out.print("주소를 입력하세요:");
String address=io.readLine();
String addr=URLEncoder.encode(address, "UTF-8");
String reqUrl="https://naveropenapi.apigw.ntruss.com/map-geocode/v2/geocode?query="+addr;
URL url=new URL(reqUrl);
HttpURLConnection con=(HttpURLConnection) url.openConnection();
con.setRequestMethod("GET");
con.setRequestProperty("X-NCP-APIGW-API-KEY-ID", client_id);
con.setRequestProperty("X-NCP-APIGW-API-KEY", client_secret);
BufferedReader br;
int responseCode=con.getResponseCode(); // 200
if(responseCode==200) {
br=new BufferedReader(new InputStreamReader(con.getInputStream(), "UTF-8"));
}else {
br=new BufferedReader(new InputStreamReader(con.getErrorStream()));
}
String line;
StringBuffer response=new StringBuffer(); // JSON
while((line=br.readLine())!=null) {
response.append(line);
}
br.close();
JSONTokener tokener=new JSONTokener(response.toString());
JSONObject object=new JSONObject(tokener);
System.out.println(object.toString());
JSONArray arr=object.getJSONArray("addresses");
for(int i=0;i<arr.length();i++) {
JSONObject temp=(JSONObject) arr.get(i);
System.out.println("address:" + temp.get("roadAddress"));
System.out.println("jibunAddress:" + temp.get("jibunAddress"));
System.out.println("경도:" + temp.get("x"));
System.out.println("위도:" + temp.get("y"));
}
} catch (Exception e) {
e.printStackTrace();
}
}
}