java.io.IOException: grpc failed

点点圈 提交于 2019-12-17 17:48:43

问题


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.


回答1:


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?




回答2:


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.




回答3:


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




回答4:


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.




回答5:


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
    }
  }
};



回答6:


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



回答7:


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.




回答8:


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.




回答9:


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.




回答10:


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

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




回答11:


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.




回答12:


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.



回答13:


In my case I just turned on (permission granted) the Position from app permission setting.




回答14:


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



回答15:


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.




回答16:


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



回答17:


I can reproduce this issue (java.io.IOException: grpc failed), when there is no Internet connection on my real device.



来源:https://stackoverflow.com/questions/45012289/java-io-ioexception-grpc-failed

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!