Thrift client on Android

倾然丶 夕夏残阳落幕 提交于 2019-11-29 23:35:33

问题


I'm new to android development and want to create an app with a thrift client which is using a thrift server on my network. I already got the thrift definition file and the java code generated by the thrift compiler.

  1. How do I need to start? How do I add the thrift library to my project? Do I need to compile it myself and if yes how should I accomplish this to be android compatible?

  2. Can I directly use the auto generated functions similar to this

    public void getProduct(int productID, org.apache.thrift.async.AsyncMethodCallback resultHandler)
    

    to make asynchronous calls to my service so that I follow the android best practices? Or does using this function block my application in any way or is there another way to be preferred over this on android?

I use the current version of googles ADT (eclipse). My app needs to run just on android 4+ devices (I set the minSdkVersion to 15).


回答1:


  1. yes, you need to build the thrift-lib for Android by yourself. Android has it's own httpcore that doesn't have a consume-method. here you can find how to change the thrift-library: here instruction After that just add the jar-file to the libs-folder and add it to the build-path.

  2. example

    THttpClient hclient = new THttpClient("your/endpoint/url");
    TProtocol protocol = new TBinaryProtocol(hclient); // it depends on your data-format
    Service.Client client = new Service.Client(protocol);
    Product product = client.getProduct(int productID);
    

you need to execute it as an Android AsyncTask: AsyncTask | Android Developers




回答2:


Sorry to pile on to an old question. As of February 2016, there is an Android-specific Thrift compiler and client: https://github.com/Microsoft/thrifty.

The classes that it generates use as few methods as possible (much smaller than the Apache implementation), and the generated clients are asynchronous by default.



来源:https://stackoverflow.com/questions/19141177/thrift-client-on-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!