【你不知道的android】-【hessian】

来源:互联网 发布:大数据 思维变革 编辑:程序博客网 时间:2024/05/16 15:30

一 、背景

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请求方式post

2、server

Creating a Hessian service using Java has four steps:

  1. Create an Java interface as the public API 
  2. Create a client using HessianProxyFactory
  3. Create the Service implementation class
  4. 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方法,然后才去直接借口的方法。

 

五、下载

 

点我下载

原创粉丝点击