2016-12-30 117 views
0

我试图获得一些数据与改造,我得到这个错误。我明白了什么是错误的,但我不知道如何解决它:改进预期的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; 
    }} 
+0

你可以包括代码'Category' –

+0

嗨约翰,刚刚添加 –

+0

作为回应,你可以看到该服务器返回给你一个数组,更改呼叫调用<列表>。 – b2mob

回答

1

在这里,我编辑您的代码:

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

你的API返回数组,但您要投它到一个单一的对象。

编辑:

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()); 
      } 
     }); 
+0

此更改为此行提供了一个不兼容的错误:mCall = apiService.getBusinessCategory(); –

+0

您还需要更改您的响应实施。我会编辑我的代码。 – savepopulation

+0

是的,请.... –

0

问题的发生是因为你试图解析数组对象,纠正你的后端实现或者如下更改代码:

你纠正接口:

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

和变量mCall:

private Call<List<Category>> mCall;