(分享)Android(Ksoap2)調用Service(可傳遞自訂物件)
本帖最後由 KinKALaw 於 2017-3-5 10:56 編輯1.參數準備
URL : 伺服器ws地址,要去掉?wsdl,並且儘量用功能變數名稱不要用IPNAMESPACE : ws中targetNamespace="http://impl.webservice.cbst.ffcs.com/"METHOD_NAME : 需要調用的方法,該例中調用遠端usersCertificate方法
2.代碼部分HttpTransportSE transport = new HttpTransportSE(URL);
SoapObject soapObject = new SoapObject(NAMESPACE, METHOD_NAME);
//需要發送的物件需要做序列化
//創建使用者認證物件
UserCertificate uc = new UserCertificate();
uc.setParamType(Constant.USERCERTIFICATE_USERACCOUNT);
uc.setParamValue(user);
md5 md = new md5();
String md5Pwd = md.md5s(pwd);
uc.setPassword(md5Pwd);
uc.setQueryParams("userAccount,userArea,userName,mobileTelephone,carNumber,email,besttoneCard,idCard,isCertificate,PROPERTY|address");
//通過getDemoWsRequest()方法獲得WsRequest物件
WSRequest ws = getDemoWSRequest();
//設置param
ws.getParam().setUserCertificate(uc);
//設置TransactionName,一般為方法名
ws.setTransactionName(METHOD_NAME);
//將ws放入pi對象
PropertyInfo pi = new PropertyInfo();
//name為伺服器端需要調用的方法參數名稱
pi.setName("request");
pi.setValue(ws);
pi.setType(ws.getClass());
soapObject.addProperty(pi);
//做類型映射
//發送時對象不指定類型,envelope.implicitTypes = true;
envelope.addMapping(NAMESPACE, "WsRequest", ws.getClass());
envelope.addMapping(NAMESPACE, "Param", Param.class);
envelope.addMapping(NAMESPACE, "UserCertificate", UserCertificate.class);
//指定xml版本,以及request中body
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
SoapEnvelope.VER10);
envelope.bodyOut = transport;
envelope.setOutputSoapObject(soapObject);
//發送request
transport.call(null, envelope);
SoapObject sb = (SoapObject) envelope.bodyIn;
Object response = envelope.getResponse();
3.序列化需要傳送的物件注:需要產生實體的物件要實現KvmSerializable介面,並且實現該幾口方法public class WSRequest implements KvmSerializable {
private String transactionName;
private Header header;
private IdentityInfo identityInfo;
private ScrollingInfo scrollingInfo;
private Param param;
public Header getHeader() {
return header;
}
public void setHeader(Header header) {
this.header = header;
}
public IdentityInfo getIdentityInfo() {
return identityInfo;
}
public void setIdentityInfo(IdentityInfo identityInfo) {
this.identityInfo = identityInfo;
}
public Param getParam() {
return param;
}
public void setParam(Param param) {
this.param = param;
}
public ScrollingInfo getScrollingInfo() {
return scrollingInfo;
}
public void setScrollingInfo(ScrollingInfo scrollingInfo) {
this.scrollingInfo = scrollingInfo;
}
public String getTransactionName() {
return transactionName;
}
public void setTransactionName(String transactionName) {
this.transactionName = transactionName;
}
@Override
public Object getProperty(int arg0) {
switch (arg0) {
case 0:
return transactionName;
case 1:
return header;
case 2:
return identityInfo;
case 3:
return scrollingInfo;
case 4:
return param;
}
return null;
}
@Override
public int getPropertyCount() {
// TODO Auto-generated method stub
return 5;
}
@Override
public void getPropertyInfo(int arg0, Hashtable arg1, PropertyInfo arg2) {
switch (arg0) {
case 0:
arg2.type = PropertyInfo.STRING_CLASS;
arg2.name = "transactionName";
break;
case 1:
arg2.type = header.getClass();
arg2.name = "header";
break;
case 2:
arg2.type = identityInfo.getClass();
arg2.name = "identityInfo";
break;
case 3:
arg2.type = scrollingInfo.getClass();
arg2.name = "ScrollingInfo";
break;
case 4:
arg2.type = param.getClass();
arg2.name = "param";
break;
default:
break;
}
}
@Override
public void setProperty(int arg0, Object arg1) {
switch (arg0) {
case 0:
transactionName = arg1.toString();
break;
case 1:
header = (Header) arg1;
break;
case 2:
identityInfo = (IdentityInfo) arg1;
break;
case 3:
scrollingInfo = (ScrollingInfo) arg1;
break;
case 4:
param = (Param) arg1;
break;
default:
break;
}
}
}
http://www.cnblogs.com/zhangchaoyang/articles/1955834.html詳細可參考該博客 http://ksoap2.sourceforge.net/doc/api/Ksoap APIhttp://www.cnasm.com/view.asp?classid=49&newsid=320 可以用這個軟體調試
頁:
[1]