Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/234.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android如何在grpc客户端添加自定义头?_Android_Client_Metadata_Grpc_Grpc Java - Fatal编程技术网

Android如何在grpc客户端添加自定义头?

Android如何在grpc客户端添加自定义头?,android,client,metadata,grpc,grpc-java,Android,Client,Metadata,Grpc,Grpc Java,我必须在android grpc客户端中添加一个自定义头。我无法成功发送它 public class HeaderClientInterceptor implements ClientInterceptor { @Override public < ReqT, RespT > ClientCall < ReqT, RespT > interceptCall(MethodDescriptor < ReqT, RespT > method,

我必须在android grpc客户端中添加一个自定义头。我无法成功发送它

public class HeaderClientInterceptor implements ClientInterceptor {
    @Override
    public < ReqT, RespT > ClientCall < ReqT, RespT > interceptCall(MethodDescriptor < ReqT, RespT > method,
        CallOptions callOptions, Channel next) {

        return new SimpleForwardingClientCall < ReqT, RespT > (next.newCall(method, callOptions)) {

            @Override
            public void start(Listener < RespT > responseListener, Metadata headers) {
                /* put custom header */
                Timber.d("header sending to server:");


                Metadata fixedHeaders = new Metadata();
                Metadata.Key < String > key =
                    Metadata.Key.of("Grps-Matches-Key", Metadata.ASCII_STRING_MARSHALLER);
                fixedHeaders.put(key, "primary.secondary");

                headers.merge(fixedHeaders);

                super.start(new SimpleForwardingClientCallListener < RespT > (responseListener) {
                    @Override
                    public void onHeaders(Metadata headers) {
                        /**
                         * if you don't need receive header from server,
                         * you can use {@link io.grpc.stub.MetadataUtils attachHeaders}
                         * directly to send header
                         */

                        Timber.e("header received from server:" + headers.toString());
                        super.onHeaders(headers);
                    }
                }, headers);
            }
        };
    }
}
我构建了上述请求,并在伪调用中调用它,如下所示

Iterator<Model> dataItems = service.getItems(SOMERequestBuilderObj);

希望这能有所帮助。

问题中的编辑版本也能起作用。在GRPC中,有许多方法可以添加标题(称为元数据)。我们可以使用拦截器添加元数据,就像我上面的问题一样。我们可以为客户端存根添加元数据,或者您可以在客户端存根通道中发出请求之前添加元数据

// create a custom header
Metadata header=new Metadata();
Metadata.Key<String> key =
    Metadata.Key.of("Grps-Matches-Key", Metadata.ASCII_STRING_MARSHALLER);
header.put(key, "match.items");

// create client stub
ServiceGrpc.ServiceBlockingStub stub = ServiceGrpc
    .newBlockingStub(channel);
给Kotlin的情人

    private fun getHeaderMetaData(): io.grpc.Metadata {
        val header: io.grpc.Metadata = io.grpc.Metadata()
        val channel: io.grpc.Metadata.Key<String> = io.grpc.Metadata.Key.of("channel", io.grpc.Metadata.ASCII_STRING_MARSHALLER)
        val api_key: io.grpc.Metadata.Key<String> = io.grpc.Metadata.Key.of("api-key", io.grpc.Metadata.ASCII_STRING_MARSHALLER)
        val lang: io.grpc.Metadata.Key<String> = io.grpc.Metadata.Key.of("lang", io.grpc.Metadata.ASCII_STRING_MARSHALLER)
        header.put(channel, "AND")
        header.put(api_key, "dummykey")
        header.put(lang, "en")
        return header
    }

您可以将预定义的拦截器与MetadataUtils.newAttachHeaderInterceptor(metadata)一起使用,因此我们不需要拦截器来附加您提到的头,对吗?两者都应该可以工作。添加拦截器作为元数据
// create a custom header
Metadata header=new Metadata();
Metadata.Key<String> key =
    Metadata.Key.of("Grps-Matches-Key", Metadata.ASCII_STRING_MARSHALLER);
header.put(key, "match.items");

// create client stub
ServiceGrpc.ServiceBlockingStub stub = ServiceGrpc
    .newBlockingStub(channel);
stub = MetadataUtils.attachHeaders(stub, header);
    private fun getHeaderMetaData(): io.grpc.Metadata {
        val header: io.grpc.Metadata = io.grpc.Metadata()
        val channel: io.grpc.Metadata.Key<String> = io.grpc.Metadata.Key.of("channel", io.grpc.Metadata.ASCII_STRING_MARSHALLER)
        val api_key: io.grpc.Metadata.Key<String> = io.grpc.Metadata.Key.of("api-key", io.grpc.Metadata.ASCII_STRING_MARSHALLER)
        val lang: io.grpc.Metadata.Key<String> = io.grpc.Metadata.Key.of("lang", io.grpc.Metadata.ASCII_STRING_MARSHALLER)
        header.put(channel, "AND")
        header.put(api_key, "dummykey")
        header.put(lang, "en")
        return header
    }
   var stub = FlightSearchGrpc.newBlockingStub(channel)
   stub = io.grpc.stub.MetadataUtils.attachHeaders(stub, getHeaderMetaData())