`

Android平台Thrift服务端开发总结

阅读更多
引言
  thrift,不解释了,可支持不同语言的代码之间分布式调用,很好用,很强大,本文使用的版本为0.9.3。Thrift服务器端,使用Android实现。客户端使用C#,VS2015开发。客户端向服务器端发送一个字符串,服务器端(Android的APK)在界面上显示字符串。
  作者:http://wallimn.iteye.com 时间:2016-01-11。

1.thrift文件,文件名Hello.thrift
namespace java thrift.test
namespace csharp Thrift.Test
service ThriftTest
{
/** added by wallimn, 2016-01-11*/
void showMessage(1: string msg)
}

2.编译Hello.thrift文件,在命令行中执行如下编译命令,分类生成JAVA及C#的辅助代码
thrift-0.9.3.exe -gen java Hello.thrift
thrift-0.9.3.exe -gen csharp Hello.thrift


3.在Android工程中加入thrift编译生成的文件,加入thrift的jar包,并定义服务接口的实现类
    public class ServiceImpl implements ThriftTest.Iface{

        @Override
        public void showMessage(String msg) throws TException {
            Message msg = new Message();
            msg.what =  0x8090;
            msg.getData().putString("msg",msg);
            MainActivity.this.handler.sendMessage(msg);//向主线程发消息,显示收到信息
        }
    }


4.Activity布局很简单,只有一个TextView,使用tvMsg保存View对应的变量,显示收到的消息,这里不给出源码了

5.在主界面中(MainActivity)定义消息处理器(handler)成员变量
    Handler handler = new Handler(){
        public void handleMessage(Message msg){
            if(msg.what==0x8090){
               String str= (String) msg.getData().get("msg");
               tvMsg.setText(str);//Activity上的TextView元素显示消息内容
            }
        }
    };


6.启动thrift服务端的代码:MainActivity中定义一个线程变量,注意定义成成员变量,Activity创建函数中启动线程
    Thread thread = new Thread(){
        @Override
        public void run() {
            //super.run();
            TServerSocket socket = null;
            try {
                socket = new TServerSocket(8090);
                TProcessor processor = new ThriftTest.Processor<ThriftTest.Iface>(new ServiceImpl());
                TBinaryProtocol.Factory factory = new TBinaryProtocol.Factory();

                TThreadPoolServer.Args tArgs = new TThreadPoolServer.Args(socket);
                tArgs.processor(processor);
                tArgs.protocolFactory(factory);
                TServer server = new TThreadPoolServer(tArgs);
                //Toast.makeText(MainActivity.this,"服务启动,8090",Toast.LENGTH_LONG).show();
                server.serve();
            } catch (TTransportException e) {
                e.printStackTrace();
            }
        }
    };    



7.测试客户端,使用C#编写,注意把thrift编译生成的文件加入工程,引用thrift.dll(自行编译thrift源码获得,源码是个VS的工程,编译很简单)。测试客户端是一个简单的窗体,上面两个TextBox,分别用于输入服务器端的IP、发送的消息,以及一个按钮
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Thrift.Protocol;
using Thrift.Transport;

namespace ThriftAndroid
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void btnSend_Click(object sender, EventArgs e)
        {
            if (tbUrl.Text == "" || tbIP.Text == "") return;
            HelloWorldServiceClient.startClient(tbIP.Text, tbUrl.Text);
            //MessageBox.Show("cmd sended");

        }
    }
    public class HelloWorldServiceClient
    {
        public static void startClient(String ip,String url)
        {
            TTransport transport = null;
            try
            {
                transport = new TSocket(ip, 8090, 30000);
                TProtocol protocol = new TBinaryProtocol(transport);
                Thrift.Test.ThriftTest.Client client = new Thrift.Test.ThriftTest.Client(protocol);

                transport.Open();
                client.sendMessage(url);
            }
            catch (Exception e)
            {
                Console.WriteLine(e.StackTrace);
            }
            finally
            {
                if (transport != null) transport.Close();
            }

        }
    }
}
  • bin.rar (1 MB)
  • 描述: 测试客户端及APK(二进制文件)
  • 下载次数: 13
1
0
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics