package com.example.service;
import java.net.URI;
import java.util.HashMap;
import java.util.Map;
import org.json.JSONArray;
import org.json.JSONObject;
import org.json.XML;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;
@RestController
@RequestMapping(value = "/api")
public class APiService {
// API 갖고오기
// XML api => 127.0.0.1:8080/BOOT1/api/exam5.json
@GetMapping(value = "/apitest")
public Map<String, Object> exam5GET() {
Map<String, Object> map = new HashMap<>();
try {
// 다른 api를 호출해서 결과를 가져 온 다음
// String url 자체 안에서 encoding을 해주기 때문에 decoding 서비스키를 쓰는게 베스트일것 같음.
String url = "http://openapi.airport.co.kr/service/rest/FlightScheduleList/getDflightScheduleList?ServiceKey=PxUQv09spE8N85pwIwySieXRxlNADWhNtWpxFKk02XAc4KGH1PmJt8NnEil8TFkRIMTk2bBhANMVyNNtWr6SVw%3D%3D&schDeptCityCode=GMP&schArrvCityCode=PUS&pageNo=1";
// springboot에서 제공하는 rest 템플릿
RestTemplate restTemplate = new RestTemplate();
URI uri = new URI(url);
// 호출하는 URL, 반환 타입
String response = restTemplate.getForObject(uri, String.class);
if (response != null) {
System.out.println("----------------------------------------");
JSONObject jobj2 = new JSONObject(response.toString());
JSONObject jobj3 = jobj2.getJSONObject("response").getJSONObject("body").getJSONObject("items");
// response 안에 body안에 items 까지 데이터 뺌
System.out.println(jobj3.toString());
System.out.println("------------------Jobj3----------------------");
JSONArray jarr = jobj3.getJSONArray("item");
for(int i=0; i<jarr.length(); i++){
JSONObject obj = jarr.getJSONObject(i);
String ret1 = obj.getString("arrivalcity");
String ret2 = obj.getString("airlineKorean");
String ret3 = obj.getString("airlineEnglish");
map.put("arrivalcity", ret1);
map.put("airlineKorean", ret2);
map.put("airlineEnglish", ret3);
System.out.println(ret1.toString());
}
}
// XML을 JSON Object로 반환하기
// {"response":{"items":{"item":[{"name":"one","id":1},{"name":"two","id":2},{"name":"three","id":3}]}}}
// {"response":{"headers" :["resultCode":00 ,"resultMsg":"NORMAL SERVICE."}],
// {"items":[{"cityChn":"VNUKOVO"}, . . . ]}}
// JSONObject jobc1 =
// jobc.getJSONObject("response").getJSONObject("headers").getJSONObject("items");
// [{"name":"one","id":1},
// {"name":"two","id":2},
// {"name":"three","id":3}]
// for(int i=0; i<jarr.length(); i++){
// JSONObject obj = jarr.getJSONObject(i);
// String ret1 = obj.getString("cityChn");
// String ret2 = obj.getString("cityCode");
// String ret3 = obj.getString("cityEng");
// String ret4 = obj.getString("cityJpn");
// String ret5 = obj.getString("cityKor");
// map.put("cityChn", ret1);
// map.put("cityCode", ret2);
// map.put("cityEng", ret3);
// map.put("cityJpn", ret4);
// map.put("cityKor", ret5);
// System.out.println(ret5.toString());
// }
// System.out.println(jarr.toString());
} catch (Exception e) {
e.printStackTrace();
}
return map;
}
}