2017년 1월 17일 화요일

[Android] okHttp 사용하기

1. okHttp 사용하기


http://square.github.io/okhttp/

1-1. Get 방식.


OkHttpClient client = new OkHttpClient();
String run(String url) throws IOException {
  Request request = new Request.Builder()
      .url(url)
      .build();
  Response response = client.newCall(request).execute();
  return response.body().string();
}

1-2. Post 방식.

public static final MediaType JSON
    = MediaType.parse("application/json; charset=utf-8");

OkHttpClient client = new OkHttpClient();
String post(String url, String json) throws IOException {
  RequestBody body = RequestBody.create(JSON, json);
  Request request = new Request.Builder()
      .url(url)
      .post(body)
      .build();
  Response response = client.newCall(request).execute();
  return response.body().string();
}



2. okHttp https 설정하기


http://stackoverflow.com/questions/33469218/okhttp-request-https-is-very-slow-on-android