RxJava登录并获取用户信息

时间:2018-09-26 03:21:12

标签: android rx-java retrofit

我有2个函数loginUsergetUserInfomation使用RxJava2和Retrofit2。登录并获取用户ID后,如何从userId和setText用户信息中获取textView的UserUserInformation?

谢谢。这是我的代码:

TextView textView;

Maybe<User> loginUser(String username, String password) {
    return RetrofitClient.getClient().create(RetrofitInterface.class)
            .loginUserRx("http://10.0.2.2:3000/users", username, password);
}

Maybe<UserInfo> getUserInformation(String id) {
    return RetrofitClient.getClient().create(RetrofitInterface.class)
            .getUserInfoById("http://10.0.2.2:3000/userinformation", id);
}

loginUser(username, password)
  .subscribeOn(Schedulers.io())
  .observeOn(AndroidSchedulers.mainThread())
  ...........

User.java

public class User {
@SerializedName("id")
@Expose
private String id;
@SerializedName("username")
@Expose
private String username;
@SerializedName("password")
@Expose
private String password;

public String getId() {
    return id;
}

public void setId(String id) {
    this.id = id;
}

public String getUsername() {
    return username;
}

public void setUsername(String username) {
    this.username = username;
}

public String getPassword() {
    return password;
}

public void setPassword(String password) {
    this.password = password;
}
}

UserInfo.java

public class UserInfo {
@SerializedName("id")
@Expose
private String id;
@SerializedName("name")
@Expose
private String name;
@SerializedName("address")
@Expose
private String address;

public String getId() {
    return id;
}

public void setId(String id) {
    this.id = id;
}

public String getName() {
    return name;
}

public void setName(String name) {
    this.name = name;
}

public String getAddress() {
    return address;
}

public void setAddress(String address) {
    this.address = address;
}
}

1 个答案:

答案 0 :(得分:1)

那样做

loginUser(username, password)
            .flatMap(user -> getUserInformation(user.getId()))
            .subscribeOn(Schedulers.io())
            .observeOn(AndroidSchedulers.mainThread())
            .subscribe(userInfo ->  { /* TODO set info in textView */ }, error -> { /* TODO handle error */  })