thrift android后台,Android Thrift (Rpc)基于 volley 的自动生成 Android 代码

为什么要基于 Thrift

多个团队,多种技术,基于 Thrift 可以自动生成代码,统一规范,至于我们前后端的Thrift 文件即接口文档

封装

基于Thrift 生成后的代码,我们通过脚本封装了 Volley,在客户端可直接调用网络请求,不需要再写 url,解析 json 。一切都可以自动生成。

使用案例

Thrift 文件

service User {

///

/// 用户登录

///

/// 用户名/邮箱

/// 密码

/// 用户信息

TCustomer Login(1:string nickName, 2:string password),

}

通过我们的脚本生成后的Java代码

Android 代码

public class UserService {

public static RpcRequest Login(String nickName, String password, Listener listener) {

String method = "User.Login";

RpcResponse res = new RpcResponse(method, TCustomer.class, listener);

ArrayList params = new ArrayList();

params.add(nickName);

params.add(password);

HashMap args = new HashMap();

args.put("id", 1);

args.put("method", method);

args.put("params", params);

RpcRequest req = new RpcRequest(1, TRpc.getInstance().getJsonRpcUrl(), res, args);

TRpc.getInstance().getQueue().add(req);

return req;

}

}

使用的时候直接调用

UserService.Login("testNickName", "password", new Response.Listener() {

@Override

public void onResponse(TCustomer response) {

//do something

}

});

就这么简单,你不需要关心你的 url是什么,也不需要解析你的 json,更不需要担心和后端定义的接口参数对不上,是不是很爽!

当然其中会有一个TRpc类的封装

public class TRpc {

private RequestQueue queue;

private String jsonRpcUrl;

private String customerCookie;

private String userAgent;

private Map mapHeader;

public ILog logJson;

public IVolleyError volleyError;

private static TRpc tRpc;

public synchronized static TRpc getInstance() {

if (tRpc == null) {

tRpc = new TRpc();

}

return tRpc;

}

public void setVolleyError(IVolleyError volleyError) {

this.volleyError = volleyError;

}

public RequestQueue getQueue() {

if (queue == null) {

throw new NullPointerException("TRpc.setQueue must be called before using!!");

}

return queue;

}

public void setQueue(RequestQueue queue) {

if (queue == null) {

throw new NullPointerException("Parameter queue must not be null!!");

}

this.queue = queue;

}

public String getJsonRpcUrl() {

if (jsonRpcUrl == null || jsonRpcUrl.isEmpty()) {

throw new NullPointerException("TRpc.setJsonRpcUrl must be called before using!!");

}

return jsonRpcUrl;

}

public void setJsonRpcUrl(String jsonRpcUrl) {

if (jsonRpcUrl == null || jsonRpcUrl.isEmpty()) {

throw new NullPointerException("Parameter jsonRpcUrl must not be null!!");

}

this.jsonRpcUrl = jsonRpcUrl;

}

public String getCustomerCookie() {

return customerCookie;

}

public void setCustomerCookie(String customerCookie) {

this.customerCookie = customerCookie;

}

public String getUserAgent() {

return userAgent;

}

public void setUserAgent(String userAgent) {

this.userAgent = userAgent;

}

public void setLogJson(ILog log) {

if (log != null)

this.logJson = log;

}

public void setAppendHeader(Map map){

this.mapHeader = map;

}

public Map getAppendHeader(){

if(mapHeader != null && mapHeader.size() > 0){

return mapHeader;

}

return new HashMap();

}

public static class builder {

TRpc tRpc;

public builder() {

tRpc = TRpc.getInstance();

}

public builder setQueue(RequestQueue queue) {

tRpc.queue = queue;

return this;

}

public builder setJsonRpcUrl(String jsonRpcUrl) {

tRpc.jsonRpcUrl = jsonRpcUrl;

return this;

}

public builder setCustomerCookie(String cookie) {

tRpc.customerCookie = cookie;

return this;

}

public builder setVolleyError(IVolleyError volleyErro) {

tRpc.volleyError = volleyErro;

return this;

}

public builder setLogJson(ILog log) {

tRpc.logJson = log;

return this;

}

public builder appendHeader(Map map) {

tRpc.mapHeader = map;

return this;

}

public TRpc build() {

return tRpc;

}

}

public interface ILog {

void LogHttp(Object json);

void LogString(String string);

}

public interface IVolleyError {

void onVolleyError(VolleyError error, String requestURL);

}

使用前在项目的Application中初始化一下就可以了。

如有问题欢迎交流~

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值