10
13

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

retrofit2のサンプル

Last updated at Posted at 2017-01-15

retrofitというのはREST URLのモデリングをするパッケージというイメージです。

対象のデータ1

http://127.0.0.1:3000/api/users/1
{"id":1,
 "email":"test1@gmail.com",
 "teacher_limit":30,
 "teacher_count":5, 
 "created_at":"2017-01-15T01:43:38.270+09:00",
 "updated_at":"2017-01-15T01:43:38.270+09:00"
}

対象のデータ2

http://127.0.0.1:3000/api/users
{"user":[
  {"id":1,
   "email":"test1@gmail.com",
   "teacher_limit":30,"teacher_count":5,
   "created_at":"2017-01-15T01:43:38.270+09:00",
   "updated_at":"2017-01-15T01:43:38.270+09:00"},
  {"id":2,"email":"test2@yahoo.com",
   "teacher_limit":40,"teacher_count":7,
   "created_at":"2017-01-15T01:44:09.086+09:00",
   "updated_at":"2017-01-15T01:44:09.086+09:00"}
  ]
}

データ1用のクラス

User.java
public class User {
    public String id;
    public String email;
    public Integer teacher_limit;
    public Integer teacher_count;

    public Integer getTeacher_count() {
        return teacher_count;
    }

    public String getId() {
        return id;
    }


    public String getEmail() {
        return email;
    }

    public Integer getTeacher_limit() {
        return teacher_limit;
    }
}

データ2用のクラス
Jsonのkey名"user"と"public List user"を合わせる必要がある。

ListUser.java
public class ListUser {
    public List<User> user;

    public List<User> getListUsers() {
        return user;
    }
}
ApiService.java
import retrofit2.Call;
import retrofit2.http.GET;
import retrofit2.http.Path;

public interface ApiService {
    @GET("api/users/{id}")
    Call<User> getUser(@Path("id") int userId);

    @GET("api/users")
    Call<ListUser> listUsers();
}

対象のサーバーはlocalで、PCのブラウザから見る時はこういうURLに

http://127.0.0.1:3000/api/users/1

ただ、Androidから見たPCのIPは違うので、ifconfigなので確認が必要。
なので、baseUrlには、このIPを設定しました。注意が必要。

http://192.168.43.134:3000/
MainActivity

import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

    private void showMember(){
        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl("http://192.168.43.134:3000/")
                .addConverterFactory(GsonConverterFactory.create())
                .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.retrofit2:retrofit:2.1.0'
    compile 'com.squareup.retrofit2:converter-gson:2.1.0'
    testCompile 'junit:junit:4.12'
}
10
13
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
10
13

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?