[英]How can I use retrofit and RxJava with multiple pojo class?
I devellop an android application on android studio. 我在android studio上开发了一个android应用程序。 I use java. 我使用Java。
I want to use this api from open food facts : https://fr.openfoodfacts.org/api/v0/produit/3029330003533.json 我想从开放食物事实中使用此api: https : //fr.openfoodfacts.org/api/v0/produit/3029330003533.json
But I only know how to use retrofit and Rxjava with only one pojo class. 但是我只知道如何仅通过一个pojo类使用Retrofit和Rxjava。
I use this website to create pojo classe : http://pojo.sodhanalibrary.com But he creates loads of pojo class and I don't know if it's correct and how i can use it ? 我使用此网站创建pojo类: http : //pojo.sodhanalibrary.com但是他创建了很多pojo类,但我不知道它是否正确以及如何使用?
Next you can see that i have loads of POJO class. 接下来,您可以看到我有很多POJO类。
Use JsonSchema for generating pojo for the parsing library you are using(GSON/Jackson etc) and for Api calling user RxJava and retrofit like this 使用JsonSchema为您正在使用的解析库(GSON / Jackson等)生成pojo并为Api调用用户RxJava进行此类改造
Create 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;
}
}
Define Api in ApiInterface like this 像这样在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
);
and Call api like this 并像这样调用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();
}));
}
I hope it helps. 希望对您有所帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.