我发现 usinghttp://ip-api.com/json
是获取必要的用户位置详细信息的一种简单方法。结果,例如来自该站点的结果是:
{"as":"AS37061 ONECOM","city":"Nairobi","country":"Kenya","countryCode":"KE","isp":"One Communications Ltd","lat":-1.2833,"lon":36.8167,"org":"ONECOM","query":"41.203.219.198","region":"110","regionName":"Nairobi Province","status":"success","timezone":"Africa/Nairobi","zip":""}
我一直在用以下代码解析上面的 Json 结果:
String countryCode = null;
String name = null;
if (reader != null) {
try {
reader.setLenient(true);
reader.beginObject();
while (reader.hasNext()) {
switch (reader.nextName()) {
case "countryCode":
countryCode = reader.nextString().toLowerCase();
break;
case "country":
name = reader.nextString();
break;
default:
reader.skipValue();
break;
}
}
reader.endObject();
} catch (IOException e) {
e.printStackTrace();
}
}
现在我收到此错误:
Fatal Exception: java.lang.IllegalStateException: Expected BEGIN_OBJECT but was END_DOCUMENT at line 1 column 2016
at com.google.gson.stream.JsonReader.beginObject(JsonReader.java:374)
at com.radioafrica.music.fragment.TopTracks.getCountryCode(TopTracks.java:397)
at com.radioafrica.music.fragment.TopTracks.access$1100(TopTracks.java:57)
at com.radioafrica.music.fragment.TopTracks$9.run(TopTracks.java:377)
at java.lang.Thread.run(Thread.java:818)
据我所知,代码很好,但并不总是有效。似乎是什么问题?