我知道这不是第一次有人问这个问题,但是使用 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 服务无法将数组转换为 json 并且改造将其读取为 String 时。 调用您的 API 服务提供商来解决将数组转换为 json :) 例如
"images": "[\"1487801544.jpg\",\"1487801544.jpg\",\"148801544.jpg\"]"
上面的改造读作字符串,应该如下更改:
"images": [
"1487801544.jpg",
"1487801544.jpg",
"1487801544.jpg"
]
关于android - Retrofit2:应为 BEGIN_ARRAY 但在第 1 行第 268 列路径 $[0].images 处为 STRING,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44226370/