gpt4 book ai didi

android - Geocoder.getFromLocation 在 Android 模拟器上抛出 IOException

转载 作者:可可西里 更新时间:2023-11-01 18:50:22 27 4
gpt4 key购买 nike

使用 Android 模拟器 2.2 api 8我不断收到 IOException

03-05 19:42:11.073: DEBUG/SntpClient(58): request time failed: java.net.SocketException: Address family not supported by protocol
03-05 19:42:15.505: WARN/System.err(1823): java.io.IOException: Service not Available

那是我的代码:

private LocationManager manager = null;
LocationListener locationListener = null;
double latitude = 0;
double longtitude = 0;
List<Address> myList = null;

public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// httpTranslateGet();
try
{


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


initLocationListener();

manager.requestLocationUpdates(manager.GPS_PROVIDER, 0, 0,
locationListener);

}
catch (Exception e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}

}

private void initLocationListener()
{
locationListener = new LocationListener()
{

@Override
public void onLocationChanged(android.location.Location location)
{
if (location != null)
{

latitude = location.getLatitude();
longtitude = location.getLongitude();


try
{
Geocoder geocoder = new Geocoder(WeatherCastDemo.this, Locale.getDefault());
List<Address> addresses = geocoder.getFromLocation(location.getLatitude(), location.getLongitude(), 1);
myList = geocoder.getFromLocation(
location.getLatitude(),
location.getLongitude(), 10);

StringBuilder sb = new StringBuilder();
if (myList.size() > 0)
{
Address address = myList.get(0);

for (int i = 0; i < address
.getMaxAddressLineIndex(); i++)
sb.append(address.getAddressLine(i)).append(
"\n");

sb.append(address.getLocality()).append("\n");
sb.append(address.getPostalCode()).append("\n");
sb.append(address.getCountryName());

}
}

catch (IOException e)
{
e.printStackTrace();
}

}

}

@Override
public void onProviderDisabled(String arg0)
{
// 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)
{

}
};
}

有人知道吗?

我已经设法用 Emulator 2.1 api 7 做到了,但是反向地理编码总是给出空结果。谁能确认我的代码?

谢谢。

谢谢,射线。

最佳答案

这是模拟器的已知问题。它在实际设备上运行良好

在 2.2 API 8 上,您将收到以下堆栈跟踪信息

java.io.IOException: Service not Available
at android.location.Geocoder.getFromLocation(Geocoder.java:117)

有关详细信息(和可能的解决方法),请参阅此处,请参阅以下 URL:

http://code.google.com/p/android/issues/detail?id=8816

如果您在较低的 API 上使用 GeoCoder 时遇到问题,您应该检查堆栈跟踪。我不时有以下情况:

java.io.IOException: Unable to parse response from server
at android.location.Geocoder.getFromLocation(Geocoder.java:124)

这可以是 Google 服务器端的任何问题,也可以是客户端(互联网连接)的问题。

如果 GeoCoder 返回一个空列表,您需要检查您的设备(模拟器或真实手机)上是否有可用的正确 GeoCoder 实现。

这可以在 Geocoder 对象上使用 isPresent() 方法来完成。

http://developer.android.com/reference/android/location/Geocoder.html

此外,在模拟器上运行时,请确保您的 AVD 镜像已使用 Google API 进行设置。

关于android - Geocoder.getFromLocation 在 Android 模拟器上抛出 IOException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5205650/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com