我想从服务器读取响应,但我有内部Json的问题
Json对象导致我这么多问题::(
[
{
"num_id": 1,
"row": {
"coord_shirota": "59.932723",
"www": "www.anichkov.ru",
"metro": "Гостиный Двор",
"description": "Старейшее здание на Невском проспекте, императорская резиденция",
"address_manual": "Санкт-Петербург, Невский просп., 39 лит. А",
"obj_history": "",
"oid": "3285",
"work_time": "пн-сб 10:00-18:00",
"ogrn": "2147483647123",
"metro_distance": "584.0",
"phone": "8(812)310-93-80, 8(812)310-43-95.",
"inn": "7840325991",
"street": "Невский проспект",
"metro_dolgota": "30.333745",
"home": "39",
"name_en": "Anichkov Palace",
"metro_line": "Невско-Василеостровская линия",
"name": "Аничков Дворец",
"town": "Санкт-Петербург",
"for_disabled": "0",
"addressline": "Санкт-Петербург, Невский проспект, 39",
"district": "Центральный район",
"country": "Россия",
"obj_history_en": "",
"subdistrict": "муниципальный округ № 78",
"coord_dolgota": "30.341459",
"description_en": "The oldest building on Nevsky prospekt, imperial palace",
"metro_shirota": "59.933938",
"type": "Исторический",
"email": "anichkovmuz@mail.ru"
}
},
...
]
我要求的Api:
public interface SpbGovSiteApi {
@Headers({ "Authorization: Token 60a368fde851f786532fdb4b65c6fdb189122666" })
@GET("123/versions/latest/data?per_page=1000")
Call<List<SpbGovSiteResponse>> getInfo();
}
我的回复课程:
public class Row {
private float coord_shirota;
private float coord_dolgota;
private String www;
private String description;
private String address_manual;
private String name;
//...getters
}
class SpbGovSiteResponse {
private Row row;
public Row getRow() {
return row;
}
}
我的改装功能:
public Observable<Map<String,String>> getInfo() {
return spbGovSiteResponse(retrofit.create(SpbGovSiteApi.class).getInfo());
}
private Observable<Map<String,String>> spbGovSiteResponse(Call<List<SpbGovSiteResponse>> call){
Log.e("gg",call.request().url().toString());
return Observable.create(e -> {
call.enqueue(new Callback<List<SpbGovSiteResponse>>() {
@Override
public void onResponse(Call<List<SpbGovSiteResponse>> call1, Response<List<SpbGovSiteResponse>> response) {
for(SpbGovSiteResponse item: response.body()) {
Map map=new HashMap<String,String>();
//map.put("name",item.getRow().getName());
//map.put("longtide",item.getRow().getCoord_dolgota());
//map.put("latitude",item.getRow().getCoord_shirota());
e.onNext(map);
}
e.onComplete();
}
@Override
public void onFailure(Call<List<SpbGovSiteResponse>> call1, Throwable t) {
e.onError(t);
}
});
});
}
最后我的问题:
在观察者中,我使用接口方法onFailed
中的throwable参数在日志中打印它。
04-05 18:52:01.019 14703-14703/ru.scapegoats.museumspb W/System.err: java.lang.NumberFormatException: empty String
04-05 18:52:01.020 14703-14703/ru.scapegoats.museumspb W/System.err: at java.lang.FloatingDecimal.readJavaFormatString(FloatingDecimal.java:1071)
04-05 18:52:01.020 14703-14703/ru.scapegoats.museumspb W/System.err: at java.lang.Double.parseDouble(Double.java:547)
04-05 18:52:01.020 14703-14703/ru.scapegoats.museumspb W/System.err: at com.google.gson.stream.JsonReader.nextDouble(JsonReader.java:909)
04-05 18:52:01.020 14703-14703/ru.scapegoats.museumspb W/System.err: at com.google.gson.Gson$3.read(Gson.java:308)
04-05 18:52:01.020 14703-14703/ru.scapegoats.museumspb W/System.err: at com.google.gson.Gson$3.read(Gson.java:302)
04-05 18:52:01.020 14703-14703/ru.scapegoats.museumspb W/System.err: at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$1.read(ReflectiveTypeAdapterFactory.java:129)
04-05 18:52:01.020 14703-14703/ru.scapegoats.museumspb W/System.err: at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.read(ReflectiveTypeAdapterFactory.java:220)
04-05 18:52:01.020 14703-14703/ru.scapegoats.museumspb W/System.err: at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$1.read(ReflectiveTypeAdapterFactory.java:129)
04-05 18:52:01.020 14703-14703/ru.scapegoats.museumspb W/System.err: at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.read(ReflectiveTypeAdapterFactory.java:220)
04-05 18:52:01.020 14703-14703/ru.scapegoats.museumspb W/System.err: at com.google.gson.internal.bind.TypeAdapterRuntimeTypeWrapper.read(TypeAdapterRuntimeTypeWrapper.java:41)
04-05 18:52:01.020 14703-14703/ru.scapegoats.museumspb W/System.err: at com.google.gson.internal.bind.CollectionTypeAdapterFactory$Adapter.read(CollectionTypeAdapterFactory.java:82)
04-05 18:52:01.020 14703-14703/ru.scapegoats.museumspb W/System.err: at com.google.gson.internal.bind.CollectionTypeAdapterFactory$Adapter.read(CollectionTypeAdapterFactory.java:61)
04-05 18:52:01.020 14703-14703/ru.scapegoats.museumspb W/System.err: at retrofit2.converter.gson.GsonResponseBodyConverter.convert(GsonResponseBodyConverter.java:37)
04-05 18:52:01.020 14703-14703/ru.scapegoats.museumspb W/System.err: at retrofit2.converter.gson.GsonResponseBodyConverter.convert(GsonResponseBodyConverter.java:25)
04-05 18:52:01.020 14703-14703/ru.scapegoats.museumspb W/System.err: at retrofit2.ServiceMethod.toResponse(ServiceMethod.java:119)
04-05 18:52:01.020 14703-14703/ru.scapegoats.museumspb W/System.err: at retrofit2.OkHttpCall.parseResponse(OkHttpCall.java:218)
04-05 18:52:01.020 14703-14703/ru.scapegoats.museumspb W/System.err: at retrofit2.OkHttpCall$1.onResponse(OkHttpCall.java:112)
04-05 18:52:01.020 14703-14703/ru.scapegoats.museumspb W/System.err: at okhttp3.RealCall$AsyncCall.execute(RealCall.java:141)
04-05 18:52:01.020 14703-14703/ru.scapegoats.museumspb W/System.err: at okhttp3.internal.NamedRunnable.run(NamedRunnable.java:32)
04-05 18:52:01.020 14703-14703/ru.scapegoats.museumspb W/System.err: at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1133)
04-05 18:52:01.020 14703-14703/ru.scapegoats.museumspb W/System.err: at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:607)
04-05 18:52:01.020 14703-14703/ru.scapegoats.museumspb W/System.err: at java.lang.Thread.run(Thread.java:761)
如果我把类SpbGovSiteApi留空了,我的程序工作得很好......所以我决定在那里出现这个问题-_-
如果我发表评论
class SpbGovSiteResponse {
private int num_id;
// private Row row;
// public Row getRow() {
// return row;
// }
}
我的程序运行没有问题
答案 0 :(得分:0)
您需要输入数字类型String
:
public class Row {
private String coord_shirota; // <- String not float
private String coord_dolgota; // <- String not float
private String www;
private String description;
private String address_manual;
private String name;
//...getters
}
或将响应作为数字返回:
"row": {
"coord_shirota": 59.932723, <- NOT "59.932723"