android - 通过 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);
}
});
}```
解决方案
它告诉您将 json 字符串转换为您的模型时发生了问题。具体来说,您的模型属性之一被声明为 String 类型,但 json 字符串的字段(键)映射到对象类型。请检查您的模型的兼容性以及您从 api 收到的结果
推荐阅读
- python - 拟合 Keras 序列模型给出 ValueError: Failed to convert a NumPy array to a Tensor (Unsupported object type numpy.ndarray)
- excel - 借方和贷方金额
- sql - 使用 SQL JSON 函数从 Oracle 12c 创建 JSON 数据
- sqlite - 重新排序 sqlite3 数据库中的行
- networking - 应用层具体是什么?
- postgresql - 在 Flask SqlAlchemy 查询中使用 to_char
- sql - 如何在 Access SQL 的追加查询中使用 SELECT?
- c# - 我无法在 AI 上播放动画,统一 2d
- android - 将 exoplayer 从服务绑定到活动不起作用?
- java - 如何将特定的 Android 代码添加到 Flutter