【您不知道的android】-【hessian】
一 、背景
android项目不会去直接连接远程数据库,你懂的。
一般都是调用接口,接口的技术实现多种多样,这次来介绍下hessian。
二 、hessian
在这里你能找到你所需要的文档,jar包,各种语言的实现。
这里主要看java的实现。
三 、目标
做一个类似微博api的接口,供android程序调用。
可参考腾讯微博api,如下
url | http://open.t.qq.com/api/t/showhttps://open.t.qq.com/api/t/show (oauth2.0使用) |
支持验证方式 | oauth1.0、oauth2.0、openid&openkey |
格式 | xml,json |
http请求方式 | get |
是否需要鉴权 | true |
请求数限制 | true, 查看API调用权限说明 |
接口测试 | 点击这里测试 |
我们做一个比较简单的,不带认证,仅支持json格式的数据返回。
四 、code
1、 api
url | http://localhost:9999/hessianServer/show |
例子 | http://localhost:9999/hessianServer/show?name=abc |
格式 | json |
http请求方式 | post |
2、server
Creating a Hessian service using Java has four steps:
- Create an Java interface as the public API
- Create a client using HessianProxyFactory
- Create the Service implementation class
- Configure the service in your servlet engine.
- 创建一个接口
public interface IShow {
String show();
}
- 实现接口,继承hession
- 配置servlet
3、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方法,然后才去直接借口的方法。
五、下载
点我下载