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.
Related :   How to use adb shell when multiple devices are connected

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

Android Android Commands Android Java Applications Application Libraries Application Stack / User Interface Bash / Shell Scripts Commands and Packages Compilation Content Management System - CMS Core Kernel C Programs Development & Build Development Environment Setup Errors & Failures Flutter git Go Language Programs Hardware Platforms HTML JAVA Programs Kernel & Device Drivers Linux, OS Concepts and Networking Linux Device Drivers Linux Host, Ubuntu, SysAdmin Linux Kernel Linux Networking Middleware Libraries, HAL Monetization / Google AdSense Multimedia - Audio, Video, Images NDK / Middleware / HAL OS Concepts PHP Programming Languages RaspberryPi Scripting and Automation Search Engine Optimisation ( SEO ) Social Media Socurce Code Management ( SCM ) System Administration, Security Testing and Debugging Uncategorized User Interface Web design and development Wordpress Yocto / Bitbake / Openembedded

Leave a Reply / Ask Question