英文:
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,
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.
答案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<Promo> jsonData = response.body();
Log.i("TESTER",""+jsonData);
String name = jsonData.get(0).getPromo_name();
Log.i("TESTER",""+name);
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论