最近开发项目中使用WebService,客户端是Android,服务端是WebService。在过程中遇到了很多问题,java程序和WebService连接的时候是基本上没有问题,但是Android连接的时候出现了各种状况。这里通过一个简单的例子分享下我的经验。Hessian是一个轻量级架包,非常好用。
准备工作
安卓端需要导入1.3两个jar文件(hessdroid是安卓专用的)
需要的可以去这里下载hessdroid和hessian这两版本是一直的,所以不会报最后的异常。
服务端只需要第2个jar文件
Hibernate+Springmvc+hessian整合架包
有需要所有Hibernate+Springmvc+hessian整合架包的可以去这里下载Hibernate+Springmvc+hessian
服务端
服务端整合了Hibernate,Spring,Hessian。首先来张工程基本图片吧。为了新手看起来容易就没有用我的工程,而是临时搭建了一个简单的,方便理解。 服务端有5步,走起……
首先是HelloIf.java 是interface暴露给客户端
package com.yijia.hessianspring.If;
public interface HelloIf {
public String Hello(String arg1,String arg2);
}
HelloImpls.java 是helloIf的实现
package com.yijia.hessianspring.Impls;
import com.yijia.hessianspring.If.HelloIf;
public class HelloImpls implements HelloIf {
public String Hello(String name,String content){
return "Hello:" + name + content;
}
}
applicationContext.xml 配置文件
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd">
<bean id="helloImpls" class="com.wtang.isayImpls.HelloImpls">
</bean>
</beans>
remote-servlet.xml的配置文件
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
<!-- 引入spring配置文件 -->
<import resource="applicationContext.xml"></import>
<!-- 接口的具体实现类 --> <!-- 这里可以定义Bean 但是我在applicationContext.xml中定义 -->
<!-- 使用Spring的HessianServie做代理 -->
<bean name="/helloSpring"
class="org.springframework.remoting.caucho.HessianServiceExporter">
<!-- service引用具体的实现实体Bean-->
<property name="service" ref="helloImpls" />
<property name="serviceInterface" value="com.yijia.hessianspring.If.HelloIf" />
</bean>
<!-- 这里可以配置多个HessianServie代理Bean-->
</beans>
最主要的web.xml(这里客户端是Android所以没有客户端没有整合Spring,所以和整合Spring的配置不一样)
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
<display-name>HessianSpring</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
<servlet>
<servlet-name>remote</servlet-name>
<!-- 使用Spring的代理Servlet -->
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>namespace</param-name>
<param-value>classes/remote-servlet</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>remote</servlet-name>
<url-pattern>/remote/*</url-pattern>
</servlet-mapping>
</web-app>
最后一步就是导出本工程的jar.也是最重要的一步
- 工程右键—>Export
- 选择JAR file 打出本工程的jar文件。
注:服务端全部结束,要注意的是配置文件,错了太麻烦了。细心细心
客户端
1.客户端导入jar, 刚导出的本工程jar和Hessdroid.jar
客户端Activity代码块
String url = "http://localhost:80/HessianSpring/remote/helloSpring";
System.out.println(url);
HessianProxyFactory factory = new HessianProxyFactory();
HelloIf helloIf = null;
try {
helloIf = (HelloIf) factory.create(HelloIf.class, url);
} catch (MalformedURLException e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
}
System.out.println(helloIf.Hello("tian", "你好"));
执行结果
异常处理
Exception in thread "main" com.caucho.hessian.client.HessianRuntimeException: com.caucho.hessian.io.HessianProtocolException: expected hessian reply at 0x48 (H)
H
- 只要是看到 0x? (H)的错误基本上都是服务器和客户端用的hessian.jar版本不同的问题。只需要换服务端的jar文件即可
com.caucho.hessian.io.HessianProtocolException: expected hessian reply at end of file
?
- 这是因为接口中出现方法重载,在调用时,服务器端会跑出异常。
在整合spring中,在客户端的配置里面加上如下代码可以解决:
<property name="overloadEnabled" value="true"></property>