Android 如何在grpc 客户端中添加自定义header?
Android How to add a custom header in grpc client?
我必须在 android grpc 客户端中添加自定义 header。无法发送成功
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);
}
};
}
}
编辑:使用这种方式成功添加了自定义 header
现在在我的grpc调用中,我是这样调用的
ClientInterceptor interceptor = new HeaderClientInterceptor();
Channel channel = ManagedChannelBuilder.forAddress(BuildConfig.HOST, BuildConfig.PORT).build();
Channel channelWithHeader = ClientInterceptors.intercept(channel, interceptor);
ServiceGrpc.ServiceBlockingStub service = ServiceGrpc.newBlockingStub(channelWithHeader);
我已经构建了上面的请求并在下面的伪调用中调用它。
Iterator<Model> dataItems = service.getItems(SOMERequestBuilderObj);
我正在尝试添加这样的自定义 header
"Grps-Matches-Key : primary.secondary"
在 Rest API 调用中,我会将其添加为 header like
builder.header("Grps-Matches-Key", "primary.secondary");
希望对您有所帮助。
问题中的编辑版本有效 too.In GRPC 有很多方法可以添加 headers(称为元数据)。我们可以使用拦截器像我上面的问题一样添加元数据,或者我们可以为客户端存根添加元数据,或者您可以在客户端存根通道中发出请求之前添加它。
// 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);
在使用 MetadataUtils
提出任何请求之前添加 header
stub.withInterceptors(MetadataUtils.newAttachHeadersInterceptor(header))
我必须在 android grpc 客户端中添加自定义 header。无法发送成功
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);
}
};
}
}
编辑:使用这种方式成功添加了自定义 header
现在在我的grpc调用中,我是这样调用的
ClientInterceptor interceptor = new HeaderClientInterceptor();
Channel channel = ManagedChannelBuilder.forAddress(BuildConfig.HOST, BuildConfig.PORT).build();
Channel channelWithHeader = ClientInterceptors.intercept(channel, interceptor);
ServiceGrpc.ServiceBlockingStub service = ServiceGrpc.newBlockingStub(channelWithHeader);
我已经构建了上面的请求并在下面的伪调用中调用它。
Iterator<Model> dataItems = service.getItems(SOMERequestBuilderObj);
我正在尝试添加这样的自定义 header
"Grps-Matches-Key : primary.secondary"
在 Rest API 调用中,我会将其添加为 header like
builder.header("Grps-Matches-Key", "primary.secondary");
希望对您有所帮助。
问题中的编辑版本有效 too.In GRPC 有很多方法可以添加 headers(称为元数据)。我们可以使用拦截器像我上面的问题一样添加元数据,或者我们可以为客户端存根添加元数据,或者您可以在客户端存根通道中发出请求之前添加它。
// 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);
在使用 MetadataUtils
提出任何请求之前添加 headerstub.withInterceptors(MetadataUtils.newAttachHeadersInterceptor(header))