LoginSignup
14
10

More than 5 years have passed since last update.

retrofit2のサンプルにokhttp3を追加します。

Last updated at Posted at 2017-01-16

okhttp3は通信用のパッケージです。
Android標準の通信パッケージが貧弱なので、みんなこれを使う用です。

logging用のパッケージを追加します。

build.gradle
compile 'com.squareup.okhttp3:okhttp:3.4.1'
compile 'com.squareup.okhttp3:logging-interceptor:3.4.1'

ヘッダーの追加

MainActivity.java
OkHttpClient.Builder httpClient = new OkHttpClient.Builder();
httpClient.addInterceptor(new Interceptor() {
    @Override
    public okhttp3.Response intercept(Chain chain) throws IOException {
        Request original = chain.request();

        //header設定
        Request request = original.newBuilder()
                .header("Accept", "application/json")
                .method(original.method(), original.body())
                .build();

        okhttp3.Response response = chain.proceed(request);

        return response;
    }
});

ログ出力追加

MainActivity.java
HttpLoggingInterceptor logging = new HttpLoggingInterceptor();
logging.setLevel(HttpLoggingInterceptor.Level.BASIC);
httpClient.addInterceptor(logging);
OkHttpClient client = httpClient.build();

これを追加しました。

MainActivity.java
.client(client)

こんな感じになります。

MainActivity.java
Retrofit retrofit = new Retrofit.Builder()
        .baseUrl("http://192.168.43.134:3000/")
        .addConverterFactory(GsonConverterFactory.create())
        .client(client)
        .build();

全体としては、こうなっています。

MainActivity.java
private void showMember(){
    OkHttpClient.Builder httpClient = new OkHttpClient.Builder();
    httpClient.addInterceptor(new Interceptor() {
        @Override
        public okhttp3.Response intercept(Chain chain) throws IOException {
            Request original = chain.request();

            //header設定
            Request request = original.newBuilder()
                    .header("Accept", "application/json")
                    .method(original.method(), original.body())
                    .build();

            okhttp3.Response response = chain.proceed(request);

            return response;
        }
    });

    //ログ出力設定
    HttpLoggingInterceptor logging = new HttpLoggingInterceptor();
    logging.setLevel(HttpLoggingInterceptor.Level.BASIC);
    httpClient.addInterceptor(logging);
    OkHttpClient client = httpClient.build();

    Retrofit retrofit = new Retrofit.Builder()
            .baseUrl("http://192.168.43.134:3000/")
            .addConverterFactory(GsonConverterFactory.create())
            .client(client)
            .build();
    ApiService service = retrofit.create(ApiService.class);
    Call<User> call = service.getUser(1);
    call.enqueue(new Callback<User>() {
        @Override
        public void onResponse(Call<User> call, Response<User> response) {
            Log.d("debug1", response.body().getEmail());
        }

        @Override
        public void onFailure(Call<User> call, Throwable t) {
            Log.d("debug2", t.getMessage());
        }
    });


    Call<ListUser> call2 = service.listUsers();
    call2.enqueue(new Callback<ListUser>() {
        @Override
        public void onResponse(Call<ListUser> call, Response<ListUser> response) {
            List<User> listUser = response.body().getListUsers();
            int s = listUser.size();
            for(int i = 0; i < s; i++){
                Log.d("debug3", listUser.get(i).getEmail());
            }
        }

        @Override
        public void onFailure(Call<ListUser> call, Throwable t) {
            Log.d("debug4", t.getMessage());
        }
    });
}
build.gradle
dependencies {
    compile 'com.android.support:appcompat-v7:25.1.0'
    compile 'com.android.support:support-v4:25.1.0'
    compile 'com.android.support:design:25.1.0'
    compile 'com.google.code.gson:gson:2.7'
    compile 'com.squareup.okhttp3:okhttp:3.4.1'
    compile 'com.squareup.okhttp3:logging-interceptor:3.4.1'
    compile 'com.squareup.retrofit2:retrofit:2.1.0'
    compile 'com.squareup.retrofit2:converter-gson:2.1.0'
    testCompile 'junit:junit:4.12'
}
14
10
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
14
10