-
190820 JSONReaderJAVA 2020. 8. 29. 15:19
JSONReader
package test.lotto; import java.io.BufferedReader; import java.io.InputStreamReader; import java.net.MalformedURLException; import java.net.URL; import javax.net.ssl.HostnameVerifier; import javax.net.ssl.HttpsURLConnection; import javax.net.ssl.SSLSession; import org.json.simple.JSONObject; import org.json.simple.parser.JSONParser; public class JsonReader { public JSONObject connectionURLToJson(String turn) { JSONObject jObj = null; try { URL url = new URL("https://www.dhlottery.co.kr/common.do?method=getLottoNumber&drwNo=" + turn); HttpsURLConnection conn = null; HostnameVerifier hnv = new HostnameVerifier() { @Override public boolean verify(String hostname, SSLSession session) { return true; } }; HttpsURLConnection.setDefaultHostnameVerifier(hnv); conn = (HttpsURLConnection)url.openConnection(); BufferedReader br = new BufferedReader(new InputStreamReader(conn.getInputStream())); String iLine = br.readLine(); JSONParser ps = new JSONParser(); jObj = (JSONObject) ps.parse(iLine); } catch (MalformedURLException e) { System.out.println("접속 실패"); } catch (Exception e) { e.printStackTrace(); } return jObj; } }
'JAVA' 카테고리의 다른 글
191020 JDBC 기본 (0) 2020.10.19 161020 시험 (0) 2020.10.16 190820 lotto (0) 2020.08.29 190820 - get Lotto Num (0) 2020.08.29