一、Android端使用protobuf
1、在工程的build.gradle文件中添加
classpath 'com.google.protobuf:protobuf-gradle-plugin:0.8.6'
如图:
2、在app的build.gradle文件中的dependencies添加 依赖
//protobuffer 数据封装
implementation 'com.google.protobuf:protobuf-java:2.6.1'
implementation 'com.google.protobuf:protoc:2.6.1'
3、书写.proto文件 如图
例如:我项目中的.proto 文件的内容
package santint.balance;
//数据包载体
enum CMD_TYPE{
Login_req_ = 1; //登录申请
Login_rsp_ = 2; //登录返回
Senddata_req_ = 3;//客户端请求发送数据
Senddata_rsp_ = 4;//服务端收到数据后回复
Supplier_req_ = 5;//请求供应商信息
Supplier_rsp_ = 6;//返回供应商信息
Food_req_ = 7; //客户端请求发送菜品信息
Food_rsp_ = 8; //服务端收到菜品信息后回复
SaveImage_req_ = 9; //请求发送图片
SaveImage_rsp_ = 10; //回复是否存储好图片
SelectFoodID_req_ = 11; //查询食材ID
SelectFoodID_rsp_ = 12; //返回食材ID
// SelectTime_req_ = 13; //查询时间
// SelectTime_rsp_ = 14; //返回时间数据
}
//登录申请
message Login_req
{
required string userName = 1;
required string userPwd = 2;
}
//登录返回
message Login_rsp
{
required sint32 retcode = 1; //返回 0:正常 -1:密码错误 -2:用户名不存在 -3:其他错误
optional sint64 userId = 2; //用户id
optional string userName = 3; //用户名
required sint64 shopID = 4; //门店ID
optional string error = 5; //错误原因
}
//请求发送数据
message Senddata_req
{
required sint64 shopID = 1;//门店ID
required string code = 2; //物料编码
required string name = 3; //物料名称
required float price = 4; //入库单价
required sint32 inputNum = 5; //入库数量
required string inputDate = 6; //入库日期
required sint32 outputNum = 7; //出库数量
required string outputDate = 8; //出库日期
required float balance = 9; //余额
}
//回复
message Senddata_rsp
{
required sint32 msgCmd = 1;//小命令号
required sint32 msgFlag = 2;//消息执行状态;0、成功;1、失败
optional string msgInfo = 3;//解释说明
}
message Supplier_req
{
required sint64 shopID = 1; //门店ID
required string date = 2; //日期
}
message Supplier_rsp
{
required sint64 shopID = 1; //门店ID
required sint32 total_num = 2; //供应商数量
repeated SupplierInfo supplier_list = 3;//供应商信息
message SupplierInfo
{
required string supplierCode = 1; //供应商ID
required string supplierName = 2; //供应商名称
required string createTime = 3; //用户下单时间
required sint32 orderNum = 4; //供应商提供的订单数量
}
}
message Food_req
{
required sint64 shopID = 1; //门店ID
optional string purchase_code = 2; //所属采购订单
required sint64 commodity_code = 3; //货品编码
optional string proof_image = 4; //图片路径
required float actual_amount = 5; //实入斤数
required sint32 status = 6; //状态:1=未收货,2=确认收货,3=拒收,4=缺货
optional string remark = 7; //备注.包含拒收原因或缺货原因
}
message Food_rsp
{
required sint32 msgCmd = 1;//小命令号
required sint32 msgFlag = 2;//消息执行状态;0、成功;1、失败
optional string msgInfo = 3;//解释说明
}
message SaveImage_req
{
required sint64 commodity_code = 1; //货品编码
required string image = 2; //图片
optional string image_name = 3; //图片名字
}
//回复
message SaveImage_rsp
{
required sint32 msgCmd = 1;//小命令号
required sint32 msgFlag = 2;//消息执行状态;0、成功;1、失败
optional string msgInfo = 3;//解释说明
}
message SelectFoodID_req
{
required sint64 shopID = 1; //门店ID
required string purchase_code = 2; //订单ID
required sint64 commodity_code = 3; //货品编码
}
message SelectFoodID_rsp
{
required string purchase_code = 1; //订单ID
required sint64 commodity_code = 2; //货品编码
required sint32 status = 3; //状态:1=未收货,2=确认收货,3=拒收,4=缺货
optional string remark = 4; //确认收货的备注、拒收原因或缺货原因
}
//message SelectTime_req
//{
// required string purchase_code = 1; //订单ID
//}
//message SelectTime_rsp
//{
// required string time = 1; //时间
//}
4、根据.proto文件生成对应的文件 如图:
5、调用.so文件向服务端发送消息
本地方法
C++文件中的方法
6、解析服务端返回的数据