基于soap协议和webservice的客户端与服务器通信
最近在做一个android的程序,因为要用到客户端之间的通信,所以就看了一些,这里的Webservice是以前经常用的一种。主要是通过soap协议,其中我们主要要用到两个包一个是ksoap的支持包,一个是axis2的支持包。这里写了一个登陆的例子。 下面的是服务器端的例子 这里操作就不搭建数据库,默认一个账号密码进行模拟登陆。 //**********服务器端************************************** /** //************************************ //*************************************** package org.lei.daoimpl; //******************* /** //********************* /** //************************************客户端****************************
这里自己封装了一个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
(编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |