简体   繁体   English

地理编码器 grpc 失败

[英]Geocoder grpc failed

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.上个月的地理编码器每次都因“grpc failed”错误而开始失败,我似乎无法解决它。 I've looked at java.io.IOException: grpc failed but it didn't really solve the problem.我看过java.io.IOException: grpc failed但它并没有真正解决问题。

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".如果我针对 SDK 25 进行编译,使用构建工具 25 它可以在具有 SDK 25 的模拟器上运行。但是如果我在具有 SDK 24、26 或任何其他 SDK 版本的模拟器上尝试,它将失败并显示“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.当前设置:targetSDK/compileSDK 25,构建工具 25.0.3,Android Studio 2.3.3,常规模拟器。 (I've tried other build tools and SDK versions, but problem still remains) (我已经尝试过其他构建工具和 SDK 版本,但问题仍然存在)

Any ideas?有任何想法吗?

问题可能是您尝试在主线程中使用 Geocoder,如果您使用 IntentService,您可以在另一个进程中创建此URL选项,尽管您始终可以使用 Asynctask 或类似任务。

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我的互联网连接不好

It's related to Google Play Service on virtual devices.它与虚拟设备上的 Google Play 服务有关。 Use API25 and version 7.1.1 for solution.使用 API25 和版本 7.1.1解决。

Here are examples;以下是示例;

When I try on Genymotion and Google Play Services is disabled on virtual device (Error: Service not Available):当我尝试使用 Genymotion 并且虚拟设备上禁用了 Google Play 服务时(错误:服务不可用):

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):当我尝试使用 Genymotion 并启用 Google Play 服务时(错误:grpc 失败):

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):当我尝试使用 AVD Manager 并启用 Google Play 服务时(无错误):

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]

在此处输入图像描述

I was able to fix a similar problem by adding我能够通过添加来解决类似的问题

<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE"/>

to my manifest file到我的清单文件

I have solution for exception "java.io.ioexception grpc failed"我有异常“java.io.ioexception grpc failed”的解决方案

Code:代码:

try {
    Geocoder geocoder = new Geocoder(MainActivity.this, Locale.getDefault());
    List addresses = geocoder.getFromLocation(latitude, longitude, 1);
    List addresslist = addresses;
    String address = addresslist.get(0).getAddressLine(0);
    Log.d("add", address);
} catch (IOException e) {
    e.printStackTrace();
} 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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