我知道网站上有很多关于IOExeption
的问题:服务不可用.....我已经检查了我能找到的关于这个主题的所有答案......我正在按async task
运行它,它似乎工作正常,但仍然得到相同的异常。
我在以前的尝试中使用过isPresent()
,尽管它不在以下代码中,并且我使用的是同一部手机。 我有互联网权限。 我试图将目标更改为google api
,看看这是否是问题所在,但这没有区别。
这是第四年项目的重要组成部分,因此任何帮助都是认真的。 PS最近从未使用过Java或Android,所以请原谅任何看起来新手的编码ty。
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
view1 = (TextView) findViewById(R.id.textView1);
view2 = (TextView)findViewById(R.id.textView2);
view3 = (TextView)findViewById(R.id.textView4);
b1 = (Button) findViewById(R.id.button1);
locationManager =
(LocationManager) this.getSystemService(Context.LOCATION_SERVICE);
//provider =
// locationManager.getProvider(LocationManager.GPS_PROVIDER);
final LocationListener listener = new LocationListener() {
@Override
public void onLocationChanged(Location location) {
view2.setText(Double.toString(location.getLatitude()));
view3.setText(Double.toString(location.getLongitude()));
Double lat = Double.valueOf(location.getLatitude());
Double longTemp = Double.valueOf(location.getLatitude());
new geo().execute(lat,longTemp);
}
@Override
public void onProviderDisabled(String provider) {
// 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) {
// TODO Auto-generated method stub
}
};
b1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
locReqest();
}
private void locReqest() {
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 10000, 0, listener);
}
});
}
class geo extends AsyncTask<Double, Void, String>{
Geocoder geocoder = new Geocoder(getApplicationContext(), Locale.getDefault());
@Override
protected String doInBackground(Double... params) {
Address address = null;
List<Address> addresses = null;
try {
// Call the synchronous getFromLocation() method by passing in the lat/long values.
addresses = geocoder.getFromLocation(params[0].doubleValue(),params[1].doubleValue(), 1);
address = addresses.get(0);
if (address != null){
return "Got your address : " + address.getCountryName().toString();
}
} catch (IOException e) {
e.printStackTrace();
return "failed";
}
return"fail";
}
@Override
protected void onPostExecute(String result) {
// TODO Auto-generated method stub
super.onPostExecute(result);
//view1.setText(result);
Toast.makeText(getApplicationContext(), "The address is: " + result, Toast.LENGTH_LONG).show();
}
}
这是日志猫
12-16 23:06:53.855: W/System.err(23578): java.io.IOException: Service not Available
12-16 23:06:53.865: W/System.err(23578): at android.location.Geocoder.getFromLocation(Geocoder.java:136)
12-16 23:06:53.865: W/System.err(23578): at com.boggerTech.local.Main$geo.doInBackground(Main.java:102)
12-16 23:06:53.880: W/System.err(23578): at com.boggerTech.local.Main$geo.doInBackground(Main.java:1)
12-16 23:06:53.900: W/System.err(23578): at android.os.AsyncTask$2.call(AsyncTask.java:264)
12-16 23:06:53.900: W/System.err(23578): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
12-16 23:06:53.905: W/System.err(23578): at java.util.concurrent.FutureTask.run(FutureTask.java:137)
12-16 23:06:53.915: W/System.err(23578): at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:208)
12-16 23:06:53.915: W/System.err(23578): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
12-16 23:06:53.915: W/System.err(23578): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
12-16 23:06:53.920: W/System.err(23578): at java.lang.Thread.run(Thread.java:856)
12-16 23:07:10.440: W/System.err(23578): java.io.IOException: Service not Available
Geocoder.isPresent()
在某些设备上可能会返回 false。
如果它返回 true 并且您仍然遇到问题,则可以使用 Google 地理编码 API 创建自己的地理编码器
我制作了一个独立于平台的Geocoder
库,可以在github和Maven Central上找到。
dependencies {
compile 'com.github.doctoror.geocoder:library:[version]'
}
编辑:上一个答案包含一个实现地理编码器的简短示例实现。
解决此问题的最佳方法是打开 AVD 管理器,当您选择在哪个移动设备上运行时,它会出现在列表下方,只需清除其中的所有数据,让它重新启动,然后再次运行您的程序......这对我有用
通过在模拟器中登录我的谷歌帐户来解决它。