6

I know there has been a lot of questions on the site about the IOExeption : service not available.....I have checked all the answers i could find on the subject... i'm running it as async task which seems to be working fine but still getting the same exception.

I have used isPresent() on previous attempts although it is not in the following code and i am using the same phone. i have the internet permission. i have tried to change target to google api to see if that was the problem but it made no difference.

This is a vital part of a forth year project so any help would be serious. ps never worked with java or android before very recently so forgive any rookie looking coding ty..

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    view1 = (TextView) findViewById(R.id.textView1);
    view2 = (TextView)findViewById(R.id.textView2);
    view3 = (TextView)findViewById(R.id.textView4);
    b1 = (Button) findViewById(R.id.button1);

    locationManager =
            (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);

    //provider =
      //      locationManager.getProvider(LocationManager.GPS_PROVIDER);

    final LocationListener listener = new LocationListener() {

        @Override
        public void onLocationChanged(Location location) {
            view2.setText(Double.toString(location.getLatitude()));
            view3.setText(Double.toString(location.getLongitude()));
            Double lat = Double.valueOf(location.getLatitude());
            Double longTemp = Double.valueOf(location.getLatitude());
            new geo().execute(lat,longTemp);
            }

        @Override
        public void onProviderDisabled(String provider) {
            // TODO Auto-generated method stub

        }

        @Override
        public void onProviderEnabled(String provider) {
            // TODO Auto-generated method stub

        }

        @Override
        public void onStatusChanged(String provider, int status,
                Bundle extras) {
            // TODO Auto-generated method stub

        }

    };
    b1.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            locReqest();
        }

        private void locReqest() {
            locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 10000, 0, listener);
        }
    });

}

class geo extends AsyncTask<Double, Void, String>{
    Geocoder geocoder = new Geocoder(getApplicationContext(), Locale.getDefault());
    @Override
    protected String doInBackground(Double... params) {
           Address address = null;
           List<Address> addresses = null;
            try {
                // Call the synchronous getFromLocation() method by passing in the lat/long values.
                addresses = geocoder.getFromLocation(params[0].doubleValue(),params[1].doubleValue(), 1);
                address = addresses.get(0);
                if (address != null){
                    return "Got your address : " + address.getCountryName().toString();
                }
            } catch (IOException e) {
                e.printStackTrace();
                return "failed";
            }
            return"fail";
    }
    @Override
    protected void onPostExecute(String result) {
        // TODO Auto-generated method stub
        super.onPostExecute(result);
        //view1.setText(result);
        Toast.makeText(getApplicationContext(), "The address is: " + result, Toast.LENGTH_LONG).show();
    }


}

heres the logcat

12-16 23:06:53.855: W/System.err(23578): java.io.IOException: Service not Available
12-16 23:06:53.865: W/System.err(23578):    at android.location.Geocoder.getFromLocation(Geocoder.java:136)
12-16 23:06:53.865: W/System.err(23578):    at com.boggerTech.local.Main$geo.doInBackground(Main.java:102)
12-16 23:06:53.880: W/System.err(23578):    at com.boggerTech.local.Main$geo.doInBackground(Main.java:1)
12-16 23:06:53.900: W/System.err(23578):    at android.os.AsyncTask$2.call(AsyncTask.java:264)
12-16 23:06:53.900: W/System.err(23578):    at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
12-16 23:06:53.905: W/System.err(23578):    at java.util.concurrent.FutureTask.run(FutureTask.java:137)
12-16 23:06:53.915: W/System.err(23578):    at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:208)
12-16 23:06:53.915: W/System.err(23578):    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
12-16 23:06:53.915: W/System.err(23578):    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
12-16 23:06:53.920: W/System.err(23578):    at java.lang.Thread.run(Thread.java:856)
12-16 23:07:10.440: W/System.err(23578): java.io.IOException: Service not Available
Vikalp Patel
  • 10,669
  • 6
  • 61
  • 96
  • Please post the LogCat errors, so we can see what is happening. – Sam Dec 16 '12 at 23:10
  • i have also come across suggestion that the local.getDefault could be a problem????? –  Dec 16 '12 at 23:10
  • Ok, `getFromLocation()` will throw [this exception](http://developer.android.com/reference/android/location/Geocoder.html#getFromLocation%28double,%20double,%20int%29) when the Network isn't active. Have you verified that you are connect to mobile data, wifi, or anything else? – Sam Dec 16 '12 at 23:48
  • ye the phones definitely connected to wifi...do i need any other permissions bar access internet. ty for the help man –  Dec 16 '12 at 23:58
  • I tested your code and it works for me... (Well, the country was wrong but I got data.) Are you running this on a real device or an emulator? – Sam Dec 17 '12 at 00:22
  • :) it works best news ive heard all day (bar wrong country) .... im running it on galaxy s2 android 4.0.4...if this turns out to be something stupid im missing my apologies man. –  Dec 17 '12 at 00:31

