在android中我在Geocoder中获得IOException

时间:2013-06-03 05:51:09

标签: android

如果网络不可用或发生任何其他I / O问题,则抛出IOException。它需要互联网才能使用Geocoder。

   public class MainActivity extends Activity {

   double LATITUDE = 37.42233;
   double LONGITUDE = -122.083;

   /** Called when the activity is first created. */
   @Override
    public void onCreate(Bundle savedInstanceState) {
   super.onCreate(savedInstanceState);
   setContentView(R.layout.activity_main);
   TextView myLatitude = (TextView)findViewById(R.id.mylatitude);
   TextView myLongitude = (TextView)findViewById(R.id.mylongitude);
   TextView myAddress = (TextView)findViewById(R.id.myaddress);

   myLatitude.setText("Latitude: " + String.valueOf(LATITUDE));
   myLongitude.setText("Longitude: " + String.valueOf(LONGITUDE));

   Geocoder geocoder = new Geocoder(this, Locale.ENGLISH);

   try {
   List<Address> addresses = geocoder.getFromLocation(LATITUDE, LONGITUDE, 1);

   if(addresses != null) {
   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");
   }
  myAddress.setText(strReturnedAddress.toString());
  }
 else{
 myAddress.setText("No Address returned!");
 }
 } catch (IOException e) {
 e.printStackTrace();
myAddress.setText("Cannot get Address!");//and tell me what is 
}

 }
}

任何人都有任何想法?

我已经设法使用Emulator 2.1 api 8,但是反向地理编码总是给出一个空的结果。有人可以确认我的代码吗?

log cat

      06-03 10:59:24.689: W/System.err(4129):java.io.IOException: Service not Available
                       :at android.location.Geocoder.getFromLocation(Geocoder.java:136)

                   :at com.example.gprsonandoff.MainActivity.city(MainActivity.java:74)

                :at com.example.gprsonandoff.MainActivity.onCreate(MainActivity.java:44)
           :android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)

         :at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)

2 个答案:

答案 0 :(得分:8)

这似乎是许多人面临的一个问题。检查this

如果使用Geocoder,即使有互联网连接,也可以尝试使用GeoCoding APIs

答案 1 :(得分:0)

我重启了我的Android设备,错误消失了。