duanlei4759
2017-05-22 12:08
浏览 439
已采纳

gRPC:如何使用Go服务器在Java客户端中获取多个返回值

I have a protocol buffer file:

syntax = "proto3";
package v1api;
option java_multiple_files = true;
option java_package = "myApp.v1";
option java_outer_classname = "V1";

service API {
    rpc Login(LoginRequest) returns (LoginResponse)
}

message LoginRequest {
    int pin = 1
}

message LoginResponse {
    string token = 1
}

I have my server written in Go (a language that can return multiple values), and my client being an Android application.

When I use this protoBuf to generate Go code for server, it is something like:

...
func (c *aPIClient) Login(ctx context.Context, in *LoginRequest, opts ...grpc.CallOption) (*LoginResponse, error) {
    out := new(LoginResponse)
    err := grpc.Invoke(ctx, "/v1api.API/Login", in, out, c.cc, opts...)
    if err != nil {
        return nil, err
    }
    return out, nil
}
...

Notice that there are two return values, (*LoginResponse, error).

Now, when I use this protoBuf to generate Java code for my Android side, I get something like:

public myApp.v1.LoginResponse login(myApp.v1.LoginRequest request) {
    return blockingUnaryCall(getChannel(), METHOD_LOGIN, getCallOptions(), request);
}

Notice that, here, there is just one return value like Java allows, myApp.v1.LoginResponse.

My question is, in case of an error returned by the server (something like: return nil, err), how will I get that second return value in my Android side.

图片转代码服务由CSDN问答提供 功能建议

我有一个协议缓冲区</ code>文件:</ p>

  syntax =“ proto3”; 
package v1api; 
option java_multiple_files = true; 
option java_package =“ myApp.v1”; 
option java_outer_classname =“ V1”; 
 
服务API {
 rpc Login(LoginRequest  )返回(LoginResponse)
} 
 
message LoginRequest {
 int销= 1 
} 
 
message LoginResponse {
字符串令牌= 1 
} 
 </ code> </ pre> 
  
 

我的服务器是用Go(可以返回多个值的语言)编写的,客户端是Android应用程序。</ p>

当我使用此protoBuf生成Go代码时, 对于服务器,它类似于:</ p>

  ... 
func(c * aPIClient)Login(ctx context.Context,在* LoginRequest中,选择... grpc.CallOption  )(* LoginResponse,错误){
 out:= new(LoginResponse)
 err:= grpc.Invoke(ctx,“ /v1api.API/Login”,in,out,c.cc,opts ...)  
 if err!= nil {
返回nil,err 
} 
返回nil 
} 
 ... 
 </ code> </ pre> 
 
  

请注意,有两个</ strong>返回值(* LoginResponse,error)</ code>。</ p>

现在,当我使用此protoBuf时 为我的Android端生成Java代码,我得到的内容如下:</ p>

  public myApp.v1.LoginResponse login(myApp.v1.LoginRequest request){
返回blockingUnaryCall(getChannel  (),METHOD_LOGIN,getCallOptions(),request); 
} 
 </ code> </ pre> 
 
 

请注意,这里的返回值只有一个</ strong> 像Java允许的那样, myApp.v1.LoginResponse </ code>。</ p>

我的问题是,如果服务器返回错误(例如: return nil ,err </ code>),如何在Android端获取第二个返回值。</ p> </ div>

2条回答 默认 最新

相关推荐 更多相似问题