Android platform Thrift server development summary

Introduction
  Thrift, not explained, can support distributed calls between codes in different languages, very easy to use, very powerful, the version used in this article is 0.9.3. Thrift server side, implemented using Android. The client is developed using C# and VS2015. The client sends a string to the server, and the server (Android's APK) displays the string on the interface.
  Author: http://wallimn.iteye.com Time: 2016-01-11.

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

2. Compile Hello .thrift file, execute the following compilation commands on the command line, and classify and generate auxiliary codes for JAVA and C#
thrift-0.9.3.exe -gen java Hello.thrift
thrift-0.9.3.exe -gen csharp Hello.thrift


3. In Add the files generated by thrift compilation to the Android project, add the jar package of thrift, and define the implementation class of the service interface
    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);//Send a message to the main thread to display the received information
        }
    }


4. The layout of the Activity is very simple. There is only one TextView. Use tvMsg to save the variables corresponding to the View and display the received messages. The source code is not given here.

5. Define the message handler member variable in the main interface (MainActivity).
    Handler handler = new Handler () {
        public void handleMessage(Message msg){
            if(msg.what==0x8090){
               String str= (String) msg.getData().get("msg");
               tvMsg.setText(str);//TextView element on Activity displays message content
            }
        }
    };


6. The code to start the thrift server: define a thread variable in MainActivity, pay attention to defining it as a member variable, and start the thread in the Activity creation function
    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,"Service start, 8090",Toast.LENGTH_LONG).show();
                server.serve();
            } catch (TTransportException e) {
                e.printStackTrace ();
            }
        }
    };    



7. Test the client, use C# to write, pay attention to add the files generated by thrift compilation into the project, and refer to thrift. The test client is a simple form with two TextBoxes, which are used to input the IP of the server, the message sent, and a button.
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();
            }

        }
    }
}

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=327040434&siteId=291194637