问题
Last month of so geocoder has started to fail every time with "grpc failed" error and I can't seem to be able to solve it. I've looked at java.io.IOException: grpc failed but it didn't really solve the problem.
For exampel the code
Geocoder geocoder = new Geocoder(context, Locale.getDefault());
addresses = geocoder.getFromLocation(lat, lng, 1); // Here if fails with "grpc failed"
I've had the same code for years, but it's just lately it has stopped working when I recompile the app and test on an emulator or real device.
If I compile against SDK 25, using build tool 25 it works on an emulator with SDK 25. But if I try on an emulator with SDK 24, 26 or any other SDK version, it will fail with "grpc failed". I've tried to create a new virtual device etc. but same problem.
I have no clue how to fix it and what the problem is.
Current setup: targetSDK/compileSDK 25, build tool 25.0.3, Android Studio 2.3.3, regular emulator. (I've tried other build tools and SDK versions, but problem still remains)
Any ideas?
回答1:
The problem may be that you are trying to use Geocoder in the main thread, if you are using an IntentService you have this URL option to create in another process although you can always use an Asynctask or similar.
回答2:
Well, this might be too late and for someone else looking for answers, in my case it was internet problem. My internet connection was bad
回答3:
It's related to Google Play Service on virtual devices. Use API25 and version 7.1.1 for solution.
Here are examples;
When I try on Genymotion and Google Play Services is disabled on virtual device (Error: Service not Available):
10-07 07:13:30.023 3481-3481/com.unalfaruk.mapexample I/System.out: Your Location: 65.96992333333333 -18.540028333333332
10-07 07:13:30.023 3481-3481/com.unalfaruk.mapexample W/System.err: java.io.IOException: Service not Available
10-07 07:13:30.024 3481-3481/com.unalfaruk.mapexample W/System.err: at android.location.Geocoder.getFromLocation(Geocoder.java:136)
10-07 07:13:30.024 3481-3481/com.unalfaruk.mapexample W/System.err: at com.unalfaruk.mapexample.MapsActivity$1.onLocationChanged(MapsActivity.java:71)
10-07 07:13:30.024 3481-3481/com.unalfaruk.mapexample W/System.err: at android.location.LocationManager$ListenerTransport._handleMessage(LocationManager.java:297)
When I try on Genymotion and Google Play Services is enabled (Error: grpc failed):
10-07 07:18:56.325 2694-2694/com.unalfaruk.mapexample I/System.out: Your Location: 65.96669666666666 -15.0
10-07 07:18:57.670 2694-2694/com.unalfaruk.mapexample W/System.err: java.io.IOException: grpc failed
10-07 07:18:57.670 2694-2694/com.unalfaruk.mapexample W/System.err: at android.location.Geocoder.getFromLocation(Geocoder.java:136)
10-07 07:18:57.670 2694-2694/com.unalfaruk.mapexample W/System.err: at com.unalfaruk.mapexample.MapsActivity$1.onLocationChanged(MapsActivity.java:71)
10-07 07:18:57.670 2694-2694/com.unalfaruk.mapexample W/System.err: at android.location.LocationManager$ListenerTransport._handleMessage(LocationManager.java:297)
When I try on AVD Manager and Google Play Services is enabled (No error):
10-07 14:20:14.769 3159-3159/com.unalfaruk.mapexample I/System.out: Your Location: 37.421998333333335 -122.08400000000002
10-07 14:20:15.220 3159-3159/com.unalfaruk.mapexample I/System.out: Your Adress: Address[addressLines=[0:"1600 Amphitheatre Parkway",1:"Mountain View, CA 94043",2:"USA"],feature=1600,admin=California,sub-admin=null,locality=Mountain View,thoroughfare=Amphitheatre Parkway,postalCode=94043,countryCode=US,countryName=United States,hasLatitude=true,latitude=37.422329,hasLongitude=true,longitude=-122.0843055,phone=null,url=null,extras=null]
来源:https://stackoverflow.com/questions/45521579/geocoder-grpc-failed