java.io.IOException: grpc failed

2019-01-18 01:55发布

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.

9条回答
Explosion°爆炸
2楼-- · 2019-01-18 02:29

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.

查看更多
smile是对你的礼貌
3楼-- · 2019-01-18 02:35

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.
查看更多
Ridiculous、
4楼-- · 2019-01-18 02:39

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
    }
  }
};
查看更多
Melony?
5楼-- · 2019-01-18 02:43

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.

查看更多
小情绪 Triste *
6楼-- · 2019-01-18 02:43

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.

查看更多
ゆ 、 Hurt°
7楼-- · 2019-01-18 02:48

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)
       ...
    }
}
查看更多
登录 后发表回答