【发布时间】:2017-05-24 02:54:03
【问题描述】:
更新: 我现在确定问题出在我的改造电话上,它正在检索错误的数据。我正在使用 android 设备测试该应用程序,因此 localhost 将无法正常工作。在我的改造电话中,我尝试使用我的公共 ip 和 wifi ip(通过查看我连接到的 wifi 的属性找到使用),但改造电话无法获取我的 JSON。任何想法如何解决这个问题?
由于某种原因,我的 JSON 被 GSON 视为格式错误,因此我使用了Gson.setLenient()。
我用 jslint 检查了我的 JSON,它是有效的。另外,我不确定为什么第一行以{开头时被认为是string。
为什么我的 JSON 格式不正确,为什么会抛出这个错误?
JSON
{
"result": [
{
"_id": "5924eea5bd50af38702c14ae",
"name": "Stoney's Bread Company",
"cuisineType": "Italian",
"address": "325 Kerr Street, Oakville",
"openTime": "0900",
"closeTime": "2100",
"lng": 43.443733,
"lat": -79.68146,
"__v": 0,
}
]
}
改造代码
Gson gson = new GsonBuilder().setLenient().create();
final MapInterface retrofit = new Retrofit.Builder()
.baseUrl("http://myComputerIP/places/")
.addConverterFactory(GsonConverterFactory.create(gson))
.build().create(MapInterface.class);
Call<Result> call = retrofit.getMongoosePlace("3333733", "-79.681460");
public Double getLat() {
return lat;
}
public void setLat(Double lat) {
this.lat = lat;
}
public Integer getV() {
return v;
}
public void setV(Integer v) {
this.v = v;
}
}
【问题讨论】:
-
请查看question
-
最后还有一个不需要的逗号:
"__v": 0, -
你能展示你的Result类吗?
-
谢谢@dat3450,不幸的是,它没有用。
-
你的改造调用应该返回一个 Retrieve 实例来匹配上面的 JSON,而不是一个 Result 实例。另外,您确定您发布的 JSON 与 Retrofit 实际从您的服务器检索到的相匹配吗?
标签: java android json parsing gson