改进了预期的BEGIN_OBJECT但在GET方法中是BEGIN_ARRAY

时间:2016-12-30 14:04:07

标签: android retrofit

我试图通过Retrofit获取一些数据并且我收到此错误。我理解错误是什么,但我不知道如何解决它:

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;
    }}

2 个答案:

答案 0 :(得分:1)

我在这里编辑了你的代码:

public interface ApiInterface {
    @GET("api/category")
    Call<List<Category>> getBusinessCategory();
}

你的api返回Array但你正试图将它转换为单个对象。

编辑:

  private Call<List<Category>> mCall;
  mCall = apiService.getBusinessCategory();
        mCall.enqueue(new Callback<List<Category>>() {
            @Override
            public void onResponse(Call<List<Category>> call, Response<List<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<List<Category>> call, Throwable t) {
                Log.e(TAG, t.toString());
            }
        });

答案 1 :(得分:1)

出现问题是因为您尝试将数组解析为对象,更正后端实现或更改代码,如下所示:

您更正后的界面:

public interface ApiInterface {
@GET("api/category")
Call<List<Category>> getBusinessCategory(); }

变量mCall:

private Call<List<Category>> mCall;