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.

Related :   How to center views in LinearLayout and RelativeLayout ?

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


lynxbee_ezoic

Subscribe with Valid Email Id to receive updates in Inbox. ( Secured by Google FeedBurner )

   


Leave a Comment

Android Android Applications Android Build system Android Commands Application Libraries Application Stack / User Interface Bash / Shell Scripts Bluetooth driver Cloud Technologies Commands and Packages Compilation Content Management System Core Kernel C Programs Development & Build Development, Debugging and Performance Tools Development Environment Setup Django & REST Api Errors & Failures Git Hardware Platforms HTML JAVA Programs Linux, OS Concepts and Networking Linux Device Drivers Linux Host, Ubuntu, SysAdmin Linux Kernel Linux Networking Middleware Libraries, HAL Multimedia Audio, Video, Images NDK / Middleware / HAL OS Concepts PHP Programming Languages Scripting and Automation Search Engine Optimisation ( SEO ) Social Media Source Code Management ( SCM ) System Administration, Security Testing and Debugging Uncategorized Web design and development Website Hosting Wordpress Yocto / Bitbake / Openembedded