1,导包
下载ksoap2-android-assembly-3.5.0-jar-with-dependencies.jar包,导入项目中libs文件夹
app的gradle中导入jar包
implementation files('libs/ksoap2-android-assembly-3.5.0-jar-with-dependencies.jar')
2,工具类
public class WebServiceUtils {
// 含有3个线程的线程池
private static final ExecutorService executorService = Executors
.newFixedThreadPool(3);
// 命名空间,默认是这个,可以更改,具体需要和后台人员确认
private static final String NAMESPACE = "http://accounts.ws.ddoc.com/";
/**
* @param url WebService服务器地址
* @param methodName WebService的调用方法名
* @param map WebService的参数
* @param webServiceCallBack 回调接口
*/
public static void callWebService(String url, final String methodName,
Map
map, final WebServiceCallBack webServiceCallBack) {
// 创建HttpTransportSE对象,传递WebService服务器地址
final HttpTransportSE httpTransportSE = new HttpTransportSE(url);
// 创建SoapObject对象
SoapObject soapObject = new SoapObject(NAMESPACE, methodName);
// SoapObject添加参数
if (map != null) {
for (Map.Entry
entry : map.entrySet()) { soapObject.addProperty(entry.getKey(), entry.getValue());
}
}
// 实例化SoapSerializationEnvelope,传入WebService的SOAP协议的版本号
final SoapSerializationEnvelope soapEnvelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
// 设置是否调用的是.Net开发的WebService
soapEnvelope.setOutputSoapObject(soapObject);
soapEnvelope.dotNet = false;
httpTransportSE.debug = true;
// 用于子线程与主线程通信的Handler
@SuppressLint("HandlerLeak") final Handler mHandler = new Handler() {
@RequiresApi(api = Build.VERSION_CODES.O)
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
// 将返回值回调到callBack的参数中
try {
webServiceCallBack.callBack((SoapObject) msg.obj);
} catch (FileSystemException e) {
e.printStackTrace();
}
}
};
// 开启线程去访问WebService
executorService.submit(new Runnable() {
@Override
public void run() {
SoapObject resultSoapObject = null;
try {
httpTransportSE.call(NAMESPACE + methodName, soapEnvelope);
if (soapEnvelope.getResponse() != null) {
// 获取服务器响应返回的SoapObject
Log.i("获取服务器返回的表示", soapEnvelope.getResponse() + "");
resultSoapObject = (SoapObject) soapEnvelope.bodyIn;
}
} catch (HttpResponseException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (XmlPullParserException e) {
e.printStackTrace();
} finally {
// 将获取的消息利用Handler发送到主线程
mHandler.sendMessage(mHandler.obtainMessage(0,
resultSoapObject));
}
}
});
}
public interface WebServiceCallBack {
void callBack(SoapObject result) throws FileSystemException;
}
3.LoginActivity调用登录
//WebService服务器地址
private String url= "http://dev.ddoc.com/DDoc-Webservices/AccountWS";
//WebService的调用方法名
private String methodName= "Login";
//请求参数
private Map
map_login= new HashMap<>(); map_login.put("AccountType", "D");
map_login.put("AccountName", et_login_zhanghao.getText().toString());
map_login.put("Password", et_login_mima.getText().toString());
WebServiceUtils.callWebService(url, methodName, map_login, new WebServiceUtils.WebServiceCallBack() {
@Override
public void callBack(SoapObject result) {
if (result!= null) {
*解析Soap XML文档
2017-05-03T11:43:59.664+08:00
a207008a-757f-4f97-9fff-635d309c8798
70a5829395ba71255a117c573ec65fe5e2cfe08642efd7ac31585a8447828ce1
2017-05-03T12:43:59.601+08:00
Login success.
200
gppnbjqqJGD5KyM0qVNWkUkj9V2TrryW
900
* */
String Timestamp= result.getPropertySafelyAsString("Timestamp");//解析【Timestamp】节点
SoapObject soapWSMessage= (SoapObject) result.getProperty("WSMessage");//解析【WSMessage】节点
String Message= soapWSMessage.getProperty("Message").toString();//解析【WSMessage】节点下的【Message】节点
if ("200".equals(soapWSMessage.getProperty("Code").toString())) {
// success
Toast.makeText(LoginActivity.this, Message, Toast.LENGTH_LONG).show();
}
}
}
}
});