//Project的build.gradle中添加rotobuf-gradle-plugin插件
buildscript {
...
dependencies {
...
classpath "com.google.protobuf:protobuf-gradle-plugin:0.8.0"
...
}
...
}
//App的build.gradle中添加下面配置
apply plugin: 'com.google.protobuf'
protobuf {
protoc {
artifact = 'com.google.protobuf:protoc:3.0.0'
}
plugins {
javalite {
artifact = "com.google.protobuf:protoc-gen-javalite:3.0.0"
}
grpc {
artifact = 'io.grpc:protoc-gen-grpc-java:1.0.0' // CURRENT_GRPC_VERSION
}
}
generateProtoTasks {
all().each { task ->
task.plugins {
javalite {}
grpc {
// Options added to --grpc_out
option 'lite'
}
}
}
}
}
//App的build.gradle中添加下面配置
dependencies {
...
compile 'io.grpc:grpc-okhttp:1.1.2'
compile 'io.grpc:grpc-protobuf-lite:1.1.2'
compile 'io.grpc:grpc-stub:1.1.2'
compile 'javax.annotation:javax.annotation-api:1.2'
...
}
configurations.all {
resolutionStrategy.force 'com.google.code.findbugs:jsr305:3.0.1'
}
Warning:Conflict with dependency ‘com.google.code.findbugs:jsr305'. Resolved versions for app (3.0.0) and test app (2.0.1) differ. See http://g.co/androidstudio/app-test-app-conflict for details.
android {
...
configurations.all {
resolutionStrategy.force 'com.google.code.findbugs:jsr305:3.0.1'
}
...
}
public class MainActivity extends AppCompatActivity {
private static final String TAG = "GrpcDemo";
private static final int PROT = 55055;
private static final String NAME = "linjw";
private static final String HOST = "localhost";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
startServer(PROT);
startClient(HOST, PROT, NAME);
}
private void startServer(int port){
try {
Server server = ServerBuilder.forPort(port)
.addService(new GreeterImpl())
.build()
.start();
} catch (IOException e) {
e.printStackTrace();
Log.d(TAG, e.getMessage());
}
}
private void startClient(String host, int port, String name){
new GrpcTask(host, port, name).execute();
}
private class GreeterImpl extends GreeterGrpc.GreeterImplBase {
public void sayHello(HelloRequest request, StreamObserver<HelloReply> responseObserver) {
responseObserver.onNext(sayHello(request));
responseObserver.onCompleted();
}
private HelloReply sayHello(HelloRequest request) {
return HelloReply.newBuilder()
.setMessage("hello "+ request.getName())
.build();
}
}
private class GrpcTask extends AsyncTask<Void, Void, String> {
private String mHost;
private String mName;
private int mPort;
private ManagedChannel mChannel;
public GrpcTask(String host, int port, String name) {
this.mHost = host;
this.mName = name;
this.mPort = port;
}
@Override
protected void onPreExecute() {
}
@Override
protected String doInBackground(Void... nothing) {
try {
mChannel = ManagedChannelBuilder.forAddress(mHost, mPort)
.usePlaintext(true)
.build();
GreeterGrpc.GreeterBlockingStub stub = GreeterGrpc.newBlockingStub(mChannel);
HelloRequest message = HelloRequest.newBuilder().setName(mName).build();
HelloReply reply = stub.sayHello(message);
return reply.getMessage();
} catch (Exception e) {
StringWriter sw = new StringWriter();
PrintWriter pw = new PrintWriter(sw);
e.printStackTrace(pw);
pw.flush();
return "Failed... : " + System.lineSeparator() + sw;
}
}
@Override
protected void onPostExecute(String result) {
try {
mChannel.shutdown().awaitTermination(1, TimeUnit.SECONDS);
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
}
Log.d(TAG, result);
}
}
}
Caused by: io.grpc.ManagedChannelProvider$ProviderNotFoundException: No functional server found. Try adding a dependency on the grpc-netty artifact
dependencies {
...
compile 'io.grpc:grpc-netty:1.1.2'
compile 'io.grpc:grpc-protobuf-lite:1.1.2'
compile 'io.grpc:grpc-stub:1.1.2'
compile 'javax.annotation:javax.annotation-api:1.2'
...
}
com.android.build.api.transform.TransformException: com.android.builder.packaging.DuplicateFileException: Duplicate files copied in APK META-INF/INDEX.LIST
android {
...
packagingOptions {
pickFirst 'META-INF/INDEX.LIST'
pickFirst 'META-INF/LICENSE'
pickFirst 'META-INF/io.netty.versions.properties'
}
...
}
03-03 00:04:20.000 6137-6137/linjw.com.grpcdemo D/GrpcDemo: hello linjw
// 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_multiple_files = true;
option java_package = "io.grpc.examples.helloworld";
option java_outer_classname = "HelloWorldProto";
option objc_class_prefix = "HLW";
package helloworld;
import "google/protobuf/any.proto";
// The greeting service definition.
service Greeter {
// Sends a greeting
rpc SayHello (google.protobuf.Any) 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;
}
google/protobuf/any.proto: File not found. helloworld.proto: Import “google/protobuf/any.proto” was not found or had errors. helloworld.proto:44:17: “google.protobuf.Any” is not defined.
dependencies {
...
compile 'io.grpc:grpc-netty:1.1.2'
compile 'io.grpc:grpc-protobuf:1.1.2'
compile 'io.grpc:grpc-stub:1.1.2'
compile 'javax.annotation:javax.annotation-api:1.2'
...
}
protobuf {
protoc {
artifact = 'com.google.protobuf:protoc:3.0.0'
}
plugins {
grpc {
artifact = 'io.grpc:protoc-gen-grpc-java:1.0.0' // CURRENT_GRPC_VERSION
}
}
generateProtoTasks {
all().each { task ->
task.builtins {
java {}
}
task.plugins {
grpc {}
}
}
}
}
public class MainActivity extends AppCompatActivity {
private static final String TAG = "GrpcDemo";
private static final int PROT = 55055;
private static final String NAME = "linjw";
private static final String HOST = "localhost";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
startServer(PROT);
startClient(HOST, PROT, NAME);
}
private void startServer(int port){
try {
Server server = ServerBuilder.forPort(port)
.addService(new GreeterImpl())
.build()
.start();
} catch (IOException e) {
e.printStackTrace();
Log.d(TAG, e.getMessage());
}
}
private void startClient(String host, int port, String name){
new GrpcTask(host, port, name).execute();
}
private class GreeterImpl extends GreeterGrpc.GreeterImplBase {
public void sayHello(Any request, StreamObserver<HelloReply> responseObserver) {
try {
responseObserver.onNext(sayHello(request.unpack(HelloRequest.class)));
responseObserver.onCompleted();
} catch (InvalidProtocolBufferException e) {
e.printStackTrace();
}
}
private HelloReply sayHello(HelloRequest request) {
return HelloReply.newBuilder()
.setMessage("hello "+ request.getName())
.build();
}
}
private class GrpcTask extends AsyncTask<Void, Void, String> {
private String mHost;
private String mName;
private int mPort;
private ManagedChannel mChannel;
public GrpcTask(String host, int port, String name) {
this.mHost = host;
this.mName = name;
this.mPort = port;
}
@Override
protected void onPreExecute() {
}
@Override
protected String doInBackground(Void... nothing) {
try {
mChannel = ManagedChannelBuilder.forAddress(mHost, mPort)
.usePlaintext(true)
.build();
GreeterGrpc.GreeterBlockingStub stub = GreeterGrpc.newBlockingStub(mChannel);
HelloRequest message = HelloRequest.newBuilder().setName(mName).build();
HelloReply reply = stub.sayHello(Any.pack(message));
return reply.getMessage();
} catch (Exception e) {
StringWriter sw = new StringWriter();
PrintWriter pw = new PrintWriter(sw);
e.printStackTrace(pw);
pw.flush();
return "Failed... : " + System.lineSeparator() + sw;
}
}
@Override
protected void onPostExecute(String result) {
try {
mChannel.shutdown().awaitTermination(1, TimeUnit.SECONDS);
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
}
Log.d(TAG, result);
}
}
}
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有