When I use call getFromLocationName I get an IOException with description \"grpc failed\".
Code that\'s ran
@Override
public void onMapReady(GoogleMa
SomeTimes Geocoder failed when latitude and longitude contain above 3 decimal places, or it may be said that Geocoder can not decode every latitude and longitude. you need to limit latitude and longitude upto 3 decimal places. complete code
DecimalFormat df = new DecimalFormat();
df.setMaximumFractionDigits(3);
double lat = Double.parseDouble(df.format(currentUserLocation.latitude));
double lon =
Double.parseDouble(df.format(currentUserLocation.longitude));
Geocoder geocoder = new Geocoder(myContext, Locale.getDefault());
List<Address> addresses = geocoder.getFromLocation(lat,lon, 1);
String cityName = addresses.get(0).getLocality();
Although pretty late on the issue, here is an answer for Kotlin users.
I stumbled upon this java.io.IOException: grpc failed
error whilst using Geocoder's getFromLocation
function. I have been using a Google Pixel 2 as my test device (running Android 9).
There are 3 things to make sure of:
Geocoder
needs to be done apart from the main thread of your app.
To support my claim, here is a quote from the Android documentation: "The method is synchronous and may take a long time to do its work, so you should not call it from the main, user interface (UI) thread of your app."Now, it is best to use IntentService
for this task like the official documentation has given an example of. However, this person tried it with an AsyncTask in Java and it worked just fine (note that their task with geocoder
was a lot less intensive). Therefore, I highly recommend using Anko's doAsync
method and implement any code regarding Geocoder
within it.
If possible, use a physical device instead of an emulator. The emulator on Android Studio gave me a lot of trouble and was not giving me the error information I needed to really debug the problem.
Reinstall your app on your physical device and Invalidate Caches/Restart your project several times.
Also make sure you have good network connection and it is enabled on your device.
Like Google writes, You should not call this method on UI thread
https://developer.android.com/training/location/display-address#java
I fixed this issue by running this operation in a new Thread
, and then if I need to update some UI view I'll do it on runOnUiThread
It works for me if introduce Geocoder as singleton for application rather than create it every time. Make sure to call Geocoder.getFromLocationName() from worker thread.
For example:
class Application {
private val geoCoder = Geocoder(context, Locale.getDefault())
...
}
class SomeClass {
@WorkerThread
fun doSmth(){
application.geoCoder.getFromLocationName("London", 10)
...
}
}
In Wifi connection worked fine but in 3g or 4g connection is fail.
Using proxy
As of August 2019, I still get this from time to time when I'm snooping my own http traffic with a proxy. Turn it off and problem gone.