Android Java 快速使用 gRPC

更新时间: 2019-07-05 12:52

This guide gets you started with gRPC in Android Java with a simple working example.

Before you begin

Prerequisites

Note: gRPC Java does not support running a server on an Android device. For this quickstart, the Android client app will connect to a server running on your local (non-Android) computer.

Download the example

You’ll need a local copy of the example code to work through this quickstart. Download the example code from our GitHub repository (the following command clones the entire repository, but you just need the examples for this quickstart and other tutorials):

$ # Clone the repository at the latest release to get the example code:
$ git clone -b v1.21.0 https://github.com/grpc/grpc-java
$ # Navigate to the Java examples:
$ cd grpc-java/examples

Run a gRPC application

  1. Compile the server
   $ ./gradlew installDist
  1. Run the server
   $ ./build/install/examples/bin/hello-world-server
  1. In another terminal, compile and run the client
   $ cd android/helloworld
   $ ../../gradlew installDebug

Congratulations! You’ve just run a client-server application with gRPC.

Update a gRPC service

Now let’s look at how to update the application with an extra method on the server for the client to call. Our gRPC service is defined using protocol buffers; you can find out lots more about how to define a service in a .proto file in gRPC Basics: Android Java. For now all you need to know is that both the server and the client “stub” have a SayHello RPC method that takes a HelloRequest parameter from the client and returns a HelloResponse from the server, and that this method is defined like this:

// The greeting service definition.
service Greeter {
  // Sends a greeting
  rpc SayHello (HelloRequest) returns (HelloReply) {}
}

// The request message containing the user's name.
message HelloRequest {
  string name = 1;
}

// The response message containing the greetings
message HelloReply {
  string message = 1;
}

Let’s update this so that the Greeter service has two methods. Edit src/main/proto/helloworld.proto and update it with a new SayHelloAgain method, with the same request and response types:

// The greeting service definition.
service Greeter {
  // Sends a greeting
  rpc SayHello (HelloRequest) returns (HelloReply) {}
  // Sends another greeting
  rpc SayHelloAgain (HelloRequest) returns (HelloReply) {}
}

// The request message containing the user's name.
message HelloRequest {
  string name = 1;
}

// The response message containing the greetings
message HelloReply {
  string message = 1;
}

(Don’t forget to save the file!)

Update and run the application

When we recompile the example, normal compilation will regenerate GreeterGrpc.java, which contains our generated gRPC client and server classes. This also regenerates classes for populating, serializing, and retrieving our request and response types.

However, we still need to implement and call the new method in the human-written parts of our example application.

Update the server

Check out the Java quickstart here.

Update the client

In the same directory, open app/src/main/java/io/grpc/helloworldexample/HelloworldActivity.java. Call the new method like this:

    try {
        HelloRequest message = HelloRequest.newBuilder().setName(mMessage).build();
        HelloReply reply = stub.sayHello(message);
        reply = stub.sayHelloAgain(message);
    } catch (Exception e) {
        StringWriter sw = new StringWriter();
        PrintWriter pw = new PrintWriter(sw);
        e.printStackTrace(pw);
        pw.flush();
        return "Failed... : " + System.lineSeparator() + sw;
    }

Run!

Just like we did before, from the examples directory:

  1. Compile the server
   $ ./gradlew installDist
  1. Run the server
   $ ./build/install/examples/bin/hello-world-server
  1. In another terminal, compile and install the client to your device
   $ cd android/helloworld
   $ ../../gradlew installDebug

Connecting to the Hello World server via USB

To run the application on a physical device via USB debugging, you must configure USB port forwarding to allow the device to communicate with the server running on your computer. This is done via the adb command line tool as follows:

adb reverse tcp:8080 tcp:50051

This sets up port forwarding from port 8080 on the device to port 50051 on the connected computer, which is the port that the Hello World server is listening on.

Now you can run the Android Hello World app on your device, using localhost and 8080 as the Host and Port.

Connecting to the Hello World server from an Android Virtual Device

To run the Hello World app on an Android Virtual Device, you don’t need to enable port forwarding. Instead, the emulator can use the IP address 10.0.2.2 to refer to the host machine. Inside the Android Hello World app, enter 10.0.2.2 and 50051 as the Host and Port.

What’s next

互联网创业核心技术:构建可伸缩的web应用

互联网创业核心技术:构建可伸缩的web应用

【美】Artur Ejsmont / 李智慧、何坤 / 电子工业出版社 / 2016-12 / 89

可伸缩架构技术是所有互联网技术中最重要,也是最引人入胜的技术。《互联网创业核心技术:构建可伸缩的web应用》针对互联网创业需求快速迭代,业务快速发展,短时间内用户、数据、访问量激增的特点,提纲挈领地描述了伸缩性架构的基本原理与设计原则,详细阐述了Web应用前端层、服务层、数据层的可伸缩架构,并花大量篇幅讲述了缓存技术和异步处理技术的可伸缩设计及其在Web系统中的具体应用。 《互联网创业核心技......一起来看看 《互联网创业核心技术:构建可伸缩的web应用》 这本书的介绍吧!

RGB转16进制工具

RGB转16进制工具

RGB HEX 互转工具

随机密码生成器

随机密码生成器

多种字符组合密码

HEX HSV 转换工具

HEX HSV 转换工具

HEX HSV 互换工具