简体   繁体   中英

java.io.IOException: grpc failed

When I use call getFromLocationName I get an IOException with description "grpc failed".

Code that's ran

@Override
public void onMapReady(GoogleMap googleMap) {
    mMap = googleMap;
    try {
        Geocoder geocoder = new Geocoder(getApplicationContext(), Locale.getDefault());
        List<Address> listAdresses = geocoder.getFromLocationName("London", 10);
        Log.i("PlaceInfo", listAdresses.get(0).toString());
    } catch (IOException e) {
        e.printStackTrace();
    }
}

Error the console outputs:

07-10 12:01:38.781 13712-13712/co.siqve.maplocationdemo W/System.err: java.io.IOException: grpc failed
07-10 12:01:38.781 13712-13712/co.siqve.maplocationdemo W/System.err:     at android.location.Geocoder.getFromLocationName(Geocoder.java:178)
07-10 12:01:38.781 13712-13712/co.siqve.maplocationdemo W/System.err:     at co.siqve.maplocationdemo.MapsActivity.onMapReady(MapsActivity.java:70)
07-10 12:01:38.781 13712-13712/co.siqve.maplocationdemo W/System.err:     at com.google.android.gms.maps.zzaj.zza(Unknown Source)
07-10 12:01:38.781 13712-13712/co.siqve.maplocationdemo W/System.err:     at com.google.android.gms.maps.internal.zzaq.onTransact(Unknown Source)
07-10 12:01:38.781 13712-13712/co.siqve.maplocationdemo W/System.err:     at android.os.Binder.transact(Binder.java:499)
07-10 12:01:38.781 13712-13712/co.siqve.maplocationdemo W/System.err:     at com.google.android.gms.maps.internal.aq.a(:com.google.android.gms.DynamiteModulesB:5)
07-10 12:01:38.781 13712-13712/co.siqve.maplocationdemo W/System.err:     at com.google.maps.api.android.lib6.impl.bb.run(:com.google.android.gms.DynamiteModulesB:5)
07-10 12:01:38.781 13712-13712/co.siqve.maplocationdemo W/System.err:     at android.os.Handler.handleCallback(Handler.java:751)
07-10 12:01:38.781 13712-13712/co.siqve.maplocationdemo W/System.err:     at android.os.Handler.dispatchMessage(Handler.java:95)
07-10 12:01:38.781 13712-13712/co.siqve.maplocationdemo W/System.err:     at android.os.Looper.loop(Looper.java:154)
07-10 12:01:38.781 13712-13712/co.siqve.maplocationdemo W/System.err:     at android.app.ActivityThread.main(ActivityThread.java:6119)
07-10 12:01:38.782 13712-13712/co.siqve.maplocationdemo W/System.err:     at java.lang.reflect.Method.invoke(Native Method)
07-10 12:01:38.782 13712-13712/co.siqve.maplocationdemo W/System.err:     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
07-10 12:01:38.782 13712-13712/co.siqve.maplocationdemo W/System.err:     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)

Android SDK Version (API Level): 25

Android Studio plugins are up to date.

Thanks in advance!

EDIT:

Problem seems to be fixed now, here is my solution.

May be you are having this issue in genymotion and android studio emulator only. I faced same issue with genymotion and android studio emulator and then i checked it in android real device. It's working good for me. Did you check it in real 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

当我的真实设备上没有互联网连接时,我可以重现这个问题(java.io.IOException: grpc failed)。

I face this issue on Android 4.4.2 device several times. For me the solution is simply to restart the handset. No code update no Android studio uninstall.

This issue appeared for me (even though I make the request on a background thread), but in the Android Studio emulator only. A strange workaround that fixes it for me is to turn on and then turn off airplane mode!

This was happening also on my Samsung S7 Edge phone with Oreo installed. It only happened when I had an airplane mode on (not always - means something might have been cached at some points). I didn't explore the guts of Geocoder but I assume it requires some sort connectivity to get the information (that would also explain why this is happening so often on emulators). For me the solution was to check the network connectivity status and call this only when status != NETWORK_STATUS_NOT_CONNECTED .

For this you can implement a broadcast receiver, that will listen to any status changes on network.

Register receiver

IntentFilter networkFilter = new IntentFilter();
networkFilter.addAction("android.net.conn.CONNECTIVITY_CHANGE");
getActivity().registerReceiver(NetworkChangeReceiver, networkFilter);

Setup broadcast receiver to handle the broadcast

