在使用Retrofit时出错,以分析数组列表中有另一个pojo类的pojo类

时间:2018-07-26 11:27:00

标签: android retrofit

我已经创建了两个模型类,并且在获取响应的同时得到0大小的ArrayList

package com.jdb.shopname.Model;
import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;

import java.util.List;

public class Registration {


@SerializedName("success")
@Expose
private String success;


@SerializedName("data")
@Expose
private List<Datum> data = null;


@SerializedName("msg")
@Expose
private String msg;

public String getSuccess() {
    return success;
}

public void setSuccess(String success) {
    this.success = success;
}

public List<Datum> getData() {
    return data;
}

public void setData(List<Datum> data) {
    this.data = data;
}

public String getMsg() {
    return msg;
}

public void setMsg(String msg) {
    this.msg = msg;
}

}

另一堂课:

public class DataProduct {
@SerializedName("ID")
@Expose
private String iD;
@SerializedName("post_title")
@Expose
private String postTitle;
@SerializedName("post_type")
@Expose
private String postType;
@SerializedName("post_content")
@Expose
private String postContent;
@SerializedName("post_status")
@Expose
private String postStatus;
@SerializedName("post_name")
@Expose
private String postName;
@SerializedName("image")
@Expose
private String image;
@SerializedName("average_rating")
@Expose
private String averageRating;
@SerializedName("sku")
@Expose
private String sku;
@SerializedName("regular_price")
@Expose
private String regularPrice;
@SerializedName("sale_price")
@Expose
private String salePrice;
@SerializedName("total_sales")
@Expose
private String totalSales;
@SerializedName("stock_status")
@Expose
private String stockStatus;
@SerializedName("price")
@Expose
private String price;

public String getID() {
    return iD;
}

public void setID(String iD) {
    this.iD = iD;
}

public String getPostTitle() {
    return postTitle;
}

public void setPostTitle(String postTitle) {
    this.postTitle = postTitle;
}

public String getPostType() {
    return postType;
}

public void setPostType(String postType) {
    this.postType = postType;
}

public String getPostContent() {
    return postContent;
}

public void setPostContent(String postContent) {
    this.postContent = postContent;
}

public String getPostStatus() {
    return postStatus;
}

public void setPostStatus(String postStatus) {
    this.postStatus = postStatus;
}

public String getPostName() {
    return postName;
}

public void setPostName(String postName) {
    this.postName = postName;
}

public String getImage() {
    return image;
}

public void setImage(String image) {
    this.image = image;
}

public String getAverageRating() {
    return averageRating;
}

public void setAverageRating(String averageRating) {
    this.averageRating = averageRating;
}

public String getSku() {
    return sku;
}

public void setSku(String sku) {
    this.sku = sku;
}

public String getRegularPrice() {
    return regularPrice;
}

public void setRegularPrice(String regularPrice) {
    this.regularPrice = regularPrice;
}

public String getSalePrice() {
    return salePrice;
}

public void setSalePrice(String salePrice) {
    this.salePrice = salePrice;
}

public String getTotalSales() {
    return totalSales;
}

public void setTotalSales(String totalSales) {
    this.totalSales = totalSales;
}

public String getStockStatus() {
    return stockStatus;
}

public void setStockStatus(String stockStatus) {
    this.stockStatus = stockStatus;
}

public String getPrice() {
    return price;
}

public void setPrice(String price) {
    this.price = price;
}

}

我叫改造的Main Class是:

 public void setData(){
    Retrofit retrofit = new Retrofit.Builder()
            .addConverterFactory(GsonConverterFactory.create())
            .baseUrl(API_URL)
            .build();

    Apiinterface apiinterface = retrofit.create(Apiinterface.class);
    Call<MultipleProductList> call = apiinterface.sendMultipleProducts();
    call.enqueue(new Callback<MultipleProductList>() {
        @Override
        public void onResponse(Call<MultipleProductList> call, Response<MultipleProductList> response) {
            response.body();

            String success = response.body().getSuccess();
            String message = response.body().getMsg();
            Log.e("success", success);
            Log.e("msg", message);

            if (response.isSuccessful()) {
                String responseData = response.body().toString();
                Log.e("here", "onResponse: " + responseData);

             MultipleProductList   multipleProductList = new MultipleProductList();
                multipleProductList.getData();

                List<DataProduct> arrayList = new ArrayList<>();
                arrayList = multipleProductList.getData();





                arrayList.size();

                String id = arrayList.get(0).getID();
                String regularPrice = arrayList.get(0).getRegularPrice();
                String sale_price = arrayList.get(0).getSalePrice();


                Log.e("regularPrice", regularPrice);
                Log.e("sale_price", sale_price);

            } else {
                Log.i("debug", "onResponse: GA BERHASIL");
            }


        }

        @Override
        public void onFailure(Call<MultipleProductList> call, Throwable t) {

            if (t instanceof IOException) {
                Toast.makeText(MainActivity.this, "this is an actual network failure :( inform the user and possibly retry", Toast.LENGTH_SHORT).show();
                // logging probably not necessary
            } else {
                Toast.makeText(MainActivity.this, "conversion issue! big problems :(", Toast.LENGTH_SHORT).show();
                //here                  // todo log to some central bug tracking service
            }

        }
    });


}

1 个答案:

答案 0 :(得分:1)

response.body使用此返回您的MultipleProductList类型的数据。

if (response.isSuccessful()) {
                String responseData = response.body().toString();
                Log.e("here", "onResponse: " + responseData);

             MultipleProductList   multipleProductList = response.body;
                multipleProductList.getData();

                List<DataProduct> arrayList = new ArrayList<>();
                arrayList = multipleProductList.getData();





                arrayList.size();

                String id = arrayList.get(0).getID();
                String regularPrice = arrayList.get(0).getRegularPrice();
                String sale_price = arrayList.get(0).getSalePrice();


                Log.e("regularPrice", regularPrice);
                Log.e("sale_price", sale_price);

            } else {
                Log.i("debug", "onResponse: GA BERHASIL");
            }