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

ray*_*man 15 android

使用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
Run Code Online (Sandbox Code Playgroud)

那是我的代码:

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

        }
    };
}
Run Code Online (Sandbox Code Playgroud)

有谁有任何想法?

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

谢谢.

谢谢,雷.

dde*_*ele 21

这是模拟器的已知问题.它在实际设备上工作正常

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

java.io.IOException: Service not Available
 at android.location.Geocoder.getFromLocation(Geocoder.java:117)
Run Code Online (Sandbox Code Playgroud)

有关详细信息,请参阅此处(以及可能的解决方法),请参阅以下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) 
Run Code Online (Sandbox Code Playgroud)

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

如果GeoCoder返回空列表,则需要检查设备(仿真器或真实电话)上是否有适当的GeoCoder实现.

这可以使用Geocoder对象上的isPresent()方法完成.

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

此外,在模拟器上运行时,请确保使用Google API设置AVD图像.


小智 10

您可以通过以下方式使用 Google Place API

创建一个方法,返回JSONObject带有HTTP调用响应的方法,如下所示

public static JSONObject getLocationInfo(String address) {
    StringBuilder stringBuilder = new StringBuilder();
    try {

    address = address.replaceAll(" ","%20");    

    HttpPost httppost = new HttpPost("http://maps.google.com/maps/api/geocode/json?address=" + address + "&sensor=false");
    HttpClient client = new DefaultHttpClient();
    HttpResponse response;
    stringBuilder = new StringBuilder();


        response = client.execute(httppost);
        HttpEntity entity = response.getEntity();
        InputStream stream = entity.getContent();
        int b;
        while ((b = stream.read()) != -1) {
            stringBuilder.append((char) b);
        }
    } catch (ClientProtocolException e) {
    } catch (IOException e) {
    }

    JSONObject jsonObject = new JSONObject();
    try {
        jsonObject = new JSONObject(stringBuilder.toString());
    } catch (JSONException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    return jsonObject;
}
Run Code Online (Sandbox Code Playgroud)

现在将JSONObject传递给getLatLong()方法,如下所示

public static GeoPoint  getLatLong(JSONObject jsonObject) {

        Double lon = new Double(0);
        Double lat = new Double(0);

        try {

            lon = ((JSONArray)jsonObject.get("results")).getJSONObject(0)
                .getJSONObject("geometry").getJSONObject("location")
                .getDouble("lng");

            lat = ((JSONArray)jsonObject.get("results")).getJSONObject(0)
                .getJSONObject("geometry").getJSONObject("location")
                .getDouble("lat");

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

        }

        return new GeoPoint((int) (lat * 1E6), (int) (lon * 1E6));
    }
Run Code Online (Sandbox Code Playgroud)

这是工作和测试...在API级别8 ...跳这个帮助..