无法将retrifit2响应上的异常清除为“ JsonSyntaxException:java.lang.IllegalStateException:预期为BEGIN_OBJECT,但为STRING”

时间:2018-12-17 06:07:52

标签: android json retrofit2 gson

我找到了许多相关的问题,但是由于对改型的了解不多,我无法解决这个问题。

RestServiceBuilder.getApiService().getAllProductByCatId(TOKEN, Constants.KEY_ROUTE_PRODUCTS_BY_CAT_ID,
            Constants.LIST_VIEW_LIMIT, page, CATEGORY_ID).enqueue(new Callback<ProductListBaseModel>() {
        @Override
        public void onResponse(Call<ProductListBaseModel> call, Response<ProductListBaseModel> response) {
            if (response.code() == 400 || response.code() == 500) {
                ErrorPojoClass errorPojoClass = ErrorUtils.parseApiError(response);
                CustomPopup.displayErrorPopup(a, errorPojoClass);

            } else {
                try {
                    if (response.body().getData().isEmpty()) {

                        pullToLoadView.setComplete();
                        isLoading = false;

                        if (page == 1) {
                            OnProductListResponse listResponse = (OnProductListResponse) a;
                            listResponse.onEmptyProductListFound(true); // produts not found
                        } else {
                        }

                    } else {
                        setupAdapterView(page, response);
                        OnProductListResponse listResponse = (OnProductListResponse) a;
                        listResponse.onEmptyProductListFound(false); // produts found
                    }
                    isSingleProduct = false;
                } catch (Exception e) {
                    Log.e( "found",""+e);
                }
            }
        }

        @Override
        public void onFailure(@NonNull Call<ProductListBaseModel> call, Throwable t) {
            isLoading = false;
            Log.e( "found",""+t);
        }
    });

这是我创建改造对象的方式

public static <S> S createService(Class<S> serviceClass) {

    Gson gson = new GsonBuilder()
            .setDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSZ")
            .setLenient()
            .create();

    HttpLoggingInterceptor interceptor = new HttpLoggingInterceptor();
    interceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
    OkHttpClient client = new OkHttpClient.Builder()
            .addInterceptor(interceptor).build();


    Retrofit retrofit = new Retrofit.Builder()
            .client(client)
            .baseUrl(OPENCART_BASEURL)
            .addConverterFactory(GsonConverterFactory.create(gson))
            .build();


    return retrofit.create(serviceClass);
}

这是我的模型类,用于存储响应中的数据。

public class ProductListBaseModel {

@SerializedName("success")
@Expose
private Integer success;
@SerializedName("error")
@Expose
private List<Object> error = null;
@SerializedName("data")
@Expose
private List<ProductListDataModel> data = null;

public Integer getSuccess() {
    return success;
}

public void setSuccess(Integer success) {
    this.success = success;
}

public List<Object> getError() {
    return error;
}

public void setError(List<Object> error) {
    this.error = error;
}

public List<ProductListDataModel> getData() {
    return data;
}

public void setData(List<ProductListDataModel> data) {
    this.data = data;
}

private String unknown_error;

public ProductListBaseModel(int statusCode, String message) {
    this.success = statusCode;
    this.unknown_error = message;
}

}

我已经记录了流程,然后得到了异常

com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: Expected BEGIN_OBJECT but was STRING

我在posman中检查了响应,发现它是有效的Json数据。我发现了很多与此有关的问题,但无法解决我的问题。

1 个答案:

答案 0 :(得分:0)

这是由于您定义的 Model类获取了String格式的数据,而来自 API 的数据却是OBJECT格式。

>

确保为获取数据定义的 Model类与从 API 获取的数据的格式完全相同。