gpt4 book ai didi

Android Geocoder 无法在 Nexus 6P 和 Android Nougat 7.0 中运行

转载 作者:太空狗 更新时间:2023-10-29 14:46:50 27 4
gpt4 key购买 nike

我无法在 Android 7.0 中使用反向地理编码功能。它在 Marshmallow 中运行良好,在另一部装有 kitkat 的手机上也能正常运行。

错误是请求超时:

java.io.IOException: Timed out waiting for response from server
at android.location.Geocoder.getFromLocation(Geocoder.java:136)

Geocoder isPresent 方法确实返回 true。

最佳答案

我通过向 maps api 发出请求解决了这个问题

并像这样处理 json 响应:

public void onResponse(JSONObject response) {
JsonArray results;
try {
results = ((JsonArray)new JsonParser().parse(response.get("results").toString()));
} catch (JSONException e) {
e.printStackTrace();
return;
}
String country = "";
String stateProvince = "";
String locality = "";
String hood = "";
if (results.size() > 0) {
JsonArray address = results.get(0).getAsJsonObject().get("address_components").getAsJsonArray();
for (JsonElement component : address) {
JsonObject data = component.getAsJsonObject();
for (JsonElement type : data.get("types").getAsJsonArray()) {
if (type.getAsString().equals("country")) {
country = data.get("short_name").getAsString();
} else if (type.getAsString().equals("administrative_area_level_1")) {
stateProvince = data.get("short_name").getAsString();
} else if (type.getAsString().equals("locality")) {
locality = data.get("long_name").getAsString();
} else if (type.getAsString().equals("sublocality")) {
hood = data.get("long_name").getAsString();
}
}
}
}
final String address = getFormattedAddress(country, stateProvince, locality, hood);
}

public String getFormattedAddress(String country, String state, String locality, String hood) {
String address = "";
if(hood.isEmpty()){
if(locality.isEmpty()){
if(!state.isEmpty()){
address += state;
}
}else{
address += locality;
if(!state.isEmpty()){
address += ", " + state;
}
}
}else{
address = hood;
if(locality.isEmpty()){
if(!state.isEmpty()){
address += ", " + state;
}
}else{
address += ", " + locality;
}
}
if(!country.isEmpty()){
if(!address.isEmpty()){
address += ", " + country;
}else{
address += country;
}
}
return address;
}

关于Android Geocoder 无法在 Nexus 6P 和 Android Nougat 7.0 中运行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39208191/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com