地理coder.getFromLocation抛出IOException异常的Andr​​oid模拟器模拟器、抛出、异常、地理

2023-09-11 12:38:07 作者:取暖

使用Android模拟器2.2 API 8 我不断收到IOException异常

Using Android emulator 2.2 api 8 I keep getting 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

这就是我的code:

Thats my code:

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)
        {

        }
    };
}

任何人有任何想法?

anyone has any idea?

我设法用模拟器2.1 API 7做到这一点,但随后的反向地理编码总是给一个空的结果。任何人都可以证实我的code?

I have managed to do it with Emulator 2.1 api 7, but then the reverse geocoding always give an empty result. anyone could confirm my code?

感谢。

谢谢, 射线。

推荐答案

这是一个已知问题与模拟器。 它工作正常,在实际设备上

This is a know issue with the Emulator. It works fine on an actual device

在2.2 API 8,你会收到以下堆栈跟踪

On 2.2 API 8 you'll receive the following stacktrace

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

在这里看到更多的信息(以及可能的解决方法)请参阅以下网址:

See here for more info (and a possible workaround) see the following URL :

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

如果您在使用地理codeR较低API的问题,您应该检查堆栈跟踪。不时,我有以下几点:

If you're having issues using the GeoCoder on lower APIs you should check the stacktrace. From time to time I'm having the following :

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

这可以是一个服务器端的问题,任何事情在谷歌,或在客户端(Internet连接)的问题。

This can be anything from a server-side issue at Google, or an issue on the client (internet connection).

如果地缘codeR返回一个空列表,你需要检查,如果你有一个正确的地理codeR执行现有的设备(模拟器或真实的电话)上。

If the GeoCoder returns an empty list, you need to check if you have a proper GeoCoder implementation available on the device (emulator or real phone).

这可以使用is present()方法的地理codeR对象上完成。

This can be done using the isPresent() method on the Geocoder object.

http://developer.android.com/reference/android/location/Geo$c$cr.html

此外,在仿真器上运行时,请确保您的AVD形象是建立与谷歌的API。

Also, when running on an emulator, make sure your AVD image is setup with the Google APIs.