在新版本的Beetle.NetPackage开源组件中集成了对Protobuf的支持,在android下可以简单地使用Beetle.NetPackage实现基于Protobuf的TCP通讯数据交互.下详细讲解实现一个网络订单管理功能的过程.
协议定义
既然使用Probobuf进行数据交互,那就需要基于Protobuf制定一系列的通信对象,主要包括数据请求和应答的数据对象格式.在做之前需要把需求整理一下明确一下有那些功能,由于Demo主要是用于介绍一下protobuf在Beetle.NetPackage下的使用所以功能并不复杂包括:订单分页查询和订单明细查看.具体Protobuf定义如下:
package ordermanager;
option java_package = "com.beetle.samples.ordermanager";
option java_outer_classname = "orderproto";
message GetEmployee
{
optional string Name=1;
}
message GetEmployeeResponse
{
repeated Employee Items=1;
}
message Employee
{
optional string ID=1;
optional string Name=2;
}
message GetCustomer
{
optional string Name=1;
}
message GetCustomerResponse
{
repeated Customer Items =1;
}
message Customer
{
optional string ID=1;
optional string Name=2;
}
message Response
{
optional string Error=1;
optional string Type =2;
}
message OrderSearch
{
optional string Employee=1;
optional int32 PageIndex=2;
optional string Customer =3;
optional string FromDate =4;
optional string ToDate =5;
}
message OrderSearchResponse
{
repeated Order Items =1;
optional int32 PageIndex =2;
optional int32 Pages =3;
}
message Order
{
optional string OrderID=1;
optional string Employee =2;
optional string Customer =3;
optional string OrderDate=4;
optional string RequiredDate=5;
optional string ShippedDate=6;
optional string ShipName=7;
optional string ShipAddress=8;
optional string ShipCity =9;
optional string ShipRegion=10;
}
message GetOrderDetail
{
optional string OrderID =1;
}
message GetOrderDetailResponse
{
repeated OrderDetail Items =1;
}
message OrderDetail
{
optional string OrderID=1;
optional string Product=2;
optional double UnitPrice=3;
optional int32 Quantity=4;
optional float Discount=5;
}
主要制定了订单雇员,客户,订单和订单明细查询的数据交互格式.
通讯定义
Beetle.NetPackage基础通讯都已经封装好了,所以使用起来非常简单,probotuf的注册和创建相应的TCP连接只需要几行代码即可
ProtoPackage.Register(orderproto.class);
mClient = new NetClient("10.0.2.2", 9088, new ProtoPackage(), this);
mClient.Connect();
以上代码是注册相关Protobuf对象信息,主要用于Beetle.NetPackage对protobuf进行自动化处理.然后定义一个基于Protobuf解释器的连接对象,交接入到相应IP端口的服务中.
消息接收定义
Beetle.NetPackage提供基础的Controller功能,因此只需要定义相关消息类型的方法即可.
public void OnGetOrderDetali(NetClient client, GetOrderDetailResponse e) {
final List<OrderDetail> details = e.getItemsList();
mHandler.post(new Runnable() {
@Override
public void run() {
// TODO Auto-generated method stub
mOrderDetailViewAdapter.clear();
for (OrderDetail item : details) {
mOrderDetailViewAdapter.add(item);
}
mOrderDetailDialog.show();
ListView lv = (ListView) mOrderDetailDialog
.findViewById(R.id.lstOrderDetail);
lv.setAdapter(mOrderDetailViewAdapter);
}
});
}
public void OnGetCustomerResponse(NetClient client, GetCustomerResponse e) {
mCustomers = e.getItemsList();
}
public void OnGetEmployeeResponse(NetClient client, GetEmployeeResponse e) {
mEmployees = e.getItemsList();
}
public void OnOrderSearchRespnose(NetClient client, OrderSearchResponse e) {
final List<Order> orders = e.getItemsList();
mPages = e.getPages();
mHandler.post(new Runnable() {
@Override
public void run() {
// TODO Auto-generated method stub
mOrderViewAdapter.clear();
for (Order item : orders) {
mOrderViewAdapter.add(item);
}
mOrderViewAdapter.notifyDataSetChanged();
}
});
}
通过调用以下方法则可以自动切入到相关消息方法中.
@Override
public void Receive(NetClient client, Object msg) {
Controller.Invoke(this, mClient, msg);
}
消息发送请求
GetCustomer.Builder getcusomer = GetCustomer.newBuilder();
mClient.Send(getcusomer.build());
GetEmployee.Builder getemployee = GetEmployee.newBuilder();
mClient.Send(getemployee.build());
功能截图
下载代码
Beetle.NetPackage官网
https://beetlenp.codeplex.com/
*--------------------------------------
*个人站:www.ikende.com www.asquestion.com
*个人开源项目github.com/IKende
*--------------------------------------*
来源:oschina
链接:https://my.oschina.net/u/1000376/blog/159930