5

我花了一些时间才发现只有当我尝试从 SyncAdapter 使用 gRPC 客户端时才会导致以下错误:

No functional channel service provider found. Try adding a dependency on the grpc-okhttp or grpc-netty artifact

设置与官方教程使用同步适配器传输数据完全相同。

我使用与grpc-java中的示例相同的设置生成了 gRPC 代码。示例中的compile 'com.squareup.okhttp:okhttp:2.2.0'依赖项不是必需的,因为compile 'io.grpc:grpc-okhttp:0.9.0'已修改为自包含。

当从主活动调用 gRPC 服务方法时,它可以正常工作。

我怀疑grpc-okhttp必须以某种方式初始化,但不知道如何初始化。

4

1 回答 1

4

I've decided to read the code of gRPC. The code I've used to create channel was:

mChannel = ManagedChannelBuilder.forAddress(mHost, mPort)
        .usePlaintext(true).build();

The way the generic ManagedChannelBuilder class works is to dynamically load all classes extending ManageChannelBoulder and choose the one with highest priority. Then the chosen class is remembered in static variable.

It seems that during a call in SyncAdapter the appropriate builder for okhttp was not available. I've fixed it by hardcoding the choosen builder:

mChannel = OkHttpChannelBuilder.forAddress(mHost, mPort)
        .usePlaintext(true).build();

I hope to save someone's evening.

于 2015-12-18T00:18:06.780 回答