【问题标题】:Android Geocoder.getLocationFromName() throws IOException: Service not available on Device [duplicate]Android Geocoder.getLocationFromName() 抛出 IOException:服务在设备上不可用 [重复]
【发布时间】:2012-09-14 03:44:50
【问题描述】:

我在模拟器和手机(Android 4.1 的 Nexus S)上都使用 Geocoder.getLocationFromName() 方法,但出现以下异常:

java.io.IOException: Service not Available

在模拟器 (example) 上出现了很多关于此的问题,其中大多数都说是特定版本的模拟器有问题。但是,异常出现在我的 AVD(2.3 和 4.1)和手机上。

我的手机和 AVD 都有互联网连接。我的 API 版本是 16(android 4.1),但我也尝试过使用旧版本。两种 AVD 都包含 Google API。

有什么想法吗?

这是相关代码sn -p:

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

这是我的清单:

<?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>

【问题讨论】:

标签: android geolocation geocoding


【解决方案1】:

确保您的应用程序的构建目标设置为 Google APIs 之一,而不仅仅是 Android 4.0(或类似)。您可以在 Eclipse 中通过右键单击您的项目并选择“Properties”然后选择“Android”来更改此设置。或者,您可以简单地将project.properties 文件编辑为如下内容:

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

原因是 Geocoder 类存在于核心 Android 框架中,但依赖于 Google API 贡献的代码才能正常运行。即使您的 AVD 包含 Google API,您的项目仍需要针对该特定构建目标进行构建。

【讨论】:

  • 感谢您的回答。但正如我已经说过的,我正在使用 Google APIs AVDs。我的手机也有同样的问题。
  • 嘿 @MarkosFragkakis,您说您在 AVD 中使用 Google API,但是您的项目的构建目标是否也设置为相同的 Google API 级别?这是两种不同的设置。
  • 你是对的。我将目标更改为您建议的目标(通过 Eclipse)。但是,现在我得到一个“java.io.IOException:无法解析来自服务器的响应”。我认为这是一个不同的问题,所以我会将您的答案标记为正确答案。但是,如果您有任何建议,我们非常欢迎。谢谢。
  • 我到处寻找答案,结果就是这么简单。感谢 Markos Fragkakis 提出的问题,并感谢 twaddington 的回答解决了我的问题。
  • 有效! @twaddington 你救了我的命。
猜你喜欢
  • 1970-01-01
  • 2020-07-24
  • 2023-03-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-09-24
  • 2011-10-29
相关资源
最近更新 更多