0

I devellop an android application on android studio. I use java.

I want to use this api from open food facts : https://fr.openfoodfacts.org/api/v0/produit/3029330003533.json

But I only know how to use retrofit and Rxjava with only one pojo class.

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 ?

Next you can see that i have loads of POJO class.

POJO class

Arno
  • 91
  • 2
  • 11
  • Please see this link https://stackoverflow.com/questions/47707564/how-to-use-robopojogenerator-to-automatically-generate-model-class-from-json-in – Sabbir Ahmed Jul 02 '19 at 10:06
  • In this case, a lot of POJO classes will be created since you have many types in your JSON response. YOu have to use you base POJO class which refers other classes as your Rxjava and retorfit class to get response – p.mathew13 Jul 02 '19 at 10:20
  • @p.mathew13 I don't know how to do this with the pojo class i have ? have you samples ? I update my question with a screenshot of the POJO class created. – Arno Jul 02 '19 at 10:45
  • One of the POJO classes that you created would be the Main Pojo class that you created when using the website to convert your Json to POJO. Use that main class as your Data type in your Retrofit and Rxjava – p.mathew13 Jul 02 '19 at 10:54

1 Answers1

2

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

Create 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

 @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


    @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.

Nitin
  • 1,280
  • 1
  • 13
  • 17
  • It was very clear but my problem is that the Api I use create me more than 15 POJO class. And i don't know how to use retrofit with lot of POJO class. You can see a screenshots of the pojo class in my question now. – Arno Jul 02 '19 at 10:42
  • @Arno You can assign pojo dynamically like this https://stackoverflow.com/a/46320656/8383248 – Nitin Jul 02 '19 at 10:45
  • @Arno Cool, If it works then please accept my answer – Nitin Jul 02 '19 at 13:02