问题android.location.geo codeR问题、android、location、geo

2023-09-12 10:03:59 作者:倾一池温柔

我知道有已经有很多网站上的有关问题 IOExeption :服务不可用.....我检查了所有的答案我能找到这个问题......我正在为异步任务这似乎是工作正常,但仍然得到了同样的异常。

我已经使用是present()在previous尝试,虽然它不是在以下code和我使用的是同一个电话。我有上网权限。我试图改变目标为谷歌API 来看看如果是这样的问题,但它并没有区别。

这是第四年项目的重要组成部分,因此任何帮助将是严重的。 PS从来没有使用Java或之前最近的android所以请原谅所有新秀寻找编码TY ..

  @覆盖
保护无效的onCreate(包savedInstanceState){
    super.onCreate(savedInstanceState);
    的setContentView(R.layout.main);

    厂景=(TextView中)findViewById(R.id.textView1);
    视图2 =(TextView中)findViewById(R.id.textView2);
    VIEW3 =(TextView中)findViewById(R.id.textView4);
    B1 =(按钮)findViewById(R.id.button1);

    locationManager =
            (LocationManager)this.getSystemService(Context.LOCATION_SERVICE);

    //提供商=
      // locationManager.getProvider(LocationManager.GPS_PROVIDER);

    最后LocationListener的监听器=新LocationListener的(){

        @覆盖
        公共无效onLocationChanged(位置定位){
            view2.setText(Double.toString(location.getLatitude()));
            view3.setText(Double.toString(location.getLongitude()));
            双纬度= Double.valueOf(location.getLatitude());
            双longTemp = Double.valueOf(location.getLatitude());
            新的地缘()执行(纬度,longTemp)。
            }

        @覆盖
        公共无效onProviderDisabled(字符串提供商){
            // TODO自动生成方法存根

        }

        @覆盖
        公共无效onProviderEnabled(字符串提供商){
            // TODO自动生成方法存根

        }

        @覆盖
        公共无效onStatusChanged(字符串商,INT地位,
                捆绑演员){
            // TODO自动生成方法存根

        }

    };
    b1.setOnClickListener(新View.OnClickListener(){

        @覆盖
        公共无效的onClick(视图v){
            // TODO自动生成方法存根
            locReqest();
        }

        私人无效locReqest(){
            locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,10000,0,监听器);
        }
    });

}

一流的地缘扩展的AsyncTask<双人间,太虚,字符串> {
    地理codeR地理codeR =新的地缘codeR(getApplicationContext(),Locale.getDefault());
    @覆盖
    保护字符串doInBackground(双人间... PARAMS){
           地址地址= NULL;
           名单<地址>地址= NULL;
            尝试 {
                //通过传递的纬度/经度值调用同步getFromLocation()方法。
                地址=地理coder.getFromLocation(PARAMS [0] .doubleValue(),则params [1] .doubleValue(),1);
                地址= addresses.get(0);
                如果(地址!= NULL){
                    。返回有你的地址:+ address.getCountryName()的toString();
                }
            }赶上(IOException异常E){
                e.printStackTrace();
                返回失败;
            }
            返回不及格;
    }
    @覆盖
    保护无效onPostExecute(字符串结果){
        // TODO自动生成方法存根
        super.onPostExecute(结果);
        //view1.setText(result);
        Toast.makeText(getApplicationContext()的地址是:+结果,Toast.LENGTH_LONG).show();
    }


}
 

继承人的logcat

  12-16 23:06:53.855:W / System.err的(23578):java.io.IOException异常:服务不可用
12-16 23:06:53.865:W / System.err的(23578):在android.location.Geo coder.getFromLocation(GEO coder.java:136)
12-16 23:06:53.865:W / System.err的(23578):在com.boggerTech.local.Main $ geo.doInBackground(Main.java:102)
12-16 23:06:53.880:W / System.err的(23578):在com.boggerTech.local.Main $ geo.doInBackground(Main.java:1)
12-16 23:06:53.900:W / System.err的(23578):在android.os.AsyncTask $ 2.call(AsyncTask.java:264)
12-16 23:06:53.900:W / System.err的(23578):在java.util.concurrent.FutureTask中$ Sync.innerRun(FutureTask.java:305)
12-16 23:06:53.905:W / System.err的(23578):在java.util.concurrent.FutureTask.run(FutureTask.java:137)
12-16 23:06:53.915:W / System.err的(23578):在android.os.AsyncTask $ SerialExecutor $ 1.运行(AsyncTask.java:208)
12-16 23:06:53.915:W / System.err的(23578):在java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
12-16 23:06:53.915:W / System.err的(23578):在java.util.concurrent.ThreadPoolExecutor中的$ Worker.run(ThreadPoolExecutor.java:569)
12-16 23:06:53.920:W / System.err的(23578):在java.lang.Thread.run(Thread.java:856)
12-16 23:07:10.440:W / System.err的(23578):java.io.IOException异常:服务不可用
 
微信在Github开源了Hardcoder,对Android开发者有什么影响

解决方案

地理coder.is present()可以返回false在某些设备上。

如果返回true,你仍然有问题,你可以通过使用的在谷歌地理编码API

我做了一个独立于平台的地理codeR 库,可在的 github上和Maven中央

 相关性{
    编译com.github.doctoror.geo codeR:图书馆:1.0.0
}
 

修改:在previous答案包含一个简短的例子执行实施地理$ C $铬

I know there has been a lot of questions on the site about the IOExeption : service not available.....I have checked all the answers i could find on the subject... i'm running it as async task which seems to be working fine but still getting the same exception.

I have used isPresent() on previous attempts although it is not in the following code and i am using the same phone. i have the internet permission. i have tried to change target to google api to see if that was the problem but it made no difference.

This is a vital part of a forth year project so any help would be serious. ps never worked with java or android before very recently so forgive any rookie looking coding 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();
    }


}

heres the logcat

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() can return false on some devices.

If it returns true and you still have problems you can create your own Geocoder by using The Google Geocoding API

I made a platform-independent Geocoder library which is available at github and Maven Central

dependencies {
    compile 'com.github.doctoror.geocoder:library:1.0.0'
}

Edit: the previous answer contained a short example implementation of implementing Geocoder.