프로그래밍/Java
HttpURLConnection을 이용하여 타 시스템 JSON 결과 값 받아오기
커피코더
2014. 6. 8. 02:04
반응형
HttpURLConnection을 이용하여 Java 상에서 타 시스템의 JSON 결과 값을 받아 올 수 있다.
String json = "";
URL postUrl = new URL('URL 경로');
HttpURLConnection connection = (HttpURLConnection) postUrl.openConnection();
connection.setDoOutput(true); // xml내용을 전달하기 위해서 출력 스트림을 사용
connection.setInstanceFollowRedirects(false); //Redirect처리 하지 않음
connection.setRequestMethod("POST");
connection.setRequestProperty("Content-Type", "application/json");
BufferedReader br = new BufferedReader(new InputStreamReader((connection.getInputStream())));
String output;
while ((output = br.readLine()) != null) {
json += output;
}
connection.disconnect();
//String to JsonNode
ObjectMapper objectMapper = new ObjectMapper();
JsonNode root = objectMapper.readTree(json);
반응형