首页 > 解决方案 > 通过 Retrofit 获取 API 时出错,但相同的 API 正在与邮递员一起使用

问题描述

这是我们面临的错误 com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: Expected a string but was BEGIN_OBJECT at line 1 column 1119 path $.data[4].last_received_message

public void DeviceList(String api, final ApiType apiTypes, String page_no, String authtoken) {
        ProgressDialog progressDialog = new ProgressDialog();
        try {
            this.pdLoading = progressDialog.getInstance(this.context);
            this.pdLoading.setProgressStyle(android.R.style.Widget_ProgressBar_Small);
            if (this.pdLoading.isShowing()) {
                this.pdLoading.cancel();
            }
            this.pdLoading.show();
        } catch (Exception e) {
            e.printStackTrace();
        }
        this.apiTypeVariable = apiTypes;
        /*RestAdapter adapter = new RestAdapter.Builder()
                .setEndpoint(api) //Setting the Root URL
                .build();*/
        Retrofit retrofit = new Retrofit.Builder().baseUrl(api).addConverterFactory(GsonConverterFactory.create()).client(okHttpClient).build();
        GitApi gi = retrofit.create(GitApi.class);
        call = (Call<T>) gi.getDevices(authtoken, page_no);
        call.enqueue(new Callback<T>() {

            @Override
            public void onResponse(Call<T> call, Response<T> response) {
                t = (T) response.body();
                if (pdLoading.isShowing())
                    pdLoading.cancel();
                onResponseListner.onResponse(t, apiTypeVariable, true);
            }

            @Override
            public void onFailure(Call<T> call, Throwable t) {
                Log.e(apiTypes.toString(), t.getMessage() + ".");
                if (pdLoading.isShowing())
                    pdLoading.cancel();
                onResponseListner.onResponse(null, apiTypeVariable, false);
            }

        });

}```

标签: android

解决方案


它告诉您将 json 字符串转换为您的模型时发生了问题。具体来说,您的模型属性之一被声明为 String 类型,但 json 字符串的字段(键)映射到对象类型。请检查您的模型的兼容性以及您从 api 收到的结果


推荐阅读