有时我会在我的申请中遇到Service Not Available
例外情况。大多数时候,没有问题。但有时它会发生。 (该应用程序仍在开发中)
解决问题的唯一方法是重启手机。 Relevant Issue38009 on support forums。但我从评论中理解的是,重启设备可以解决永远的问题。
这是对的吗?
因为在我的情况下,错误可以返回(频率:我认为每月一次或两次&我是这个应用程序的全职时间)。
是否有从应用程序内部重新启动地理编码器的方法?
我唯一的解决方案是提供一种替代解决方案,以防万一“手动”获取地址,如下所示。你有更好的吗?
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;
答案 0 :(得分:0)
来自@mike的链接:使用GeoCoder处理程序和asynctask类的回退实现都需要同步 - 就像没有服务我们可以从URL获取地址。 这是我们可以提供的唯一解决方案@Poutrathor。
请参阅此处Service not Available - Geocoder Android
此外,我们无法强制用户重启,但至少要告知解决方案。