반응형
[JAVA(자바) 실습 #3] http통신, REST API 호출, json 데이터 처리 - StringBuilder, BufferedReader 활용
BufferedReader, StringBuilder 활용
1
2
3
4
5
6
7
8
9
10
|
private static String getStringFromJson(BufferedReader br) throws IOException {
StringBuilder sb = new StringBuilder();
String read;
while ((read = br.readLine()) != null) {
sb.append(read);
}
return sb.toString();
}
|
cs |
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
|
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.nio.file.Paths;
public class TestJsonString {
private static HttpURLConnection testHttpUrlConnection() throws MalformedURLException, IOException {
long startTime = System.currentTimeMillis();
URL url = new URL("http://dummy.restapiexample.com/api/v1/employees");
HttpURLConnection httpConn = (HttpURLConnection) url.openConnection();
long endTime = System.currentTimeMillis();
int statusCode = httpConn.getResponseCode();
String pf = (statusCode == 200) ? "success" : "fail";
System.out.println("1. connection " + pf + " in " + (endTime - startTime) + " millisecond");
System.out.println("2. Response code: " + statusCode);
return httpConn;
}
private static int getResponseStatus() throws MalformedURLException, IOException {
URL url = new URL("http://dummy.restapiexample.com/api/v1/employees");
HttpURLConnection httpConn = (HttpURLConnection) url.openConnection();
int responseCode = httpConn.getResponseCode();
return responseCode;
}
private static String getJsonObjectStringFromFile() throws MalformedURLException, IOException {
System.out.println("\nRead from json file instead!!");
String currentPath = Paths.get(".").normalize().toString();
FileReader fr = new FileReader(currentPath + ".\\employees.json");
BufferedReader br = new BufferedReader(fr);
String outputString = getStringFromJson(br);
return outputString;
}
// Print Json using StringBuilder
private static String getStringFromJson(BufferedReader br) throws IOException {
StringBuilder sb = new StringBuilder();
String read;
while ((read = br.readLine()) != null) {
sb.append(read);
}
return sb.toString();
}
public static void main(String[] args) throws MalformedURLException, IOException {
HttpURLConnection httpConn = testHttpUrlConnection();
int statusCode = getResponseStatus();
String res = null;
if (statusCode == 200) {
BufferedReader br = new BufferedReader(new InputStreamReader(httpConn.getInputStream()));
res = getStringFromJson(br);
} else {
res = getJsonObjectStringFromFile();
}
System.out.println("result string: " + res);
}
}
|
cs |
반응형
'JAVA 자바 > JAVA 실습' 카테고리의 다른 글
[JAVA 실습 #5] TCP 전문 통신 (fixed length) - 전문 헤더 읽기 readInt, writeInt 활용 (1) (0) | 2021.02.07 |
---|---|
[JAVA 실습 #4] WAR파일 실행시 IPv4 사용 설정 (0) | 2020.12.20 |
[JAVA 실습 #3] http통신, REST API 호출, json 데이터 처리 (0) | 2020.11.30 |
[JAVA 실습 #2] TCP Echo Server/Client - 전문 통신 (fixed length) - 전문 길이(byte) 읽기 (0) | 2020.10.25 |
[JAVA 실습 #1] 카드번호 마스킹 예시 - StringBuilder, charArray, substring 활용 (0) | 2020.10.17 |