Qiita Teams that are logged in
You are not logged in to any team

Log in to Qiita Team
Community
OrganizationAdvent CalendarQiitadon (β)
Service
Qiita JobsQiita ZineQiita Blog
3
Help us understand the problem. What is going on with this article?
@joji

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

More than 3 years have passed since last update.
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;
    }
}
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;
        }
    }
}
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"}
        ]
}
3
Help us understand the problem. What is going on with this article?
Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
joji
I worked in Philippines and work in Tokyo.

Comments

No comments
Sign up for free and join this conversation.
Sign Up
If you already have a Qiita account Login
3
Help us understand the problem. What is going on with this article?