반응형

[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

 

 

 

반응형