问题
I know this is not the first time someone asking about this problem but with Retrofit2 I can't find the right solution to my problem.
I have an object that contains a list of String. when I want to convert JSON response to my object all other fields are ok but I got this error for converting the list of string to my list:
Retrofit2: Expected BEGIN_ARRAY but was STRING at line 1 column 268 path $[0].images
This is my API:
@POST("/cp/api/")// get list of products
Call<List<Product>> Get_Special_Products(@Body Object request);
My Retrofit setting:
public Retrofit Store_retrofit(OkHttpClient client) {
return new Retrofit.Builder()
.baseUrl(Urls.Sotre_Base_Url)
.client(client)
.addConverterFactory(GsonConverterFactory.create())
.build();
}
My Object:
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;
}
}
and this a part of JSON that cause the error for image:
images:[
"1487801544.jpg","1487801544.jpg","1487801544.jpg"
]
回答1:
This mostly happens when your API service can not convert array to json and retrofit reads it as String . call to your API service provider to solve converting array to json :) for example
"images": "[\"1487801544.jpg\",\"1487801544.jpg\",\"148801544.jpg\"]"
retrofit read above as String and should be change as below :
"images": [
"1487801544.jpg",
"1487801544.jpg",
"1487801544.jpg"
]
来源:https://stackoverflow.com/questions/44226370/retrofit2expected-begin-array-but-was-string-at-line-1-column-268-path-0-ima