简体   繁体   中英

Retrofit and GRPC

Been tackling this for two days. I'm trying to use the ProtoConverterFactory with gRPC but not having any luck with it.

public class RetrofitService {
    public void makeRequest() {

        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl("https://myurl:444")
                .addConverterFactory(ProtoConverterFactory.create())
                .build();

        RetrofitServiceImp serviceImp = retrofit.create(RetrofitServiceImp.class);
        serviceImp.getToken(Empty.getDefaultInstance()).enqueue(new Callback<Token.TokenResponse>() {
            @Override
            public void onResponse(Call<Token.TokenResponse> call, Response<Token.TokenResponse> response) {
                Log.d("SUCCESS", "onResponse: ");
            }

            @Override
            public void onFailure(Call<Token.TokenResponse> call, Throwable t) {
                Log.d("ERROR", "onFailure: " + t.getLocalizedMessage() + t.getMessage());
            }
        });
    }

    interface RetrofitServiceImp {

        @POST("/")
        Call<Token.TokenResponse> getToken(@Body Empty empty);

    }
}

There are no examples of how to do this correctly so I tried following the Unit Tests in the converter factory.

https://github.com/square/retrofit/blob/master/retrofit-converters/protobuf/src/test/java/retrofit2/converter/protobuf/ProtoConverterFactoryTest.java

How do I link the compiled gRPC code to the converter or the retrofit lib?

The problem is that retrofit doesn't support gRPC out of the box.

Haven't tried it myself, but you might want to check Armenia framework. It supports gRPC and provides network engine compatible with retrofit's one.

https://line.github.io/armeria/client-retrofit.html

For future readers!

If you want to use gRPC with your android app instead of REST, use Square's Wire library.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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