Update Android example.

This commit is contained in:
Xudong Ma 2015-09-16 13:01:09 -07:00
parent 4cc103ac6a
commit 57ebf8aaf3
9 changed files with 108 additions and 376 deletions

View File

@ -1,10 +1,6 @@
gRPC Hello World Tutorial (Android Java)
========================
BACKGROUND
-------------
For this sample, we've already generated the server and client stubs from [helloworld.proto](examples/protos/helloworld.proto).
PREREQUISITES
-------------
- [Java gRPC](https://github.com/grpc/grpc-java)

View File

@ -1,34 +1,67 @@
apply plugin: 'com.android.application'
apply plugin: 'com.google.protobuf'
android {
compileSdkVersion 21
buildToolsVersion "21.1.2"
compileSdkVersion 22
buildToolsVersion "22.0.1"
defaultConfig {
applicationId "io.grpc.helloworldexample"
minSdkVersion 7
targetSdkVersion 21
applicationId "io.grpc.android.helloworldexample"
minSdkVersion 9
targetSdkVersion 22
versionCode 1
versionName "1.0"
}
buildTypes {
release {
minifyEnabled false
minifyEnabled true
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
lintOptions {
disable 'InvalidPackage', 'HardcodedText'
}
}
protobuf {
protoc {
artifact = 'com.google.protobuf:protoc:3.0.0-beta-1'
}
plugins {
grpc {
artifact = 'io.grpc:protoc-gen-grpc-java:0.9.0'
}
}
generateProtoTasks {
all().each { task ->
task.builtins {
javanano {
// Options added to --javanano_out
option 'ignore_services=true'
}
}
task.plugins {
grpc {
// Options added to --grpc_out
option 'nano=true'
}
}
}
}
}
dependencies {
compile 'com.android.support:appcompat-v7:21.0.3'
compile 'com.android.support:appcompat-v7:22.1.1'
compile 'com.google.code.findbugs:jsr305:3.0.0'
compile 'com.squareup.okhttp:okhttp:2.2.0'
compile 'com.google.guava:guava:18.0'
compile 'javax.annotation:javax.annotation-api:1.2'
// You need to build the https://github.com/grpc/grpc-java
// to obtain these libraries below.
compile 'io.grpc:grpc-core:0.1.0-SNAPSHOT'
compile 'io.grpc:grpc-protobuf-nano:0.1.0-SNAPSHOT'
compile 'io.grpc:grpc-okhttp:0.1.0-SNAPSHOT'
compile 'io.grpc:grpc-stub:0.1.0-SNAPSHOT'
compile 'io.grpc:grpc-core:0.9.0'
compile 'io.grpc:grpc-protobuf-nano:0.9.0'
compile 'io.grpc:grpc-okhttp:0.9.0'
compile 'io.grpc:grpc-stub:0.9.0'
}

View File

@ -15,3 +15,6 @@
#-keepclassmembers class fqcn.of.javascript.interface.for.webview {
# public *;
#}
-dontwarn com.google.common.**
-dontwarn okio.**

View File

@ -1,179 +0,0 @@
package io.grpc.helloworldexample;
import java.io.IOException;
import static io.grpc.stub.Calls.asyncUnaryCall;
import static io.grpc.stub.Calls.blockingUnaryCall;
import static io.grpc.stub.Calls.createMethodDescriptor;
import static io.grpc.stub.Calls.unaryFutureCall;
import static io.grpc.stub.ServerCalls.asyncUnaryRequestCall;
import static io.grpc.stub.ServerCalls.createMethodDefinition;
public class GreeterGrpc {
private static final io.grpc.stub.Method<Helloworld.HelloRequest,
Helloworld.HelloReply> METHOD_SAY_HELLO =
io.grpc.stub.Method.create(
io.grpc.MethodType.UNARY, "SayHello",
io.grpc.protobuf.nano.NanoUtils.<Helloworld.HelloRequest>marshaller(
new io.grpc.protobuf.nano.Parser<Helloworld.HelloRequest>() {
@Override
public Helloworld.HelloRequest parse(com.google.protobuf.nano.CodedInputByteBufferNano input) throws IOException {
return Helloworld.HelloRequest.parseFrom(input);
}
}),
io.grpc.protobuf.nano.NanoUtils.<Helloworld.HelloReply>marshaller(
new io.grpc.protobuf.nano.Parser<Helloworld.HelloReply>() {
@Override
public Helloworld.HelloReply parse(com.google.protobuf.nano.CodedInputByteBufferNano input) throws IOException {
return Helloworld.HelloReply.parseFrom(input);
}
}));
public static GreeterStub newStub(io.grpc.Channel channel) {
return new GreeterStub(channel, CONFIG);
}
public static GreeterBlockingStub newBlockingStub(
io.grpc.Channel channel) {
return new GreeterBlockingStub(channel, CONFIG);
}
public static GreeterFutureStub newFutureStub(
io.grpc.Channel channel) {
return new GreeterFutureStub(channel, CONFIG);
}
public static final GreeterServiceDescriptor CONFIG =
new GreeterServiceDescriptor();
public static class GreeterServiceDescriptor extends
io.grpc.stub.AbstractServiceDescriptor<GreeterServiceDescriptor> {
public final io.grpc.MethodDescriptor<Helloworld.HelloRequest,
Helloworld.HelloReply> sayHello;
private GreeterServiceDescriptor() {
sayHello = createMethodDescriptor(
"helloworld.Greeter", METHOD_SAY_HELLO);
}
private GreeterServiceDescriptor(
java.util.Map<java.lang.String, io.grpc.MethodDescriptor<?, ?>> methodMap) {
sayHello = (io.grpc.MethodDescriptor<Helloworld.HelloRequest,
Helloworld.HelloReply>) methodMap.get(
CONFIG.sayHello.getName());
}
@java.lang.Override
protected GreeterServiceDescriptor build(
java.util.Map<java.lang.String, io.grpc.MethodDescriptor<?, ?>> methodMap) {
return new GreeterServiceDescriptor(methodMap);
}
@java.lang.Override
public com.google.common.collect.ImmutableList<io.grpc.MethodDescriptor<?, ?>> methods() {
return com.google.common.collect.ImmutableList.<io.grpc.MethodDescriptor<?, ?>>of(
sayHello);
}
}
public static interface Greeter {
public void sayHello(Helloworld.HelloRequest request,
io.grpc.stub.StreamObserver<Helloworld.HelloReply> responseObserver);
}
public static interface GreeterBlockingClient {
public Helloworld.HelloReply sayHello(Helloworld.HelloRequest request);
}
public static interface GreeterFutureClient {
public com.google.common.util.concurrent.ListenableFuture<Helloworld.HelloReply> sayHello(
Helloworld.HelloRequest request);
}
public static class GreeterStub extends
io.grpc.stub.AbstractStub<GreeterStub, GreeterServiceDescriptor>
implements Greeter {
private GreeterStub(io.grpc.Channel channel,
GreeterServiceDescriptor config) {
super(channel, config);
}
@java.lang.Override
protected GreeterStub build(io.grpc.Channel channel,
GreeterServiceDescriptor config) {
return new GreeterStub(channel, config);
}
@java.lang.Override
public void sayHello(Helloworld.HelloRequest request,
io.grpc.stub.StreamObserver<Helloworld.HelloReply> responseObserver) {
asyncUnaryCall(
channel.newCall(config.sayHello), request, responseObserver);
}
}
public static class GreeterBlockingStub extends
io.grpc.stub.AbstractStub<GreeterBlockingStub, GreeterServiceDescriptor>
implements GreeterBlockingClient {
private GreeterBlockingStub(io.grpc.Channel channel,
GreeterServiceDescriptor config) {
super(channel, config);
}
@java.lang.Override
protected GreeterBlockingStub build(io.grpc.Channel channel,
GreeterServiceDescriptor config) {
return new GreeterBlockingStub(channel, config);
}
@java.lang.Override
public Helloworld.HelloReply sayHello(Helloworld.HelloRequest request) {
return blockingUnaryCall(
channel.newCall(config.sayHello), request);
}
}
public static class GreeterFutureStub extends
io.grpc.stub.AbstractStub<GreeterFutureStub, GreeterServiceDescriptor>
implements GreeterFutureClient {
private GreeterFutureStub(io.grpc.Channel channel,
GreeterServiceDescriptor config) {
super(channel, config);
}
@java.lang.Override
protected GreeterFutureStub build(io.grpc.Channel channel,
GreeterServiceDescriptor config) {
return new GreeterFutureStub(channel, config);
}
@java.lang.Override
public com.google.common.util.concurrent.ListenableFuture<Helloworld.HelloReply> sayHello(
Helloworld.HelloRequest request) {
return unaryFutureCall(
channel.newCall(config.sayHello), request);
}
}
public static io.grpc.ServerServiceDefinition bindService(
final Greeter serviceImpl) {
return io.grpc.ServerServiceDefinition.builder("helloworld.Greeter")
.addMethod(createMethodDefinition(
METHOD_SAY_HELLO,
asyncUnaryRequestCall(
new io.grpc.stub.ServerCalls.UnaryRequestMethod<
Helloworld.HelloRequest,
Helloworld.HelloReply>() {
@java.lang.Override
public void invoke(
Helloworld.HelloRequest request,
io.grpc.stub.StreamObserver<Helloworld.HelloReply> responseObserver) {
serviceImpl.sayHello(request, responseObserver);
}
}))).build();
}
}

View File

@ -1,175 +0,0 @@
// Generated by the protocol buffer compiler. DO NOT EDIT!
package io.grpc.helloworldexample;
@SuppressWarnings("hiding")
public interface Helloworld {
public static final class HelloRequest extends
com.google.protobuf.nano.MessageNano {
private static volatile HelloRequest[] _emptyArray;
public static HelloRequest[] emptyArray() {
// Lazily initializes the empty array
if (_emptyArray == null) {
synchronized (
com.google.protobuf.nano.InternalNano.LAZY_INIT_LOCK) {
if (_emptyArray == null) {
_emptyArray = new HelloRequest[0];
}
}
}
return _emptyArray;
}
// optional string name = 1;
public java.lang.String name;
public HelloRequest() {
clear();
}
public HelloRequest clear() {
name = "";
cachedSize = -1;
return this;
}
@Override
public void writeTo(com.google.protobuf.nano.CodedOutputByteBufferNano output)
throws java.io.IOException {
if (!this.name.equals("")) {
output.writeString(1, this.name);
}
super.writeTo(output);
}
@Override
protected int computeSerializedSize() {
int size = super.computeSerializedSize();
if (!this.name.equals("")) {
size += com.google.protobuf.nano.CodedOutputByteBufferNano
.computeStringSize(1, this.name);
}
return size;
}
@Override
public HelloRequest mergeFrom(
com.google.protobuf.nano.CodedInputByteBufferNano input)
throws java.io.IOException {
while (true) {
int tag = input.readTag();
switch (tag) {
case 0:
return this;
default: {
if (!com.google.protobuf.nano.WireFormatNano.parseUnknownField(input, tag)) {
return this;
}
break;
}
case 10: {
this.name = input.readString();
break;
}
}
}
}
public static HelloRequest parseFrom(byte[] data)
throws com.google.protobuf.nano.InvalidProtocolBufferNanoException {
return com.google.protobuf.nano.MessageNano.mergeFrom(new HelloRequest(), data);
}
public static HelloRequest parseFrom(
com.google.protobuf.nano.CodedInputByteBufferNano input)
throws java.io.IOException {
return new HelloRequest().mergeFrom(input);
}
}
public static final class HelloReply extends
com.google.protobuf.nano.MessageNano {
private static volatile HelloReply[] _emptyArray;
public static HelloReply[] emptyArray() {
// Lazily initializes the empty array
if (_emptyArray == null) {
synchronized (
com.google.protobuf.nano.InternalNano.LAZY_INIT_LOCK) {
if (_emptyArray == null) {
_emptyArray = new HelloReply[0];
}
}
}
return _emptyArray;
}
// optional string message = 1;
public java.lang.String message;
public HelloReply() {
clear();
}
public HelloReply clear() {
message = "";
cachedSize = -1;
return this;
}
@Override
public void writeTo(com.google.protobuf.nano.CodedOutputByteBufferNano output)
throws java.io.IOException {
if (!this.message.equals("")) {
output.writeString(1, this.message);
}
super.writeTo(output);
}
@Override
protected int computeSerializedSize() {
int size = super.computeSerializedSize();
if (!this.message.equals("")) {
size += com.google.protobuf.nano.CodedOutputByteBufferNano
.computeStringSize(1, this.message);
}
return size;
}
@Override
public HelloReply mergeFrom(
com.google.protobuf.nano.CodedInputByteBufferNano input)
throws java.io.IOException {
while (true) {
int tag = input.readTag();
switch (tag) {
case 0:
return this;
default: {
if (!com.google.protobuf.nano.WireFormatNano.parseUnknownField(input, tag)) {
return this;
}
break;
}
case 10: {
this.message = input.readString();
break;
}
}
}
}
public static HelloReply parseFrom(byte[] data)
throws com.google.protobuf.nano.InvalidProtocolBufferNanoException {
return com.google.protobuf.nano.MessageNano.mergeFrom(new HelloReply(), data);
}
public static HelloReply parseFrom(
com.google.protobuf.nano.CodedInputByteBufferNano input)
throws java.io.IOException {
return new HelloReply().mergeFrom(input);
}
}
}

View File

@ -12,9 +12,10 @@ import android.widget.EditText;
import android.widget.TextView;
import io.grpc.ManagedChannel;
import io.grpc.helloworldexample.Helloworld.HelloReply;
import io.grpc.helloworldexample.Helloworld.HelloRequest;
import io.grpc.transport.okhttp.OkHttpChannelBuilder;
import io.grpc.ManagedChannelBuilder;
import io.grpc.android.examples.GreeterGrpc;
import io.grpc.android.examples.nano.Helloworld.HelloReply;
import io.grpc.android.examples.nano.Helloworld.HelloRequest;
import java.util.concurrent.TimeUnit;
@ -69,7 +70,9 @@ public class HelloworldActivity extends ActionBarActivity {
@Override
protected String doInBackground(Void... nothing) {
try {
mChannel = OkHttpChannelBuilder.forAddress(mHost, mPort).build();
mChannel = ManagedChannelBuilder.forAddress(mHost, mPort)
.usePlaintext(true)
.build();
return sayHello(mChannel);
} catch (Exception e) {
return "Failed... : " + e.getMessage();
@ -79,7 +82,7 @@ public class HelloworldActivity extends ActionBarActivity {
@Override
protected void onPostExecute(String result) {
try {
mChannel.shutdown().awaitTerminated(1, TimeUnit.SECONDS);
mChannel.shutdown().awaitTermination(1, TimeUnit.SECONDS);
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
}

View File

@ -0,0 +1,50 @@
// Copyright 2015, Google Inc.
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are
// met:
//
// * Redistributions of source code must retain the above copyright
// notice, this list of conditions and the following disclaimer.
// * Redistributions in binary form must reproduce the above
// copyright notice, this list of conditions and the following disclaimer
// in the documentation and/or other materials provided with the
// distribution.
// * Neither the name of Google Inc. nor the names of its
// contributors may be used to endorse or promote products derived from
// this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
syntax = "proto3";
option java_package = "io.grpc.android.examples";
package helloworld;
// 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;
}

View File

@ -42,13 +42,13 @@
android:layout_height="wrap_content"
android:paddingTop="12dp"
android:paddingBottom="12dp"
android:textSize="16dp"
android:textSize="16sp"
android:text="Response:" />
<TextView
android:id="@+id/grpc_response_text"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:textSize="16dp" />
android:textSize="16sp" />
</LinearLayout>

View File

@ -6,6 +6,7 @@ buildscript {
}
dependencies {
classpath 'com.android.tools.build:gradle:1.1.0'
classpath "com.google.protobuf:protobuf-gradle-plugin:0.6.1"
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files