首页 > 解决方案 > Android grpc 执行失败

问题描述

尝试使用地理编码器从 latlong 获取位置时出现此错误。

错误

由 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) at com.google.android.gms.tasks.zzj.run(Unknown Source) at 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 ) 在 java.lang.reflect.Method.invoke(Method.java) 在 com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1496) 在 com.android.internal.os。ZygoteInit.main(ZygoteInit.java:1386)

代码

    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")
}
}

标签: javaandroidkotlingeolocationgoogle-geocoder

解决方案


这是向 Google 报告的已知错误,但遗憾的是尚未解决。这个错误发生在真实设备和模拟器上。您可以在此处查看有关此问题的主题:

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

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

在您的情况下尝试解决此错误的工作方法是尝试使用 Geocoding API Web 服务: https ://github.com/googlemaps/google-maps-services-java

或者您可以尝试捕获异常并像这样处理异常:

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
}

推荐阅读