Pou*_*hor 5 android reverse-geocoding google-geocoder service-not-available
有时我会Service Not Available在我的申请中遇到例外情况.大多数时候,没有问题.但有时它会发生.(该应用程序仍在开发中)
解决问题的唯一方法是重启手机.关于支持论坛的相关问题38009.但我从评论中理解的是,重启设备可以永远解决问题.
它是否正确?
因为在我的情况下,错误可以返回(频率:我认为每月一次或两次,我在这个应用程序上是全职).
有没有办法从应用程序内部重新启动地理编码器?
我唯一的解决方案是提供一个替代解决方案,以防万一"手动"获取地址,如下所示.你有更好的吗?
Dbg.d(TAG, "=== address_info FetchLocationTask - doInBackground");
new Thread(new Runnable() {
@Override
public void run() {
Looper.prepare();
mHandler = new Handler();
Looper.loop();
}
}).start();
/**
* Implementation of a second method in case of failure:
*
*/
double lat = Double.parseDouble(args[0]);
double lng = Double.parseDouble(args[1]);
String address = String.format(Locale.ENGLISH,
"http://maps.googleapis.com/maps/api/geocode/json?latlng="+Double.toString(lat)+","+Double.toString(lng)+"&sensor=true&language="+Locale.getDefault().getCountry(), lat, lng);
Dbg.d(TAG, "fetching path : "+ address);
HttpGet httpGet = new HttpGet(address);
HttpClient client = new DefaultHttpClient();
HttpResponse response;
StringBuilder stringBuilder = new StringBuilder();
List<Address> retList = null;
try {
response = client.execute(httpGet);
HttpEntity entity = response.getEntity();
InputStream stream = entity.getContent();
int b;
while ((b = stream.read()) != -1) {
stringBuilder.append((char) b);
}
JSONObject jsonObject = new JSONObject();
jsonObject = new JSONObject(stringBuilder.toString());
retList = new ArrayList<Address>();
if("OK".equalsIgnoreCase(jsonObject.getString("status"))){
JSONArray results = jsonObject.getJSONArray("results");
for (int i=0;i<results.length();i++ ) {
JSONObject result = results.getJSONObject(i);
String indiStr = result.getString("formatted_address");
Address addr = new Address(Locale.ITALY);
addr.setAddressLine(0, indiStr);
Dbg.d(TAG, "adresse :"+addr.toString());
retList.add(addr);
}
}
} catch (ClientProtocolException e) {
Dbg.e(TAG, "Error calling Google geocode webservice.", e);
} catch (IOException e) {
Dbg.e(TAG, "Error calling Google geocode webservice.", e);
} catch (JSONException e) {
Dbg.e(TAG, "Error parsing Google geocode webservice response.", e);
}
JSONObject jObj = new JSONObject();
boolean found = false;
if (retList.size() > 0) {
Address a = retList.get(0);
String name = a.getAddressLine(0);
String city = a.getLocality();
String postalCode = a.getPostalCode();
String country = a.getCountryName();
if (!TextUtils.isEmpty(name)) {
JsonUtils.setSringInJson(jObj, BookLocation.ADDRESS_NAME_KEY, name);
found = true;
}
if (!TextUtils.isEmpty(postalCode)) {
JsonUtils.setSringInJson(jObj, BookLocation.ADDRESS_POSTAL_CODE_KEY, postalCode);
found = true;
}
if (!TextUtils.isEmpty(city)) {
JsonUtils.setSringInJson(jObj, BookLocation.ADDRESS_CITY_KEY, city);
found = true;
}
if (!TextUtils.isEmpty(country)) {
JsonUtils.setSringInJson(jObj, BookLocation.ADDRESS_COUNTRY_KEY, country);
found = true;
}
}
mHandler.getLooper().quit();
if (found) {
return jObj;
} else return null;
Run Code Online (Sandbox Code Playgroud)
小智 0
@mike 的链接指出:使用 GeoCoder 处理程序和 asynctask 类的后备实现都需要同步 - 就像没有服务我们可以从 URL 获取地址一样。不幸的是,这是我们可以提供的唯一解决方案@Poutrathor。
此外,我们不能强迫用户重新启动,但至少告知解决方案。
| 归档时间: |
|
| 查看次数: |
3672 次 |
| 最近记录: |