无法在rxretrofit中访问OnNext()内的POJO的getter方法

时间:2017-03-08 16:25:14

标签: android retrofit rx-java retrofit2

我正在尝试访问响应中收到的pojo的getter属性,但是没有这样的选项。还有其他任何方式,我能做到吗?

该电话的代码如下:

results.subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Subscriber<GenericResponse<List<FoodTruck>>>() {
                    @Override
                    public void onCompleted() {
                        Toast.makeText(getActivity(), "completed", Toast.LENGTH_SHORT).show();
                        unsubscribe();
                    }

                    @Override
                    public void onError(Throwable e) {
                        Toast.makeText(getActivity(), "error", Toast.LENGTH_SHORT).show();
                    }

                    @Override
                    public void onNext(GenericResponse<List<FoodTruck>> response) {
                        Toast.makeText(getActivity(), response.toString(), Toast.LENGTH_SHORT).show();
                    }
                });

GenericResponse.java

public class GenericResponse<T> {

    @JsonProperty("status")
    private String status;
    @JsonProperty("message")
    private String message;
    @JsonProperty("data")
    private T data;

}

FoodTruck.java

@JsonInclude(JsonInclude.Include.NON_NULL)
public class FoodTruck implements Parcelable {
    @JsonProperty("_id")
    private String foodTruckId;
    @JsonProperty("foodtruck_name")
    private String foodTruckName;
    @JsonProperty("foodtruck_location")
    private String foodTruckLocation;
    @JsonProperty("foodtruck_tag")
    private String foodTruckTag;
    @JsonProperty("foodtruck_timing")
    private String foodTruckTiming;
    @JsonProperty("foodtruck_cusine")
    private String foodTruckCusine;
    @JsonProperty("foodtruck_img")
    private String foodTruckImg;
    @JsonProperty("foodtruck_logo")
    private String foodTruckLogo;
    @JsonProperty("foodtruck_total_votes")
    private int foodTruckTotalVotes;
    @JsonProperty("foodtruck_rating")
    private double foodTruckRating;
    @JsonProperty("item_list")
    private List<FoodTruckItem> foodTruckItemList;

    public String getFoodTruckId() {
        return foodTruckId;
    }

    public String getFoodTruckName() {
        return foodTruckName;
    }

    public String getFoodTruckLocation() {
        return foodTruckLocation;
    }

    public String getFoodTruckTag() {
        return foodTruckTag;
    }

    public String getFoodTruckTiming() {
        return foodTruckTiming;
    }

    public String getFoodTruckCusine() {
        return foodTruckCusine;
    }

    public String getFoodTruckImg() {
        return foodTruckImg;
    }

    public String getFoodTruckLogo() {
        return foodTruckLogo;
    }

    public int getFoodTruckTotalVotes() {
        return foodTruckTotalVotes;
    }

    public double getFoodTruckRating() {
        return foodTruckRating;
    }

    public List<FoodTruckItem> getFoodTruckItemList() {
        return foodTruckItemList;
    }

    public void setFoodTruckId(String foodTruckId) {
        this.foodTruckId = foodTruckId;
    }

    public void setFoodTruckName(String foodTruckName) {
        this.foodTruckName = foodTruckName;
    }

    public void setFoodTruckLocation(String foodTruckLocation) {
        this.foodTruckLocation = foodTruckLocation;
    }

    public void setFoodTruckTag(String foodTruckTag) {
        this.foodTruckTag = foodTruckTag;
    }

    public void setFoodTruckTiming(String foodTruckTiming) {
        this.foodTruckTiming = foodTruckTiming;
    }

    public void setFoodTruckCusine(String foodTruckCusine) {
        this.foodTruckCusine = foodTruckCusine;
    }

    public void setFoodTruckImg(String foodTruckImg) {
        this.foodTruckImg = foodTruckImg;
    }

    public void setFoodTruckLogo(String foodTruckLogo) {
        this.foodTruckLogo = foodTruckLogo;
    }

    public void setFoodTruckTotalVotes(int foodTruckTotalVotes) {
        this.foodTruckTotalVotes = foodTruckTotalVotes;
    }

    public void setFoodTruckRating(double foodTruckRating) {
        this.foodTruckRating = foodTruckRating;
    }

    public void setFoodTruckItemList(List<FoodTruckItem> foodTruckItemList) {
        this.foodTruckItemList = foodTruckItemList;
    }


    @Override
    public int describeContents() {
        return 0;
    }

    @Override
    public void writeToParcel(Parcel dest, int flags) {
        dest.writeString(this.foodTruckId);
        dest.writeString(this.foodTruckName);
        dest.writeString(this.foodTruckLocation);
        dest.writeString(this.foodTruckTag);
        dest.writeString(this.foodTruckTiming);
        dest.writeString(this.foodTruckCusine);
        dest.writeString(this.foodTruckImg);
        dest.writeString(this.foodTruckLogo);
        dest.writeInt(this.foodTruckTotalVotes);
        dest.writeDouble(this.foodTruckRating);
        dest.writeTypedList(this.foodTruckItemList);
    }

    public FoodTruck() {
    }

