1 回答
TA贡献1815条经验 获得超13个赞
使用JsonSchema为您正在使用的解析库(GSON/Jackson 等)和 Api 调用用户 RxJava 生成 pojo 并像这样进行改造
创建 Pojo
import java.util.ArrayList;
import java.util.List;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.JsonPropertyOrder;
import com.foodit.data.remote.wrapper.SignupDetailsWrapper;
@JsonInclude(JsonInclude.Include.NON_NULL)
@JsonPropertyOrder({
"code",
"msg",
"details"
})
public class LoginResponse {
@JsonProperty("code")
private int code;
@JsonProperty("msg")
private String msg;
@JsonProperty("details")
private List<LoginDetailsWrapper> details = new ArrayList<LoginDetailsWrapper>();
@JsonProperty("code")
public int getCode() {
return code;
}
@JsonProperty("code")
public void setCode(int code) {
this.code = code;
}
@JsonProperty("msg")
public String getMsg() {
return msg;
}
@JsonProperty("msg")
public void setMsg(String msg) {
this.msg = msg;
}
@JsonProperty("details")
public List<LoginDetailsWrapper> getDetails() {
return details;
}
@JsonProperty("details")
public void setDetails(List<LoginDetailsWrapper> details) {
this.details = details;
}
}
像这样在 ApiInterface 中定义 Api
@FormUrlEncoded
@POST("login")
Observable<LoginResponse> userLogin(@Field("device_id") String device_id, @Field("device_type") String device_type,
@Field("username") String username, @Field("password") String password
);
并像这样调用 api
@Override
public void userLogin(String device_id, String device_type, String username, String password) {
getCompositeDisposable().add(loginActivtiyInteractor.userLogin(device_id, device_type, username, password)
.subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread())
.subscribe(loginResponse -> {
if (loginResponse != null) {
if (loginResponse.getCode() == 1) {
getMvpView().hideLoading();
getMvpView().updateView(loginResponse);
} else {
getMvpView().hideLoading();
getMvpView().onError(loginResponse.getMsg());
}
}
}, throwable -> {
throwable.printStackTrace();
getMvpView().onError(throwable.getLocalizedMessage());
getMvpView().hideLoading();
}));
}
我希望它有所帮助。
添加回答
举报