6

I am using the Geocoder.getLocationFromName() method on both the emulator and my phone (Nexus S with Android 4.1) and I get the following exception:

java.io.IOException: Service not Available

There are a number of questions about this appearing on the emulator (example), and most of them say that it is a specific version of the emulator that has the problem. However, the exception appears both on my AVDs (2.3 and 4.1) and my phone.

Both my phone and the AVDs have internet connection. My API version is 16 (android 4.1), but I also tried with older ones. Both AVDs include Google APIs.

Any ideas what is going on here?

This is the relevant code snippet:

Geocoder myGeocoder = new Geocoder(getApplicationContext(), Locale.getDefault());
List<Address> newAddresses = myGeocoder.getFromLocationName(arg0.toString(), 10);

And this is my manifest:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.asdasd" android:versionCode="6" android:versionName="1.3.1">
    <uses-sdk android:minSdkVersion="16" />
    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
    <application android:icon="@drawable/ic_launcher_asdasd"
        android:label="@string/app_name">
        <activity android:name=".AsdasdActivity" android:label="@string/app_name"
            android:screenOrientation="portrait" android:windowSoftInputMode="stateHidden">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity android:name="com.google.ads.AdActivity"
            android:configChanges="keyboard|keyboardHidden|orientation" />
    </application>
</manifest>
Community
  • 1
  • 1
Markos Fragkakis
  • 7,499
  • 18
  • 65
  • 103

1 Answers1

7

Make sure your application's build target is set to one of the Google APIs and not simply Android 4.0 (or similar). You can change this setting in Eclipse by right-clicking on your project and selecting "Properties" then "Android." Alternatively, you can simply edit the project.properties file to something like the following:

# Project target
target=Google Inc.:Google APIs:16

The reason is that the Geocoder class is present in the core Android framework, but depends on code contributed by the Google APIs to function properly. Even if your AVD includes the Google APIs, your project still needs to be built against that specific build target.

twaddington
  • 11,607
  • 5
  • 35
  • 46
  • Thanks for the answer. But as I have already said, I am using Google APIs AVDs. And I also have the same problem on my phone. – Markos Fragkakis Sep 23 '12 at 20:18
  • Hey @MarkosFragkakis, you said you're using the Google APIs in your AVD, but is your project's build target also set to the same Google API level? These are two different settings. – twaddington Sep 23 '12 at 21:21
  • 1
    You are right. I changed the target to the one you suggested (through Eclipse). However, now I get a "java.io.IOException: Unable to parse response from server". I assume this is a different problem, so I will mark your answer as the correct one. However, if you have any suggestions, they are more than welcome. Thanks. – Markos Fragkakis Sep 26 '12 at 15:31
  • 2
    I searched everywhere for the answer and in the end it was this simple. Thanks Markos Fragkakis,for the question and thanks the twaddington for the answer that solved my issue. – Gafanha Dec 17 '12 at 13:54
  • So if I have the error on my app and I edit the project.properties file to Google APIs:16, the problem should disapear ? – mrroboaat Sep 03 '13 at 15:55