Javaweb中利用rpc(hessian)与Android通信

时间:2021-11-10 05:36:51

一、服务端(tomcat)

  1.需要引入的包hessian-4.0.60.jar 下载链接:https://files.cnblogs.com/files/javabull/hessian-4.0.60.zip

  2.编写实体类

  

 1 package com.javabull.inter.entity;
 2 
 3 public class User {
 4     private String name;
 5     private String password;
 6     public String getName() {
 7         return name;
 8     }
 9     public void setName(String name) {
10         this.name = name;
11     }
12     public String getPassword() {
13         return password;
14     }
15     public void setPassword(String password) {
16         this.password = password;
17     }
18     public User(String name, String password) {
19         super();
20         this.name = name;
21         this.password = password;
22     }
23     public User() {
24         super();
25     }
26         
27 }

 

  3.编写接口com.javabull.inter.InterManager

    

1 package com.javabull.inter;
2 
3 import com.javabull.inter.entity.User;
4 
5 public interface InterManager {
6     public boolean loginCheck(User user);
7 }

 

  4.编写实现类com.javabull.inter.impl.InterManagerImpl

 1 package com.javabull.inter.impl;
 2 
 3 import com.javabull.inter.InterManager;
 4 import com.javabull.inter.entity.User;
 5 
 6 public class InterManagerImpl implements InterManager{
 7 
 8     @Override
 9     public boolean loginCheck(User user) {
10         boolean ret = false;
11         if(user!=null && user.getName()!=null && user.getPassword()!=null) {
12             if (user.getName().equals("javabull")&&user.getPassword().equals("123456")) {
13                 ret = true;
14             }
15         }
16         return ret;
17     }
18 
19 }

   5.配置web.xml文件

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 3     xmlns="http://java.sun.com/xml/ns/javaee"
 4     xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
 5     id="WebApp_ID" version="3.0">
 6 
 7     <display-name>TestHessian</display-name>
 8     <welcome-file-list>
 9         <welcome-file>index.jsp</welcome-file>
10     </welcome-file-list>
11     
12     <servlet>
13         <servlet-name>server</servlet-name>
14         <servlet-class>com.caucho.hessian.server.HessianServlet</servlet-class>
15         <init-param>
16             <!-- 接口实现 -->
17             <param-name>home-class</param-name>
18             <param-value>com.javabull.inter.impl.InterManagerImpl</param-value>
19         </init-param>
20         <init-param>
21             <!-- 接口 -->
22             <param-name>home-api</param-name>
23             <param-value>com.javabull.inter.InterManager</param-value>
24         </init-param>
25     </servlet>
26     <servlet-mapping>
27         <servlet-name>server</servlet-name>
28         <url-pattern>/server.do</url-pattern>
29     </servlet-mapping>
30 </web-app>

  二、Android客户端

    1..包的引入:https://files.cnblogs.com/files/javabull/android_hessian.zip

    2.使用

   1 InterManager interManager = (InterManager)HessianProxyFactory.create(InterManager.class,"http://192.168.1.153");  //之后即可调用接口中的方法了