Skip to content
Advertisement

List All Data Using Retrofit/OkHttp With Response List

I have some codes that fetch some data from my API. My question is how can I list all the objects that I fetched before without using this jsonData.get(0), I expect something like jsonData.get(i), so I assume using something like below, but I can’t use it, so how can I do that? Thanks.

for (int i=0;i<jsonData.length();i++){

MainActivity.java

List<Promo> jsonData = response.body();
Log.i("TESTER",""+jsonData);
String promoID = jsonData.get(0).getId_promo();
String promoTipe = jsonData.get(0).getPromo_type();
String promoValue = jsonData.get(0).getValue_based();
String promoName = jsonData.get(0).getPromo_name();

With POJO class that looks like this

Promo.java

public class Promo  {
    @SerializedName("id_promo")
    private String id_promo;
    @SerializedName("promo_name")
    private String promo_name;
    @SerializedName("promo_type")
    private String promo_type;
    @SerializedName("value_based")
    private String value_based;
    @SerializedName("quota")
    private String quota;
    @SerializedName("id_event")
    private String id_event;
    @SerializedName("description")
    private String description;

    public String getId_promo() {
        return id_promo;
    }

    public void setId_promo(String id_promo) {
        this.id_promo = id_promo;
    }

    public String getPromo_name() {
        return promo_name;
    }
}

ApiUrl.java

@FormUrlEncoded
@POST("promopublic")
Call<List<Promo>> getPromo(
        @Field("id_event") String id_event,
        @Field("total_buyer") String totalBuyer,
        @Field("id_user") String id_user,
        @Field("id_ticket") String id_ticket);

Advertisement

Answer

Using for loop like below solved my problem

for (int i=0;i<jsonData.size();i++){}
User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement