基于soap协议和webservice的客户端与服务器通信

最近在做一个android的程序,因为要用到客户端之间的通信,所以就看了一些,这里的Webservice是以前经常用的一种。主要是通过soap协议,其中我们主要要用到两个包一个是ksoap的支持包,一个是axis2的支持包。这里写了一个登陆的例子。

下面的是服务器端的例子这里是我用到的一些包,主要是为了让结构更加的清晰,分别将数据的访问,逻辑的控制,和给服务器提供接口放在了不同的包里。

这里操作就不搭建数据库,默认一个账号密码进行模拟登陆。

//**********服务器端**************************************

/**
 * 
 */
package org.lei.model;


/**
 * @author renlei
 * @date 2014-2-24 下午5:00:08 
 * 实体类
 */


public class User {
/**
* 账号
*/
private String account;
/**
* 密码
*/
private String password;
public String getAccount() {
return account;
}
public void setAccount(String account) {
this.account = account;
}
public String getPassword() {
return password;
}


public void setPassword(String password) {
this.password = password;
}
public User(String account, String password) {
super();
this.account = account;
this.password = password;
}

public User() {
super();
// TODO Auto-generated constructor stub
}


}


//************************************


//***************************************

package org.lei.daoimpl;


import org.lei.dao.UserDao;




/**
 * @author renlei
 * @date 2014-2-24 下午4:57:42 
 * 实现数据库的访问函数
 */


public class UserDaoImpl implements UserDao{


/* (non-Javadoc)
* @see org.lei.dao.UserDao#Login(java.lang.String, java.lang.String)
*/
public boolean Login(String account, String passwrod) {
// TODO Auto-generated method stub
if(account.equals("renlei")&&passwrod.equals("123456"))
return true;

return false;
}




}


//*******************

/**
 * 
 */
package org.lei.func;


import org.lei.daoimpl.UserDaoImpl;


/**
 * @author renlei
 * @date 2014-2-24 下午5:04:17 
 * 该类中可以放一些逻辑处理
 */
public class UserFunc {
/**

* @param account
* @param password
* @return
*/
public boolean Login(String account,String password){

return new UserDaoImpl().Login(account, password);
}
}

//*********************

/**
 * 
 */
package org.lei.forclient;


import org.lei.func.UserFunc;


/**
 * @author renlei
 * @date 2014-2-24 下午5:06:34 
 * 该类用于发布给客户端调用
 */
public class ForServer {
/**

* @param account
* @param password
* @return
*/
public boolean Login(String account,String password){
System.out.println(account.toString()+password);
return new UserFunc().Login(account, password);
}
}

//************************************客户端****************************



这里自己封装了一个soap的类
package org.lei.model;


import java.io.Serializable;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
import org.ksoap2.SoapEnvelope;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransportSE;




//import android.util.Log;


public class Soap implements Serializable{
/**
*/
private static final long serialVersionUID = 1L;
//所需要访问的地址
String url = "http://172.4.167.104:8080/";
//工程名
String project = "WebServiceDemo";
//类名
String service;
//命名空间
String namespace;
//所调用的方法,名
String webMethod;
String soapAction;
public Soap(String service, String webMethod, String namespace) {
super();
url = "http://172.4.167.104:8080/";
this.service = service;
this.webMethod = webMethod;
this.namespace = namespace;
}




/**
* @return
*/
public Object getResult(Map<String, Object> propertys){
String wsdlUrl = url + project + "/services/" + service;
//命名空间+方法名
soapAction = namespace + webMethod;
System.out.println(soapAction+"***");
SoapObject soapObject = new SoapObject(namespace, webMethod);
Set<String> set = propertys.keySet();
for(Iterator<String> it = set.iterator();it.hasNext();){
String propertyKey = it.next();
Object value = propertys.get(propertyKey);
soapObject.addProperty(propertyKey, value);
//Log.v(propertyKey, value.toString());
}
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
SoapEnvelope.VER11);
envelope.bodyOut = soapObject;


HttpTransportSE se = new HttpTransportSE(wsdlUrl);
try {
se.call(soapAction, envelope);
if (envelope.getResponse() != null) {
Object obj = envelope.getResponse();
return obj;
}
} catch (Exception e) {
e.printStackTrace();
return null;
}
return null;
}
}


//在客户端类进行调用
/**
 * 
 */
package org.lei.client;


import java.util.LinkedHashMap;
import java.util.Map;


import javax.jws.soap.SOAPBinding.Use;


import org.lei.model.Soap;
import org.lei.model.User;


/**
 * @author renlei
 * @date 2014-2-24 下午5:49:58 
 */
public class WebServiceTest {

public static void main(String []arsg){

User user = new User("renlei", "123456");
Map<String, Object> properties = new LinkedHashMap<>();
properties.put("account", user.getAccount());
properties.put("password", user.getPassword());

Object resultObject = new Soap("ForServer", "Login", "http://forclient.lei.org").getResult(properties);
System.out.println(resultObject.toString());
if(Boolean.parseBoolean(resultObject.toString())==true){

System.out.println("登录成功");
}
else {
System.out.println("登录失败");
}



}
}


下面的链接可以下载到整个工程的资源以及配置文件等 http://download.csdn.net/detail/renlei0109/6958925

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值