最近正在学习和研究Google新推出的网络请求方式,主要面向移动应用开发并基于HTTP/2协议标准而设计,基于ProtoBuf(Protocol Buffers)序列化协议开发。在使用的时候发现这句代码是不变的。
而XXServiceGrpc.XXServiceBlockingStub stub = XXServiceGrpc.newBlockingStub(mChannel); ( 其中XX是由你的.proto文件的名称决定)ManagedChannelBuilder mChannel = ManagedChannelBuilder.forAddress(params[0], Integer.parseInt(params[1])).usePlaintext(true) .build();
XXProto.XXRequest request = new XXProto.XXRequest();//请求体
request.name = "张三"; // 利用请求体可以像服务器传入事先定义好的变量的值
XXProto.ResultMsgResponse response = stub.savePersonInfoRpc(request); //响应体, 相应体中有从服务器中传下来的一系列事先定义好的变量的值
PersonProto.PersonRequest request = new PersonProto.PersonRequest(); request.name = et_name.getEditText().getText().toString().trim(); request.manage = "管理员".equals(hv_role.getRightText()) ? "1": "0"; request.status = "正常".equals(hv_status.getRightText()) ? "1": "0"; request.mobilephone = et_phone.getEditText().getText().toString().trim(); PersonProto.ResultMsgResponse response = stub.savePersonInfoRpc(request);
封装:构造一个抽象的类, 使用泛型的方式定义特定的所需要对象。
package com.dog.util; import android.os.AsyncTask; import android.util.Log; import java.util.concurrent.TimeUnit; import io.grpc.ManagedChannel; import io.grpc.ManagedChannelBuilder; /** * Created by zw on 2016/3/23 0023. * Grpc 的异步AsyncTask处理类的基类 */ public abstract class BaseGrpcTask<T> extends AsyncTask<String, String, T> { private ManagedChannel mChannel; @Override protected void onPreExecute() { } /** * 异步操作 * @param channel * @return */ protected abstract T doInback(ManagedChannel channel); /** * 更新UI操作 * @param result */ protected abstract void upDateUI(T result); @Override protected T doInBackground(String... params) { try { mChannel = ManagedChannelBuilder.forAddress(params[0], Integer.parseInt(params[1])) .usePlaintext(true) .build(); return doInback(mChannel); } catch (Exception e) { e.printStackTrace(); } return null; } @Override protected void onPostExecute(T result) { try { upDateUI(result); mChannel.shutdown().awaitTermination(1, TimeUnit.SECONDS); } catch (InterruptedException e) { Thread.currentThread().interrupt(); } Log.i("TAG", "onPostExecute: result = " + result); } }
使用:
private class GrpcTask extends BaseGrpcTask<PersonProto.ResultMsgResponse>{ @Override protected PersonProto.ResultMsgResponse doInback(ManagedChannel channel) { PersonServiceGrpc.PersonServiceBlockingStub stub = PersonServiceGrpc.newBlockingStub(channel); PersonProto.PersonRequest request = new PersonProto.PersonRequest(); request.name = et_name.getEditText().getText().toString().trim(); request.manage = "管理员".equals(hv_role.getRightText()) ? "1": "0"; request.status = "正常".equals(hv_status.getRightText()) ? "1": "0"; request.mobilephone = et_phone.getEditText().getText().toString().trim(); PersonProto.ResultMsgResponse response = stub.savePersonInfoRpc(request); return response; } @Override protected void upDateUI(PersonProto.ResultMsgResponse result) { dismissProgressDialog(); if(result == null){ Toast.makeText(mContext, "添加失败", Toast.LENGTH_SHORT).show(); return; } String code = result.code; LogUtils.d(Constant.TAG, "codeInfo = " + result.codeInfo); if("false".equals(code)){ Toast.makeText(mContext, "添加失败" , Toast.LENGTH_SHORT).show(); return; }else{ Toast.makeText(mContext, "添加成功" , Toast.LENGTH_SHORT).show(); } finish(); } }
其中person.proro文件为:
syntax = "proto3"; import "google/protobuf/any.proto"; option java_package = "com.eeepay.api.grpc"; option java_outer_classname = "PersonProto"; package grpc; service PersonService{ rpc getPersonInfoRpc (PersonRequest) returns(PersonResponse){}; rpc savePersonInfoRpc (PersonRequest) returns(ResultMsgResponse){}; rpc updatePersonInfoRpc (PersonRequest) returns(ResultMsgResponse){}; } message PersonRequest{ string manage = 1; string mobilephone = 2; string status = 3; string name = 4; string id = 5; } message PersonResponse{ repeated PerMsg perMsg = 1; ResultMsgResponse resultMsg =2 ; } message PerMsg { string user_name = 1; string mobilephone = 2; string manage = 3; string status = 4; string id = 5 ; } message ResultMsgResponse{ string code =1; string codeInfo =2; }使用的时候直接实例化自己的Task对象,执行的时候传入地址和端口号即可。
new GrpcTask().execute("192.168.1.103", "8090");