我试图获得一些数据与改造,我得到这个错误。我明白了什么是错误的,但我不知道如何解决它:改进预期的BEGIN_OBJECT,但GET方法中的BEGIN_ARRAY
com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: Expected BEGIN_OBJECT but was BEGIN_ARRAY at line 1 column 2 path $
我试图在这里找到在其他问题的答案,但我找不到像我的问题什么...
我的接口代码:
public interface ApiInterface {
@GET("api/category")
Call<Category> getBusinessCategory();
}
我的类代码调用改造:
private Call<Category> mCall;
mCall = apiService.getBusinessCategory();
mCall.enqueue(new Callback<Category>() {
@Override
public void onResponse(Call<Category> call, Response<Category> response) {
if (response.isSuccess()) {
Log.e(TAG, response.toString());
} else {
Toast.makeText(getApplication(), "No conexion", Toast.LENGTH_SHORT).show();
}
}
@Override
public void onFailure(Call<Category> call, Throwable t) {
Log.e(TAG, t.toString());
}
});
这是JSON:
[
{
"id": 1,
"name": "FOOD",
"imageRef": "v1475594353/categories/a",
"translate": null
},
{
"id": 2,
"name": "CAR",
"imageRef": "v1475594195/categories/b",
"translate": null
}
]
分类等级:
public class Category implements Serializable {
@SerializedName("category")
@Expose
private final static String TAG = "Category";
private String imageRef = "";
private Long id;
private String name;
private String translate;
private transient Bitmap image;
... Getters and setters
ApiClient类
public class ApiClient {
public static final String BASE_URL = "http://xxxxx/";
private static Retrofit retrofit = null;
public static Retrofit getClient() {
if (retrofit==null) {
retrofit = new Retrofit.Builder()
.baseUrl(BASE_URL)
.addConverterFactory(GsonConverterFactory.create())
.build();
}
return retrofit;
}}
你可以包括代码'Category' –
嗨约翰,刚刚添加 –
作为回应,你可以看到该服务器返回给你一个数组,更改呼叫调用<列表>。 –
b2mob