为什么要基于 Thrift
多个团队,多种技术,基于 Thrift 可以自动生成代码,统一规范,至于我们前后端的Thrift 文件即接口文档
封装
基于Thrift 生成后的代码,我们通过脚本封装了 Volley,在客户端可直接调用网络请求,不需要再写 url,解析 json 。一切都可以自动生成。
使用案例
Thrift 文件
service User {
/// <summary>
/// 用户登录
/// </summary>
/// <param name="nickName">用户名/邮箱</param>
/// <param name="password">密码</param>
/// <returns>用户信息</returns>
TCustomer Login(1:string nickName, 2:string password),
}
通过我们的脚本生成后的Java代码
Android 代码
public class UserService {
public static RpcRequest Login(String nickName, String password, Listener<TCustomer> listener) {
String method = "User.Login";
RpcResponse res = new RpcResponse(method, TCustomer.class, listener);
ArrayList<Object> params = new ArrayList();
params.add(nickName);
params.add(password);
HashMap<String, Object> 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<TCustomer>() {
@Override
public void onResponse(TCustomer response) {
//do something
}
});
就这么简单,你不需要关心你的 url是什么,也不需要解析你的 json,更不需要担心和后端定义的接口参数对不上,是不是很爽!
此处应有自动生成代码脚本的 GitHub 地址
https://github.com/RenshawPeng/tgen
当然其中会有一个TRpc
类的封装
public class TRpc {
private RequestQueue queue;
private String jsonRpcUrl;
private String customerCookie;
private String userAgent;
private Map<String,String> 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<String,String> map){
this.mapHeader = map;
}
public Map<String,String> getAppendHeader(){
if(mapHeader != null && mapHeader.size() > 0){
return mapHeader;
}
return new HashMap<String,String>();
}
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<String,String> 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
中初始化一下就可以了。
如有问题欢迎交流~
网友评论