Geocoder geocode = new Geocoder(MainPage.this);
List<Address> address = null;
StringBuilder addressText = null;
try {
address = geocode.getFromLocation(latitudeTemp, longitudeTemp,
1);
} catch (IOException e) {
e.printStackTrace();
}
if (address != null && address.size() != 0) {
Address addr = address.get(0);
addressText = new StringBuilder();
if (addr.getCountryName() != null)
addressText.append(addr.getCountryName()).append(" ");
if (addr.getAdminArea() != null)
addressText.append(addr.getAdminArea()).append(" ");
if (addr.getLocality() != null)
addressText.append(addr.getLocality()).append(" ");
if (addr.getSubLocality() != null)
addressText.append(addr.getSubLocality()).append(" ");
if (addr.getThoroughfare() != null)
addressText.append(addr.getThoroughfare()).append(" ");
if (addr.getFeatureName() != null)
addressText.append(addr.getFeatureName()).append(" ");
} else {
addressText = new StringBuilder()
.append("error");
}
myAddress2 = addressText;
我正在尝试使用反向地理编码来获取实际地址。而且,实际上,上面的代码通常有效。
但是,有时它会给出 IOException,上面写着“等待服务器响应超时”,当我尝试使用单独的线程来处理代码时,设备有时会由于 IOException 冻结。
我应该怎么做才能避免异常?
请帮忙...