本帖最後由 KinKALaw 於 2017-3-5 10:56 編輯
1.參數準備
URL : 伺服器ws地址,要去掉?wsdl,並且儘量用功能變數名稱不要用IP NAMESPACE : 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介面,並且實現該幾口方法
|