Home » Android » How to fix: “java.io.IOException: grpc failed” while using Geocoder.getFromLocation

How to fix: “java.io.IOException: grpc failed” while using Geocoder.getFromLocation

Have you ever seen a crash or error logs like below, when accessing location data in Android.

I Geocoder: Geocoder getFromLocation --- mService = android.location.ILocationManager$Stub$Proxy@4f84fe, ex = grpc failed, return results = []
D AndroidRuntime: Shutting down VM
E AndroidRuntime: FATAL EXCEPTION: main
E AndroidRuntime: Process: com.your.packagename, PID: 19452
E AndroidRuntime: java.lang.RuntimeException: java.lang.reflect.InvocationTargetException
E AndroidRuntime:        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:503)
E AndroidRuntime:        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)
E AndroidRuntime: Caused by: java.lang.reflect.InvocationTargetException
E AndroidRuntime:        at java.lang.reflect.Method.invoke(Native Method)
E AndroidRuntime:        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
E AndroidRuntime:        ... 1 more
E AndroidRuntime: Caused by: java.io.IOException: grpc failed
E AndroidRuntime:        at android.location.Geocoder.getFromLocation(Geocoder.java:190)

The code used for reverse geodecoding is normally look like as below,

fusedLocationClient!!.lastLocation
        .addOnSuccessListener { location : Location? ->
            // Got last known location. In some rare situations this can be null.
		mLocation = location
                if (mLocation != null) {
                    getCompleteReadableAddress()
                }
        }

    fun getCompleteReadableAddress() {
        val geocoder = Geocoder(this, Locale.getDefault())
        // Address found using the Geocoder.
        var addresses: List<Address> = emptyList()
        if (mLocation != null) {
            Log.d(TAG,"Last known Latitude :: " + mLocation!!.getLatitude() + " Longitude :: " + mLocation!!.getLongitude()
            )
            addresses = geocoder.getFromLocation(
                mLocation!!.getLatitude(),
                mLocation!!.getLongitude(),
                // In this sample, we get just a single address.
                1
            )
            if (!addresses.isEmpty()) {
                val address = addresses[0]
                Log.d(TAG, "CompleteReadableAddress: " + address)
                Log.d(TAG, "Admin area " + address.adminArea)
            }
        }
    }

The crash has happened while execution of function “geocoder.getFromLocation” which actually does the reverse geodecoding.

The possible cause of the failure of this function, might be unstable network connectivity which causes the reverse geodecoding failing. As reported in [1] and [2]

Solution:

  1. Make sure to check you are connected to network using network check API’s of Android
  2. Add “try catch” around “geocoder.getFromLocation” and display error message from “catch” block saying, address decoding failed retry etc.
  3. Use some default values if you are taking some actions based on users fetched address.

References – https://developer.android.com/training/location/display-address#kotlin


Subscribe our Rurban Life YouTube Channel.. "Rural Life, Urban LifeStyle"

Leave a Comment