【发布时间】:2017-10-28 19:28:15
【问题描述】:
我知道这不是第一次有人问这个问题,但是使用 Retrofit2 我找不到解决问题的正确方法。
我有一个包含字符串列表的对象。当我想将 JSON 响应转换为我的对象时,所有其他字段都可以,但是将字符串列表转换为我的列表时出现此错误:
Retrofit2: Expected BEGIN_ARRAY but was STRING at line 1 column 268 path $[0].images
这是我的 API:
@POST("/cp/api/")// get list of products
Call<List<Product>> Get_Special_Products(@Body Object request);
我的改造设置:
public Retrofit Store_retrofit(OkHttpClient client) {
return new Retrofit.Builder()
.baseUrl(Urls.Sotre_Base_Url)
.client(client)
.addConverterFactory(GsonConverterFactory.create())
.build();
}
我的对象:
public class Product implements Serializable {
@SerializedName("id")
private int id;
@SerializedName("user_id")
private int user_id;
@SerializedName("cat_id")
private int cat_id;
@SerializedName("title")
private String title;
@SerializedName("description")
private String description;
@SerializedName("image")
private String image;
@SerializedName("images")
private List<String> images;
public int getUser_id() {
return user_id;
}
public int getCat_id() {
return cat_id;
}
public String getTitle() {
return title;
}
public String getDescription() {
return description;
}
public String getImage() {
return image;
}
public List<String> getImages() {
return images;
}
}
这是导致图像错误的 JSON 的一部分:
images:[
"1487801544.jpg","1487801544.jpg","1487801544.jpg"
]
【问题讨论】:
-
我猜你的反应正在改变图像的数量。检查 api 是否在一张图像的情况下返回对象
-
我认为你应该检查来自你的 api 的响应,这个错误通常是由不正确的响应格式引起的。
-
@HammadAkram 谢谢,我找到了问题,错误的数据类型来自服务器。
-
@SepJaPro2.4 谢谢,我找到了问题,错误的数据类型来自服务器。
标签: android json web-services retrofit2