好的改装响应中的图像为空模型

时间:2018-07-30 20:27:29

标签: java android model retrofit android-glide

通过改型我可以收到很好的答复,并且我可以在视图上显示模型属性,除了一张图像列表。尽管看到日志包含必需的JSON数据,但我从中获得了空值。由于它是图像列表,因此我正在使用Glide进行显示,并且在日志中显示W/Glide: Load failed for null with size [0x0] class com.bumptech.glide.load.engine.GlideException: Received null model

我要访问的属性是promotionalimages类中的Data

如果需要更多信息,请告诉我。

我在做什么错?

型号:

RestaurantModel

我的活动的一部分,我在其中显示响应:

Call<RestaurantModel> call = apiInterface.getRestaurant(getSharedPreference.getToken(), identifier);
    call.enqueue(new Callback<RestaurantModel>() {
        @Override
        public void onResponse(@NonNull Call<RestaurantModel> call, @NonNull Response<RestaurantModel> response) {
            restaurant = response.body();

            rName.setText(restaurant.getData().getName());
            ratingtxt.setText(String.format("%s", restaurant.getData().getAvgRatingCount()));
            Glide.with(context).load(restaurant.getData().getPromotionalimages().get(0).getPromotionalPicture()).apply(RequestOptions.centerCropTransform().placeholder(R.drawable.default_image)).into(holder.image);

响应:

{
"data": {
    "identifier": "0464eee2-5160-40e7-8ae0-77e7484fe251",
    "name": "Apple Lab",
    "phonenumber": "11116425300",
    "start_time": "7:00 AM",
    "end_time": "1:00 PM",
    "created_at": "2018-07-30 20:10:05",
    "updated_at": "2018-07-30 20:11:00",
    "reviews_count": 0,
    "avgRatingCount": 0,
    "categoryCount": 0,
    "subCategoryCount": 0,
    "totalItemCount": 0,
    "availableItemCount": 0,
    "isOpen": 1,
    "logo": "https://x.co/storage/logoandcover/0464eee2-5160-40e7-8ae0-77e7484fe251/15b5f1dc0ef064.png",
    "cover": "https://x.co/storage/logoandcover/0464eee2-5160-40e7-8ae0-77e7484fe251/15b5f1df0e2f78.png",
    "galleryimages": [
        {
            "id": 6,
            "identifier": "0464eee2-5160-40e7-8ae0-77e7484fe2511",
            "restaurant_id": 7,
            "gallery_picture": "https://x.co/storage/galleryimages/0464eee2-5160-40e7-8ae0-77e7484fe251/15b5f1e2cc80aa.png",
            "created_at": "2018-07-30 20:18:20",
            "updated_at": "2018-07-30 20:18:20"
        }
    ],
    "location": {
        "house": "x",
        "road": "x",
        "details": "x",
        "country": "x",
        "created_at": "2018-07-30 20:12:28",
        "updated_at": "2018-07-30 20:12:28",
        "cityName": "x",
        "areaName": "x"
    },
    "promotionalimages": [
        {
            "id": 10,
            "identifier": "0464eee2-5160-40e7-8ae0-77e7484fe2511",
            "restaurant_id": 7,
            "promotional_picture": "https://x.co/storage/promotionalimages/0464eee2-5160-40e7-8ae0-77e7484fe251/15b5f1ec0afffb.png",
            "created_at": "2018-07-30 20:20:48",
            "updated_at": "2018-07-30 20:20:48"
        },
        {
            "id": 11,
            "identifier": "0464eee2-5160-40e7-8ae0-77e7484fe2512",
            "restaurant_id": 7,
            "promotional_picture": "https://x.co/storage/promotionalimages/0464eee2-5160-40e7-8ae0-77e7484fe251/15b5f1ec13a9b7.png",
            "created_at": "2018-07-30 20:20:49",
            "updated_at": "2018-07-30 20:20:49"
        }
    ],
    "features": [
        {
            "id": 25,
            "restaurant_id": 7,
            "name": "Food Quality",
            "created_at": "2018-07-30 20:11:00",
            "updated_at": "2018-07-30 20:11:00",
            "favoritesCount": 0,
            "isFavorited": false,
            "favorites": []
        },
        {
            "id": 26,
            "restaurant_id": 7,
            "name": "Customer Service",
            "created_at": "2018-07-30 20:11:00",
            "updated_at": "2018-07-30 20:11:00",
            "favoritesCount": 0,
            "isFavorited": false,
            "favorites": []
        },
        {
            "id": 27,
            "restaurant_id": 7,
            "name": "Environment",
            "created_at": "2018-07-30 20:11:00",
            "updated_at": "2018-07-30 20:11:00",
            "favoritesCount": 0,
            "isFavorited": false,
            "favorites": []
        },
        {
            "id": 28,
            "restaurant_id": 7,
            "name": "Food Pricing",
            "created_at": "2018-07-30 20:11:00",
            "updated_at": "2018-07-30 20:11:00",
            "favoritesCount": 0,
            "isFavorited": false,
            "favorites": []
        }
    ],
    "reviews": [],
    "categories": [],
    "menus": [],
    "profile": {
        "id": 7,
        "restaurant_id": 7,
        "logo": "https://x.co/storage/logoandcover/0464eee2-5160-40e7-8ae0-77e7484fe251/15b5f1dc0ef064.png",
        "cover": "https://x.co/storage/logoandcover/0464eee2-5160-40e7-8ae0-77e7484fe251/15b5f1df0e2f78.png",
        "payable_amount": "0.00",
        "payment_received": "0.00",
        "last_paid": "2018-07-30 14:17:21",
        "score": "100.00",
        "vat_reg_number": null,
        "created_at": "2018-07-30 20:11:00",
        "updated_at": "2018-07-30 20:17:21",
        "status": "Bronze"
    }}}

非常感谢您的宝贵时间。

2 个答案:

答案 0 :(得分:1)

在JSON响应中,我看到:

"promotionalimages": [
    {
        ...
        "promotional_picture": "https://x.co/storage/promotionalimages/0464eee2-5160-40e7-8ae0-77e7484fe251/15b5f1ec13a9b7.png",
    }
]

但是,在您的模型类中,我看到了:

public static class Promotionalimage {
    ...
    private String promotionalPicture;
}

这两个名称不相同:JSON使用promotional_picture,而Java使用promotionalPicture。可能您还设置了一些其他代码,以将下划线转换为驼峰大小写,但我敢打赌,您不会。

有两种修复方法。我建议添加@SerializedName批注:

public static class Promotionalimage {
    ...
    @SerializedName("promotional_picture")
    private String promotionalPicture;
}

但是您也可以简单地更改Java字段的名称:

public static class Promotionalimage {
    ...
    private String promotional_picture;
}

答案 1 :(得分:0)

只需尝试毕加索

Picasso.with(context)
                    .load(link)
                    .into(yourView)