    protected FoodTruck(Parcel in) {
        this.foodTruckId = in.readString();
        this.foodTruckName = in.readString();
        this.foodTruckLocation = in.readString();
        this.foodTruckTag = in.readString();
        this.foodTruckTiming = in.readString();
        this.foodTruckCusine = in.readString();
        this.foodTruckImg = in.readString();
        this.foodTruckLogo = in.readString();
        this.foodTruckTotalVotes = in.readInt();
        this.foodTruckRating = in.readDouble();
        this.foodTruckItemList = in.createTypedArrayList(FoodTruckItem.CREATOR);
    }

    public static final Parcelable.Creator<FoodTruck> CREATOR = new Parcelable.Creator<FoodTruck>() {
        @Override
        public FoodTruck createFromParcel(Parcel source) {
            return new FoodTruck(source);
        }

        @Override
        public FoodTruck[] newArray(int size) {
            return new FoodTruck[size];
        }
    };
}

FoodTruckItem.java

@JsonInclude(JsonInclude.Include.NON_NULL)
public class FoodTruckItem implements Parcelable {
    @JsonProperty("_id")
    private String itemId;
    @JsonProperty("no_of_times_ordered")
    private int noOfTimesOrdered;
    @JsonProperty("item_name")
    private String itemName;
    @JsonProperty("item_tag")
    private String itemTag;
    @JsonProperty("item_category")
    private String itemCategory;
    @JsonProperty("item_stock")
    private int itemStock;
    @JsonProperty("item_price")
    private double itemPrice;
    @JsonProperty("item_img")
    private String itemImg;
    @JsonProperty("no_of_likes")
    private int noOfLikes;
    @JsonProperty("item_quantity_ordered")
    private int itemQuantityOrdered;
    @JsonProperty("item_illustrations")
    private List<String> itemIllustration;

    public String getItemId() {
        return itemId;
    }

    public int getNoOfTimesOrdered() {
        return noOfTimesOrdered;
    }

    public String getItemName() {
        return itemName;
    }

    public String getItemTag() {
        return itemTag;
    }

    public String getItemCategory() {
        return itemCategory;
    }

    public int getItemStock() {
        return itemStock;
    }

    public double getItemPrice() {
        return itemPrice;
    }

    public String getItemImg() {
        return itemImg;
    }

    public int getNoOfLikes() {
        return noOfLikes;
    }

    public int getItemQuantityOrdered() {
        return itemQuantityOrdered;
    }

    public List<String> getItemIllustration() {
        return itemIllustration;
    }

    public void setItemId(String itemId) {
        this.itemId = itemId;
    }

    public void setNoOfTimesOrdered(int noOfTimesOrdered) {
        this.noOfTimesOrdered = noOfTimesOrdered;
    }

    public void setItemName(String itemName) {
        this.itemName = itemName;
    }

    public void setItemTag(String itemTag) {
        this.itemTag = itemTag;
    }

    public void setItemCategory(String itemCategory) {
        this.itemCategory = itemCategory;
    }

    public void setItemStock(int itemStock) {
        this.itemStock = itemStock;
    }

    public void setItemPrice(double itemPrice) {
        this.itemPrice = itemPrice;
    }

    public void setItemImg(String itemImg) {
        this.itemImg = itemImg;
    }

    public void setNoOfLikes(int noOfLikes) {
        this.noOfLikes = noOfLikes;
    }

    public void setItemQuantityOrdered(int itemQuantityOrdered) {
        this.itemQuantityOrdered = itemQuantityOrdered;
    }

    public void setItemIllustration(List<String> itemIllustration) {
        this.itemIllustration = itemIllustration;
    }


    @Override
    public int describeContents() {
        return 0;
    }

    @Override
    public void writeToParcel(Parcel dest, int flags) {
        dest.writeString(this.itemId);
        dest.writeInt(this.noOfTimesOrdered);
        dest.writeString(this.itemName);
        dest.writeString(this.itemTag);
        dest.writeString(this.itemCategory);
        dest.writeInt(this.itemStock);
        dest.writeDouble(this.itemPrice);
        dest.writeString(this.itemImg);
        dest.writeInt(this.noOfLikes);
        dest.writeInt(this.itemQuantityOrdered);
        dest.writeStringList(this.itemIllustration);
    }

    public FoodTruckItem() {
    }

    protected FoodTruckItem(Parcel in) {
        this.itemId = in.readString();
        this.noOfTimesOrdered = in.readInt();
        this.itemName = in.readString();
        this.itemTag = in.readString();
        this.itemCategory = in.readString();
        this.itemStock = in.readInt();
        this.itemPrice = in.readDouble();
        this.itemImg = in.readString();
        this.noOfLikes = in.readInt();
        this.itemQuantityOrdered = in.readInt();
        this.itemIllustration = in.createStringArrayList();
    }

    public static final Parcelable.Creator<FoodTruckItem> CREATOR = new Parcelable.Creator<FoodTruckItem>() {
        @Override
        public FoodTruckItem createFromParcel(Parcel source) {
            return new FoodTruckItem(source);
        }

        @Override
        public FoodTruckItem[] newArray(int size) {
            return new FoodTruckItem[size];
        }
    };
}

现在我基本上想要像 response.getData()...... 那样进行操作。但是没有这样的选择。

1 个答案:

答案 0 :(得分:0)

可能data无法访问,因为它是私有的。尝试克里特吸气剂方法。

public class GenericResponse<T> {

    @JsonProperty("status")
    private String status;
    @JsonProperty("message")
    private String message;
    @JsonProperty("data")
    private T data;

    public T getData() {
        return data;
    }

}