3

I have tested the following code in two different devices one is 4.3 and other is 4.1.2.The 4.3 device is giving me the address while the tablet i used (4.1.2) is giving exception in getFromLocation().I dont understand why two devices giving two different outputs.

Geocoder geocoder = new Geocoder(AAAView.this, Locale.getDefault());
            // Get the current location from the input parameter list
            Location loc = params[0];
            // Create a list to contain the result address
            List<Address> addresses ;
            try {

                lat=loc.getLatitude();
                 lon=loc.getLongitude();

         addresses = geocoder.getFromLocation(lat,lon, 1);

           System.out.println("The address is " + addresses );

            } catch (IOException e1) {
            Log.e("LocationSampleActivity",
                    "IO Exception in getFromLocation() ");
            e1.printStackTrace();
            return ("Cannot get address");
            }

Logcat error

03-24 18:51:54.501: W/System.err(12673): java.io.IOException: Service not Available
03-24 18:51:54.509: W/System.err(12673):    at android.location.Geocoder.getFromLocation(Geocoder.java:136)
03-24 18:51:54.509: W/System.err(12673):    at com.assuretech.ku.aaa.AAAView$GetAddressTask.doInBackground(AAAView.java:1070)
03-24 18:51:54.517: W/System.err(12673):    at com.assuretech.ku.aaa.AAAView$GetAddressTask.doInBackground(AAAView.java:1)
03-24 18:51:54.517: W/System.err(12673):    at android.os.AsyncTask$2.call(AsyncTask.java:287)
03-24 18:51:54.525: W/System.err(12673):    at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
03-24 18:51:54.525: W/System.err(12673):    at java.util.concurrent.FutureTask.run(FutureTask.java:137)
03-24 18:51:54.525: W/System.err(12673):    at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
03-24 18:51:54.533: W/System.err(12673):    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
03-24 18:51:54.533: W/System.err(12673):    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
03-24 18:51:54.541: W/System.err(12673):    at java.lang.Thread.run(Thread.java:856)
zyonneo
  • 1,319
  • 5
  • 25
  • 63

1 Answers1

2

Geocoding api depends on a OS service, which needs to be running while you do the query. So devices that doesn't have service will throw the exception.

See The documentati here: Geocoder.

Additionally you can use the isPresent() method to determine, if the service is available or not for the particular device.

Have a look at my answer here: Geocoder's isPresent() method always returns false

In order to get it working, I would recommend to use the Geocoding api.

Community
  • 1
  • 1
Adil Soomro
  • 37,609
  • 9
  • 103
  • 153