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

使用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
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
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

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

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

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

 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; } 

现在将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)); } 

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

我读了@ddewaele提到的讨论主题,有人说重启可以解决问题。 它做了。 顺便说一下,我的设备的Android版本是4.1。

纬度和长度是你各自的价值观

 Geocoder geocoder = new Geocoder(getBaseContext(), Locale.ENGLISH); try { List
addresses = geocoder.getFromLocation(latitude, longitude, 1); if (addresses.size() > 0) { Address returnedAddress = addresses.get(0); StringBuilder strReturnedAddress = new StringBuilder( "Address:\n"); for (int i = 0; i < returnedAddress .getMaxAddressLineIndex(); i++) { strReturnedAddress.append( returnedAddress.getAddressLine(i)).append("\n"); } adrs.setText(strReturnedAddress.toString()); } else { adrs.setText("No Address returned!"); } } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); }

上面的代码稍有变化,允许替换某些终端似乎不支持的getFromLocationName调用:

 private static List
getAddrByWeb(JSONObject jsonObject){ List
res = new ArrayList
(); try { JSONArray array = (JSONArray) jsonObject.get("results"); for (int i = 0; i < array.length(); i++) { Double lon = new Double(0); Double lat = new Double(0); String name = ""; try { lon = array.getJSONObject(i).getJSONObject("geometry").getJSONObject("location").getDouble("lng"); lat = array.getJSONObject(i).getJSONObject("geometry").getJSONObject("location").getDouble("lat"); name = array.getJSONObject(i).getString("formatted_address"); Address addr = new Address(Locale.getDefault()); addr.setLatitude(lat); addr.setLongitude(lon); addr.setAddressLine(0, name != null ? name : ""); res.add(addr); } catch (JSONException e) { e.printStackTrace(); } } } catch (JSONException e) { e.printStackTrace(); } return res; }

}

您可以使用Mr.cyclopes49给出的代码作为两个函数,并在onCreate()方法中将函数调用添加到getLatLong()getLocationInfo() ,它的工作原理是样例语句是

 JSONObject jo = this.getLocationInfo("vizianagaram"); GeoPoint p=this.getLatLong(jo); Toast.makeText(getBaseContext(), p.getLatitudeE6() / 1E6 + "," + p.getLongitudeE6() /1E6 , Toast.LENGTH_SHORT).show(); 

这很好用!