亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何將改造和 RxJava 與多個 pojo 類一起使用?

如何將改造和 RxJava 與多個 pojo 類一起使用?

小怪獸愛吃肉 2023-03-02 10:09:46
我在 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類
查看完整描述

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();

                }));

    }

我希望它有所幫助。


查看完整回答
反對 回復 2023-03-02
  • 1 回答
  • 0 關注
  • 125 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號