3 Answers3

12

Geocoder.isPresent() can return false on some devices.

If it returns true and you still have problems you can create your own Geocoder by using The Google Geocoding API

I made a platform-independent Geocoder library which is available at github and Maven Central

dependencies {
    compile 'com.github.doctoror.geocoder:library:[version]'
}

Edit: the previous answer contained a short example implementation of implementing Geocoder.

Yaroslav Mytkalyk
  • 16,950
  • 10
  • 72
  • 99
  • Only seeing this now doctoror, its been a while since I was at the geocoding but ill sit down with this and run through it as best I can tomorrow. I'll probably have a question or two for you then if your around :) thanks for the help –  Mar 03 '13 at 11:48
  • sorry man if you see this could you point me in the direction for the best documentation on this implementation of geocoding .. ty –  Mar 04 '13 at 13:53
  • @Shane When I wrote this I refered to https://developers.google.com/maps/documentation/geocoding/ – Yaroslav Mytkalyk Mar 04 '13 at 14:03
  • came across that in the search.... its awful pity the geocoder doesnt do the job far easier to implement for newbies such as myself thanks again for the help man. –  Mar 06 '13 at 17:22
  • Hi Doctoror Drive, could you give some information about WebserviceClient.download(); Thanks. – realuser Apr 01 '13 at 14:47
  • @realuser it just downlaods data from the server to byte array. http://stackoverflow.com/questions/2436385/android-getting-from-a-uri-to-an-inputstream-to-a-byte-array – Yaroslav Mytkalyk Apr 01 '13 at 14:48
  • Updated Geocoder code. I recently found that status OVER_QUERY_LIMIT can be returned if there are too much calls per second, and the service will be usable again in two seconds. – Yaroslav Mytkalyk May 04 '13 at 10:32
  • Good point about the limit. Also, if the call is made from a mobile, it should have the sensor=false parameter set to TRUE. – shkschneider Mar 27 '14 at 17:08
  • It shows me red line under WebserviceClient.download(url.toString()); @DoctororDrive – Ankit Sharma Sep 30 '14 at 10:44
  • @AnkitSharma the question about WebserviceClient.download was already mentioned int the previous comments. All it does is downloads the data to a byte array. I did not include this code since any app that works with network probably has it's own implementation. I provided a link to a question that has phew answers to create implementation from. But since I had this question for the second time, I added the code to the answer. – Yaroslav Mytkalyk Sep 30 '14 at 11:32
  • @YaroslavMytkalyk I use your library with version 1.3.0 but it always return null as result. Any ideas what I've missed? The 'normal' geocoder works great but I want to support other devices too. – Cilenco Nov 12 '17 at 13:11
  • My library should not return null from `getFromLocation` or `getFromLocationName`. Please open a new issue on the library github page and explain what method returns null and under what conditions. – Yaroslav Mytkalyk Nov 13 '17 at 15:45
0

The best solution to this problem is to just to Open the AVD Manager which is present under the list when you select which mobile device you run on and just clear all data in it let it reboot and then run your program again...this worked for me

Sai
  • 1
0

Solved it by signing into my google account in the emulator.