我收到了通过改造收到的良好响应,并且我能够在我的 View 上显示模型属性(除了一个图像列表之外)。尽管我看到日志具有所需的 JSON 数据,但我从中得到了一个空值。由于它是图像列表,我使用 Glide 来显示它们,并在日志上显示 W/Glide: Load failed for null with size [0x0] class com.bumptech.glide.load.engine.GlideException:收到空模型
我尝试访问的属性是 Data
类中的 promotionalimages
。
如果需要更多信息,请告诉我。
我做错了什么?
型号:
我的 Activity 的一部分,我显示了响应:
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"
}}}
非常感谢您抽出时间。
最佳答案
在 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;
}
关于java - 来自 ok Retrofit 响应的图像的空模型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51601826/