private BroadcastReceiver NetworkChangeReceiver = new BroadcastReceiver() {

  @Override
  public void onReceive(final Context context, final Intent intent) {
    int status = NetworkUtil.getConnectivityStatusString(context);

    if (status == NetworkUtil.NETWORK_STATUS_NOT_CONNECTED) {
    } else {
      // do your stuff with geocoder here
    }
  }
};

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.

Today (2017-09-16) my Android Studio (2.3.3) got an update for Google Play services to revision 44 . The issue was fixed afterwards. No code or setup changes from my side.

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:

  1. As noted by many here (and in the Android documentation here ), anything relating to 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.

  1. 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.

  2. 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.

I faced this issue while getting Addresses List using geocoder.getFromLocation() method, the problem is that getting addresses list from latitude,longitude takes time or on some slow devices it takes more time and, also sometimes it give me the java-io-ioexception-grpc-failed.

I fix this problem using Rx Java.

public class AsyncGeocoder {

private final Geocoder geocoder;

public AsyncGeocoder(Context context) {
    geocoder = new Geocoder(context);
}

public Disposable reverseGeocode(double lat, double lng, Callback callback) {
    return Observable.fromCallable(() -> {
        try {
            return geocoder.getFromLocation(lat, lng, 1);
        } catch (Exception e) {
            AppLogger.d("throwable,", new Gson().toJson(e));
            e.printStackTrace();
        }
        return false;
    }).subscribeOn(Schedulers.io())
            .observeOn(AndroidSchedulers.mainThread())
            .subscribe(result -> {
                //Use result for something
                AppLogger.d("throwable,", new Gson().toJson(result));

                callback.success((Address) ((ArrayList) result).get(0));
            }, throwable -> AppLogger.d("throwable,", new Gson().toJson(throwable)));
}

public interface Callback {
    void success(Address address);

    void failure(Throwable e);
}
}

Calling Position

mViewModel.getLocation(asyncGeocoder, getLat(), getLng(), this);

ViewModel Method

public void getLocation(AsyncGeocoder geocoder, Double lat, Double lng, AsyncGeocoder.Callback callback) {
        getCompositeDisposable().add(geocoder.reverseGeocode(lat, lng, callback));
    }

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();

I finally got this to work by using the following configuration for version 25 SDK, Build Tools and API emulator.

