java.io.IOException: grpc failed

前端 未结 24 1045
野性不改
野性不改 2020-12-02 20:00

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

Code that\'s ran

@Override
public void onMapReady(GoogleMa         


        
24条回答
  •  天命终不由人
    2020-12-02 20:48

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

提交回复
热议问题