๋ฐ์ํ
JsonObject๋ฅผ ๋๊ฒจ ๊ฒฐ๊ณผ๊ฐ์ ์๋ต๋ฐ๋ REST API ํธ์ถ ์๋ฐ ์ฝ๋์ ๋๋ค.
์๋์ ๊ฐ์ด ๋ฉ์๋๋ก ํ๋ ์ ๋ฆฌํด ๋๋ฉด ์ฌ์ฉ์ ์ฉ์ดํฉ๋๋ค.
/**
* REST API ํธ์ถ
*
* @param paramUrl
* @param jsonObject void
*/
private void RestCall(String paramUrl,JSONObject jsonObject){
try {
URL url = new URL(paramUrl);
HttpURLConnection conn = (HttpURLConnection)url.openConnection();
conn.setRequestMethod("POST");
conn.setRequestProperty("X-Auth-Token", API_KEY);
conn.setRequestProperty("X-Data-Type", "application/json");
conn.setRequestProperty("Content-Type", "application/json");
conn.setDoOutput(true);
OutputStreamWriter osw = new OutputStreamWriter(conn.getOutputStream(),"UTF-8");
osw.write(jsonObject.toString());
osw.flush();
osw.close();
BufferedReader br = new BufferedReader(new InputStreamReader(conn.getInputStream(),"UTF-8"));
if (conn.getResponseCode() != 200) {
System.out.println("Failed: HTTP error code : " + conn.getResponseCode());
throw new RuntimeException("Failed: HTTP error code : " + conn.getResponseCode());
} else {
System.out.println("๋ฐ์ก ์ฑ๊ณต");
}
String line = null;
while((line = br.readLine()) != null){
System.out.println(line);
}
br.close();
conn.disconnect();
} catch (IOException e) {
System.out.println("RestCall Fail : " + e.getMessage());
}
}