【你不知道的android】-【hessian】
一 、背景
android项目不会去直接连接远程数据库,你懂的。
一般都是调用接口,接口的技术实现多种多样,这次来介绍下hessian。
二 、hessian在这里你能找到你所需要的文档,jar包,各种语言的实现。
这里主要看java的实现。
三 、目标做一个类似微博api的接口,供android程序调用。
可参考腾讯微博api,如下
urlhttp://open.t.qq.com/api/t/showhttps://open.t.qq.com/api/t/show (oauth2.0使用)支持验证方式oauth1.0、oauth2.0、openid&openkey格式xml,jsonhttp请求方式get是否需要鉴权true请求数限制true, 查看API调用权限说明接口测试点击这里测试我们做一个比较简单的,不带认证,仅支持json格式的数据返回。
四 、code
1、 api
urlhttp://localhost:9999/hessianServer/show例子http://localhost:9999/hessianServer/show?name=abc格式jsonhttp请求方式post2、server
Creating a Hessian service using Java has four steps:
- Create an Java interface as the public API Create a client using HessianProxyFactoryCreate the Service implementation classConfigure the service in your servlet engine.
public interface IShow {
String show();
}
实现接口,继承hession 配置servlet3、client
调用
public class Client {
public static void main(String[] args) { String url = “http://localhost:9999/hessianServer/show?name=abc“;
HessianProxyFactory factory = new HessianProxyFactory(); IShow show; try { show = (IShow) factory.create(IShow.class,url);
String str = show.show(); System.out.println(str); } catch (MalformedURLException e) { e.printStackTrace(); }
} }
注:
hessian仅支持post方式,url访问,会有提示,查看hessian源码可以看见。
hessian的HessianServlet如下:
public class HessianServlet extends GenericServlet
继承了Servlet,你懂的。
hessian会先执行service方法,然后才去直接借口的方法。
五、下载
点我下载