I am building an android application, and i am using Geocoder to decode the location, but i have noticed that sometimes when there is comparatively low connectivity Gecoder does not work. But during the same time, the browser and the rest of the internet based apps function. Is there any way to fix this ? What could possibly be going wrong in this. I do understand that its a back end service but it should not matter.For regular http requests we could give a retry and fix similar issues, but how can i fix this ? Any suggestions?
class ReverseGeocodingTask extends AsyncTask<Double, Void, String> implements OnDismissListener{
Context context;
ProgressDialog progDialog;
String progressString;
public ReverseGeocodingTask(Context context, String progressStr) {
this.context = context;
this.progressString = progressStr;
if(!isCancelled()){
initProgDialog();
}
}
void initProgDialog(){
progDialog = new ProgressDialog(context);
progDialog.setCanceledOnTouchOutside(false);
progDialog.setButton(DialogInterface.BUTTON_NEGATIVE, getString(R.string.btn_cancel),new DialogInterface.OnClickListener() {
public void onClick(final DialogInterface dialog, final int id) {
progDialog.dismiss();
}
});
progDialog.setMessage(progressString);
progDialog.setOnDismissListener(this);
progDialog.show();
}
@Override
protected void onPreExecute() {
//removeAllPendingRevGeocodingTasks();
addTask(this);
super.onPreExecute();
}
@Override
protected String doInBackground(Double... params) {
String result = null;
if(!isCancelled()){
Geocoder geocoder = new Geocoder(getApplicationContext(), Locale.getDefault());
try {
List<Address> list = geocoder.getFromLocation(latitude, longitude, 1);
if (list != null && list.size() > 0) {
Address address = list.get(0);
result = address.getAddressLine(0) + ", " + address.getLocality();
}
} catch (IOException e) {
progDialog.dismiss();
failedToUpdateLocation();
Log.e("Location listener", "Failed to decode location name");
}
}
return result;
}
@Override
protected void onCancelled() {
progDialog.dismiss();
this.cancel(true);
super.onCancelled();
}
@Override
protected void onPostExecute(String result) {
latLongInDegrees = " "+latLongInDegreesToDMSString(latitude,longitude);
setLocationName(result);
updateLocationName(result + latLongInDegrees);
String progressStr=getString(R.string.msg_slp_fetch_from_geocoder_result_1)+latLongInDegreesToDMSString(latitude,longitude)+"...";
executeSLPWebserviceTask(latitude, longitude, progressStr);
finishedUpdatingLocation(latitude,longitude);
progDialog.dismiss();
super.onPostExecute(result);
this.cancel(true);
//removeAllPendingRevGeocodingTasks();
}
public void onDismiss(DialogInterface dialog) {
this.cancel(true);
}
}
protected void executeRevGeocoderTask(double lat, double longitude)
{
String progressStr=getString(R.string.msg_rev_geocoder_fetch)+" "+latLongInDegreesToDMSString(lat,longitude);
ReverseGeocodingTask task = new ReverseGeocodingTask(this,progressStr);
task.execute(lat,longitude);
}
Geocoder is a backend service for which we dont have any handle on its http request or anything of that sort. it requires good internet connection.