android {
    compileSdkVersion 25
    buildToolsVersion "25.0.3"
    defaultConfig {
        applicationId "com.example..."
        minSdkVersion 15
        targetSdkVersion 25

Also using Google API version 11.0.2.

I think this depends on the location of the device. because google map sometimes not working properly in some area. I'm from Iran and I also had this problem on real device and emulator. After a while go with this problem, I used a vpn application and it worked on the real device and emulator. because vpn application changed my ip address.

AOA! turn the accuracy Mode of device Gps to (only gps) instant of other option and then try......

worked for me settings are like this , i am postie you can also find them on emulators

This issue has already been reported, you can find other stack posts talking about it. Here is a comment on google issue with some of the different way geocoder crashes. I advise you to surround your part of code with try and catch, so your application doesn't stop.

In Wifi connection worked fine but in 3g or 4g connection is fail.

Using proxy

From my experience, I can say this error is thrown by the Geocoder 's getFromLocation() method. So don't do the following:

  • Do not call this method on main thread. This will freeze the UI and if anything which depends on the result of this method will crash. Handle this case.
  • When there's no internet connection, again, the IOExeption will be thrown. Again, anything depending on the result of this method will crash. Handle this case.

after some research figure out how to solve this , in my case all ways like put code in new Thread or use GeocodingApi not work , i just make sure

play-services-base

and

play-services-location

have same version like below :

implementation 'com.google.android.gms:play-services-base:17.1.0'

implementation 'com.google.android.gms:play-services-location:17.1.0'

就我而言,我刚刚从应用程序权限设置中打开(授予权限)位置。

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

I experienced the same issue and didn't know where the problem was. At all... Until recently, I started noticing that this problem appeared on a real device, when I had configured a proxy. When I used charles on MAC there was no issue with that, but when I started using MITM on Linux, this issue starting popping every time, without exception. Probably need to configure MITM additionally. But the bottom line is, if you turn off your proxy, you should be fine.

So look into that maybe.

When I use call getFromLocationName I get an IOException with description "grpc failed".

Code that's ran

@Override
public void onMapReady(GoogleMap googleMap) {
    mMap = googleMap;
    try {
        Geocoder geocoder = new Geocoder(getApplicationContext(), Locale.getDefault());
        List<Address> listAdresses = geocoder.getFromLocationName("London", 10);
        Log.i("PlaceInfo", listAdresses.get(0).toString());
    } catch (IOException e) {
        e.printStackTrace();
    }
}

Error the console outputs:

07-10 12:01:38.781 13712-13712/co.siqve.maplocationdemo W/System.err: java.io.IOException: grpc failed
07-10 12:01:38.781 13712-13712/co.siqve.maplocationdemo W/System.err:     at android.location.Geocoder.getFromLocationName(Geocoder.java:178)
07-10 12:01:38.781 13712-13712/co.siqve.maplocationdemo W/System.err:     at co.siqve.maplocationdemo.MapsActivity.onMapReady(MapsActivity.java:70)
07-10 12:01:38.781 13712-13712/co.siqve.maplocationdemo W/System.err:     at com.google.android.gms.maps.zzaj.zza(Unknown Source)
07-10 12:01:38.781 13712-13712/co.siqve.maplocationdemo W/System.err:     at com.google.android.gms.maps.internal.zzaq.onTransact(Unknown Source)
07-10 12:01:38.781 13712-13712/co.siqve.maplocationdemo W/System.err:     at android.os.Binder.transact(Binder.java:499)
07-10 12:01:38.781 13712-13712/co.siqve.maplocationdemo W/System.err:     at com.google.android.gms.maps.internal.aq.a(:com.google.android.gms.DynamiteModulesB:5)
07-10 12:01:38.781 13712-13712/co.siqve.maplocationdemo W/System.err:     at com.google.maps.api.android.lib6.impl.bb.run(:com.google.android.gms.DynamiteModulesB:5)
07-10 12:01:38.781 13712-13712/co.siqve.maplocationdemo W/System.err:     at android.os.Handler.handleCallback(Handler.java:751)
07-10 12:01:38.781 13712-13712/co.siqve.maplocationdemo W/System.err:     at android.os.Handler.dispatchMessage(Handler.java:95)
07-10 12:01:38.781 13712-13712/co.siqve.maplocationdemo W/System.err:     at android.os.Looper.loop(Looper.java:154)
07-10 12:01:38.781 13712-13712/co.siqve.maplocationdemo W/System.err:     at android.app.ActivityThread.main(ActivityThread.java:6119)
07-10 12:01:38.782 13712-13712/co.siqve.maplocationdemo W/System.err:     at java.lang.reflect.Method.invoke(Native Method)
07-10 12:01:38.782 13712-13712/co.siqve.maplocationdemo W/System.err:     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
07-10 12:01:38.782 13712-13712/co.siqve.maplocationdemo W/System.err:     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)

Android SDK Version (API Level): 25

Android Studio plugins are up to date.

Thanks in advance!

EDIT:

Problem seems to be fixed now, here is my solution.

I seem to have fixed this issue by opening the top toolbar of the phone and just swithing between wifi and network, i guess the operating memory on all virtual devices is set to 2GB RAM and finding location is just using too much therefore, I saw a skip in frames and location not being able to search. So if it is dropping this error, just switch from Wifi to Network and backwards.

就我而言,我删除了 AVD 并重新安装,一切正常。

Hi I had the "grpc failed" error after returning to an older project/test phone.

My code was calling the getFromLocation() method and just failing. reading and playing around for a few hours, I thought I would check if google maps would work, it showed my location on a blank map, wifi was switched off, so I turned WIFI on, Map worked fine.

Retested app, and voila my getFromLocation() method is working, so must have had something sticky in the location services area....

List<Address> addresses  = geocoder.getFromLocation(lat,lon, 1);

地址的大小通常为 0。这可能是由于未能达到峰值地址,因此将它们标记为“未知”或“默认”可能是一个选项

I got the same error when the search term was empty. Once I made sure that it is not empty the error disappeared. By search term I'm referring to the first argument of getFromLocationName method.

What seems to have worked for me was to log into a Google account on the emulator. A reboot afterwards might also be necessary.

In my case the problem seemed to be that I was calling the Geocoder too many times in rapid succession. Debouncing the requests fixed it.

UPDATE:

The problem seems to be solved now. I'm not sure if it the problem ever was on my end, so if you had this problem prior to this post, re-run your app and see if it works now.

If it still doesn't work, here is the stuff I did myself that "might" have made it work:

  1. I uninstalled and deleted all files related to Android Studio. ( NOT the project files).
  2. I then reinstalled Android Studio, and reopened the project back up.
  3. When running the app (I'm using Android Studio's built in emulator) I used a different virtual device and device API. This time I ran it on Pixel, with API 23, x86_64.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM