연구노트/안드로이드

안드로이드 OKHTTP

생존형수동개발자 2019. 6. 5. 21:04
OkHttp 사용하기
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
                OkHttpClient client = new OkHttpClient();
                Request request = new Request.Builder()
                        .url(url주소)
                        .build();
                okhttp3.Callback callback = new okhttp3.Callback() {
                    @Override
                    public void onFailure(@NotNull okhttp3.Call call, @NotNull IOException e) {
                        //실패시 콜백
                    }
 
                    @Override
                    public void onResponse(@NotNull okhttp3.Call call, @NotNull okhttp3.Response response) throws IOException {
                        //성공시 콜백
                        //response.body().string() => 결과값 받을때
                    }
                };
                client.newCall(request).enqueue(callback);
            }
        });
cs