获取数据 Retrofit/OkHttp 与响应列表

huangapple go评论84阅读模式
英文:

Fetch Data Retrofit/OkHttp With Response List

问题

我从API apiServices.getPromo获取了一些数据,你可以看到URL如下,JSON如下所示:

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

带有以下结构的POJO:

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

日志显示如下图所示,我的问题是如何提取这些数据并使用数组中的对象?谢谢。

英文:

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

获取数据 Retrofit/OkHttp 与响应列表

MainActivity.java

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

ApiUrl.java

@FormUrlEncoded
    @POST(&quot;promopublic&quot;)
    Call&lt;List&lt;Promo&gt;&gt; getPromo(
            @Field(&quot;id_event&quot;) String id_event,
            @Field(&quot;total_buyer&quot;) String totalBuyer,
            @Field(&quot;id_user&quot;) String id_user,
            @Field(&quot;id_ticket&quot;) String id_ticket);

with POJO that looks like this,

Promo.java

public class Promo  {
    @SerializedName(&quot;id_promo&quot;)
    private String id_promo;
    @SerializedName(&quot;promo_name&quot;)
    private String promo_name;
    @SerializedName(&quot;promo_type&quot;)
    private String promo_type;
    @SerializedName(&quot;value_based&quot;)
    private String value_based;
    @SerializedName(&quot;quota&quot;)
    private String quota;
    @SerializedName(&quot;id_event&quot;)
    private String id_event;
    @SerializedName(&quot;description&quot;)
    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.
获取数据 Retrofit/OkHttp 与响应列表

答案1

得分: 1

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

Nevermind, this is the trick I used

List&lt;Promo&gt; jsonData = response.body();
Log.i(&quot;TESTER&quot;,&quot;&quot;+jsonData);
String name = jsonData.get(0).getPromo_name();
Log.i(&quot;TESTER&quot;,&quot;&quot;+name);

huangapple
  • 本文由 发表于 2020年6月29日 12:06:46
  • 转载请务必保留本文链接:https://go.coder-hub.com/62631052.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定