简体   繁体   English

Android grpc执行失败

[英]Android grpc failed execption

Getting this error when trying to get location from latlong using Geocoder. 尝试使用Geocoder从latlong获取位置时收到此错误。

Error 错误

Caused by java.io.IOException: grpc failed at android.location.Geocoder.getFromLocation(Geocoder.java:136) at com.example.myApp.test.Fragments.DashboardFragment$sendLocationData$1.onSuccess(DashboardFragment.kt:676) at com.example.myApp.test.Fragments.DashboardFragment$sendLocationData$1.onSuccess(DashboardFragment.kt:81) at com.google.android.gms.tasks.zzj.run(Unknown Source) at android.os.Handler.handleCallback(Handler.java:751) at android.os.Handler.dispatchMessage(Handler.java:95) at android.os.Looper.loop(Looper.java:154) at android.app.ActivityThread.main(ActivityThread.java:6776) at java.lang.reflect.Method.invoke(Method.java) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1496) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1386) 由java.io.IOException引起:grpc在com.example.myApp.test.Fragments.DashboardFragment $ sendLocationData $ 1.onSuccess(DashboardFragment.kt:676)处的android.location.Geocoder.getFromLocation(Geocoder.java:136)处失败com.example.myApp.test.Fragments.DashboardFragment $ sendLocationData $ 1.onSuccess(DashboardFragment.kt:81)位于com.google.android.gms.tasks.zzj.run(未知源),位于android.os.Handler.handleCallback( Handler.java:751)位于android.os.Handler.dispatchMessage(Handler.java:95)位于android.os.Looper.loop(Looper.java:154)android.app.ActivityThread.main(ActivityThread.java:6776) )的com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:1496)的java.lang.reflect.Method.invoke(Method.java)(com.android.internal.os.ZygoteInit.main( ZygoteInit.java:1386)

Code

    punch_button?.setOnClickListener {
    createLocationRequest()
}


protected fun createLocationRequest() {
    mLocationRequest = LocationRequest()
    mLocationRequest?.interval = 10
    mLocationRequest?.fastestInterval = 50
    mLocationRequest?.priority = LocationRequest.PRIORITY_HIGH_ACCURACY
    val builder = LocationSettingsRequest.Builder()
            .addLocationRequest(mLocationRequest!!)

    mLocationCallback = object : LocationCallback() {
        override fun onLocationResult(p0: LocationResult?) {
            super.onLocationResult(p0)
            mCurrentLocation = p0?.lastLocation
        }

    }

    val client = LocationServices.getSettingsClient(context)
    val task = client.checkLocationSettings(builder.build())



    task.addOnSuccessListener(OnSuccessListener<LocationSettingsResponse> {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            askForPermission();
        }
    })  }



    private fun askForPermission() {
    if (ContextCompat.checkSelfPermission(context, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
        if (ActivityCompat.shouldShowRequestPermissionRationale(activity, Manifest.permission.ACCESS_FINE_LOCATION)) {
            requestPermissions(arrayOf(Manifest.permission.ACCESS_FINE_LOCATION), 123);
        } else {
            requestPermissions(arrayOf(Manifest.permission.ACCESS_FINE_LOCATION), 123);
        }
    } else {
        sendLocationData()
    }
}


    @SuppressLint("MissingPermission")
fun sendLocationData() {
try{
    Log.e("lat", mCurrentLocation?.latitude.toString())
    //mCurrentLocation?.latitude !=null && mCurrentLocation?.latitude !=null
    mFusedLocationClient.requestLocationUpdates(mLocationRequest, mLocationCallback, null)

    mFusedLocationClient.lastLocation.addOnSuccessListener({ location ->
        if (location != null) {
            val address: List<Address> = geocoder?.getFromLocation(location.latitude, location.longitude, 1)!!
        }
    })

}catch(e:Exception){
  Log.e("tag","error")
}
}

This is a know bug reported to Google, but unfortunately not solved yet. 这是报告给Google的已知错误,但不幸的是尚未解决。 This bug happens for real devices and emulators. 此错误发生在实际的设备和仿真器上。 You can see the thread about this issue here: 您可以在此处查看有关此问题的主题:

https://issuetracker.google.com/issues/64418751 https://issuetracker.google.com/issues/64418751

https://issuetracker.google.com/issues/64247769 https://issuetracker.google.com/issues/64247769

A workround to try solved this bug in your case is try to use Geocoding API web service: https://github.com/googlemaps/google-maps-services-java 解决您所遇到的错误的一种方法是尝试使用Geocoding API网络服务: https : //github.com/googlemaps/google-maps-services-java

Or you can try just catch the except and handle the exception like this: 或者,您可以尝试捕获异常并按如下方式处理异常:

try{
geocoder = new Geocoder(this, Locale.getDefault())
   // ... your code that throws the exception here
}catch(e: IOException){
   Log.e("Error", "grpc failed: " + e.message, e)
   // ... retry again your code that throws the exeception
}

I faced this issue while getting Addresses List using geocoder.getFromLocation() method, the problem is that getting addresses list from latitude,longitude takes time or on some slow devices it takes more time and, also sometimes it give me the java-io-ioexception-grpc-failed. 我在使用geocoder.getFromLocation()方法获取地址列表时遇到了这个问题,问题是从纬度,经度获取地址列表会花费时间,或者在一些慢速的设备上会花费更多的时间,有时它还会给我java-io- ioexception-grpc失败。

I fix this problem using Rx Java. 我使用Rx Java解决了这个问题。

public class AsyncGeocoder {

private final Geocoder geocoder;

public AsyncGeocoder(Context context) {
    geocoder = new Geocoder(context);
}

public Disposable reverseGeocode(double lat, double lng, Callback callback) {
    return Observable.fromCallable(() -> {
        try {
            return geocoder.getFromLocation(lat, lng, 1);
        } catch (Exception e) {
            AppLogger.d("throwable,", new Gson().toJson(e));
            e.printStackTrace();
        }
        return false;
    }).subscribeOn(Schedulers.io())
            .observeOn(AndroidSchedulers.mainThread())
            .subscribe(result -> {
                //Use result for something
                AppLogger.d("throwable,", new Gson().toJson(result));

                callback.success((Address) ((ArrayList) result).get(0));
            }, throwable -> AppLogger.d("throwable,", new Gson().toJson(throwable)));
}

public interface Callback {
    void success(Address address);

    void failure(Throwable e);
}
}

Calling Position 通话位置

mViewModel.getLocation(asyncGeocoder, getLat(), getLng(), this);

ViewModel Method ViewModel方法

public void getLocation(AsyncGeocoder geocoder, Double lat, Double lng, AsyncGeocoder.Callback callback) {
        getCompositeDisposable().add(geocoder.reverseGeocode(lat, lng, callback));
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM