Reputation: 2059
From times to times I meet the Service Not Available
Exceptions in my application. Most of the time, there is no problem. But sometimes it happens. (The app is still under development)
Only way to solve it is to restart the handset. Relevant Issue38009 on support forums. But what I understand from the comments is that rebooting the device solve the issue forever.
Is this correct?
Because in my case, the bug can return (frequency: once or twice a month I think & I am full time on this app).
Is there a way to restart the geocoder from inside the application?
The only solution I have is to provide an alternative solution just in case to fetch "manually" the address like this below. Do you have any better?
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;
Upvotes: 5
Views: 3752
Reputation: 393
the link from @mike states: a fallback implementation using GeoCoder handler and asynctask class both needs to be synchronized - like if no service we can get the address from URL. This is the only solution we can provide unfortunately @Poutrathor.
Refer here Service not Available - Geocoder Android
Also we cannot force user to reboot but at least inform a solution about it.
Upvotes: 0