简体   繁体   中英

Fetch Data Retrofit/OkHttp With Response List

I fetch some data from API apiServices.getPromo , you can see the url looks like below with the JSON looks like this,

在此处输入图像描述

MainActivity.java

apiServices.getPromo(intentData.getStringExtra("event_id"),dataBooking.getString("total_buyer"),prefManager.getIdUser(),intentData.getStringExtra("ticket_id")).enqueue(new Callback<List<Promo>>() {
                @Override
                public void onResponse(@NonNull Call<List<Promo>> call, @NonNull Response<List<Promo>> response) {
                    if (response.isSuccessful()){
                        if (!response.body().isEmpty()){
                            List<Promo> jsonData = response.body();
                            Log.i("TESTER",""+jsonData);
                        }else{
                            Toast.makeText(PaymentActivity.this, "Maaf, belum ada promo yang tersedia.", Toast.LENGTH_SHORT).show();
                        }

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

with POJO 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;
    }

The Log show like the picture below, my question is how can I extract those data and use the object inside the array? Thanks. 在此处输入图像描述

Nevermind, this is the trick I used

List<Promo> jsonData = response.body();
Log.i("TESTER",""+jsonData);
String name = jsonData.get(0).getPromo_name();
Log.i("TESTER",""+name);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM