如何将改造和 RxJava 与多个 pojo 类一起使用?

我在 android studio 上开发了一个 android 应用程序。我使用java。

我想使用来自 open food facts 的这个 api: https://fr.openfoodfacts.org/api/v0/produit/3029330003533.json

但我只知道如何在一个 pojo 类中使用 retrofit 和 Rxjava。

我使用这个网站来创建 pojo 类:http://pojo.sodhanalibrary.com 但是他创建了大量的 pojo 类,我不知道它是否正确以及我如何使用它?

接下来你可以看到我有很多 POJO 类。

POJO类


小怪兽爱吃肉
浏览 107回答 1
1回答

萧十郎

使用JsonSchema为您正在使用的解析库(GSON/Jackson 等)和 Api 调用用户 RxJava 生成 pojo 并像这样进行改造创建 Pojoimport 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({&nbsp; &nbsp; &nbsp; &nbsp; "code",&nbsp; &nbsp; &nbsp; &nbsp; "msg",&nbsp; &nbsp; &nbsp; &nbsp; "details"})public class LoginResponse {&nbsp; &nbsp; @JsonProperty("code")&nbsp; &nbsp; private int code;&nbsp; &nbsp; @JsonProperty("msg")&nbsp; &nbsp; private String msg;&nbsp; &nbsp; @JsonProperty("details")&nbsp; &nbsp; private List<LoginDetailsWrapper> details = new ArrayList<LoginDetailsWrapper>();&nbsp; &nbsp; @JsonProperty("code")&nbsp; &nbsp; public int getCode() {&nbsp; &nbsp; &nbsp; &nbsp; return code;&nbsp; &nbsp; }&nbsp; &nbsp; @JsonProperty("code")&nbsp; &nbsp; public void setCode(int code) {&nbsp; &nbsp; &nbsp; &nbsp; this.code = code;&nbsp; &nbsp; }&nbsp; &nbsp; @JsonProperty("msg")&nbsp; &nbsp; public String getMsg() {&nbsp; &nbsp; &nbsp; &nbsp; return msg;&nbsp; &nbsp; }&nbsp; &nbsp; @JsonProperty("msg")&nbsp; &nbsp; public void setMsg(String msg) {&nbsp; &nbsp; &nbsp; &nbsp; this.msg = msg;&nbsp; &nbsp; }&nbsp; &nbsp; @JsonProperty("details")&nbsp; &nbsp; public List<LoginDetailsWrapper> getDetails() {&nbsp; &nbsp; &nbsp; &nbsp; return details;&nbsp; &nbsp; }&nbsp; &nbsp; @JsonProperty("details")&nbsp; &nbsp; public void setDetails(List<LoginDetailsWrapper> details) {&nbsp; &nbsp; &nbsp; &nbsp; this.details = details;&nbsp; &nbsp; }}像这样在 ApiInterface 中定义 Api&nbsp;@FormUrlEncoded&nbsp; &nbsp; @POST("login")&nbsp; &nbsp; Observable<LoginResponse> userLogin(@Field("device_id") String device_id, @Field("device_type") String device_type,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; @Field("username") String username, @Field("password") String password&nbsp; &nbsp; );并像这样调用 api&nbsp; &nbsp; @Override&nbsp; &nbsp; public void userLogin(String device_id, String device_type, String username, String password) {&nbsp; &nbsp; &nbsp; &nbsp; getCompositeDisposable().add(loginActivtiyInteractor.userLogin(device_id, device_type, username, password)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread())&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .subscribe(loginResponse -> {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (loginResponse != null) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (loginResponse.getCode() == 1) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; getMvpView().hideLoading();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; getMvpView().updateView(loginResponse);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; getMvpView().hideLoading();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; getMvpView().onError(loginResponse.getMsg());&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }, throwable -> {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; throwable.printStackTrace();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; getMvpView().onError(throwable.getLocalizedMessage());&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; getMvpView().hideLoading();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }));&nbsp; &nbsp; }我希望它有所帮助。
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java