okhttp3,gson,retrofit2,rxjavaを使ってデータを取得する


```java:User.java
public class User {
    public final String id;
    public final String email;

    public User(String id, String email) {
        this.id = id;
        this.email = email;
    }
}
```

```java:UserService.java
import java.util.List;

import retrofit2.http.GET;
import retrofit2.http.Path;
import rx.Observable;

public interface UserService {
    @GET("api/users/{id}")
    Observable<User> getUser(@Path("id") int userId);
    @GET("api/users")
    Observable<UserList> getListUser();


    public static class UserList {
        public final List<User> users;

        public UserList(List<User> users){
            this.users = users;
        }
    }
}
```

```java:MainActivity

import okhttp3.OkHttpClient;
import okhttp3.logging.HttpLoggingInterceptor;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;
import retrofit2.adapter.rxjava.RxJavaCallAdapterFactory;
import rx.Observable;
import rx.Subscriber;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

public class MainActivity extends AppCompatActivity {
    private Retrofit retrofit;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        showMember();

    }

    private void showMember(){
        HttpLoggingInterceptor logging = new HttpLoggingInterceptor(new HttpLoggingInterceptor.Logger() {
            @Override
            public void log(String message) {
                Log.d("API LOG", message);
            }
        });

        logging.setLevel(HttpLoggingInterceptor.Level.BASIC);

        OkHttpClient client = new OkHttpClient.Builder().addInterceptor(logging).build();

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

        UserService userService = retrofit.create(UserService.class);
        Observable<UserService.UserList> observable = userService.getListUser();
        observable.subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread()).subscribe(new Subscriber<UserService.UserList>() {
            @Override
            public void onCompleted() {
                Log.d("user--1", "test");
            }

            @Override
            public void onError(Throwable e) {
                Log.d("user2--", e.getMessage());
            }

            @Override
            public void onNext(UserService.UserList users) {
                Log.d("user3--", users.users.get(0).id);
                Log.d("user4--", users.users.get(0).email);
            }
        });
    }
```

参照しているデータ

```
{
	"users":[
		{
			"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"}
		]
}
```