首先我们先导入Ksoap的架包
ksoap2-android-assembly-3.5.0-jar-with-dependencies.jar
在APP文件中的build.gradle我们进行添加
//SOAP的架包
compile files('libs/ksoap2-android-assembly-3.5.0-jar-with-dependencies.jar')
然后我们将我们的配置参数进行配置
// 命名空间
String nameSpace = "http://wangjiansk.gicp.net/";
// 调用方法的名称
String methodName = "LoginState";
// EndPoint
String endPoint = "http://wangjiansk.gicp.net/pmservice.asmx";
// SOAP Action
String soapAction = "http://wangjiansk.gicp.net/pmservice.asmx/LoginState";
// 指定WebService的命名空间和调用方法
SoapObject soapObject = new SoapObject(nameSpace, methodName);
// 设置需要调用WebService接口的两个参数
soapObject.addProperty("userName", name);
soapObject.addProperty("userPass", password);
下来我们生成调用WebService方法调用Soap信息,并设置号WebService的版本
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
SoapEnvelope.VER12);
envelope.bodyOut = soapObject;
// 是否调用DotNet开发的WebService
envelope.dotNet = true;
envelope.setOutputSoapObject(soapObject);
同时我们进行创建初始化HttpTransportSE,并进行打Call操作
HttpTransportSE transport = new HttpTransportSE(endPoint);
try {
transport.call(soapAction, envelope);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (XmlPullParserException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
到这里我们的请求就结束了,下来就可以获取返回的数据了
// 获取返回的数据
SoapObject object = (SoapObject) envelope.bodyIn;
// 获取返回的结果
String result = object.getProperty(0).toString();
Message message = handler.obtainMessage();
message.obj = result;
handler.sendMessage(message);
然后我们贴上全部的代码:
public void XXX() {
new Thread(new Runnable() {
@Override
public void run() {
// TODO Auto-generated method stub
// 命名空间
String nameSpace = "http://wangjiansk.gicp.net/";
// 调用方法的名称
String methodName = "LoginState";
// EndPoint
String endPoint = "http://wangjiansk.gicp.net/pmservice.asmx";
// SOAP Action
String soapAction = "http://wangjiansk.gicp.net/pmservice.asmx/LoginState";
// 指定WebService的命名空间和调用方法
SoapObject soapObject = new SoapObject(nameSpace, methodName);
// 设置需要调用WebService接口的两个参数
soapObject.addProperty("userName", name);
soapObject.addProperty("userPass", password);
// 生成调用WebService方法调用的soap信息,并且指定Soap版本
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
SoapEnvelope.VER12);
envelope.bodyOut = soapObject;
// 是否调用DotNet开发的WebService
envelope.dotNet = true;
envelope.setOutputSoapObject(soapObject);
HttpTransportSE transport = new HttpTransportSE(endPoint);
try {
transport.call(soapAction, envelope);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (XmlPullParserException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
// 获取返回的数据
SoapObject object = (SoapObject) envelope.bodyIn;
for (int i = 0; i < object.getPropertyCount(); i++) {
SoapObject child = (SoapObject) object.getProperty(i);
XX = child.getProperty(0).toString();
XX = child.getProperty(1).toString();
XX= Integer.parseInt(code);
}
Message message = handler.obtainMessage();
message.obj = XX;
handler.sendMessage(message);
}
}).start();
}
private Handler handler = new Handler() {
public void handleMessage(android.os.Message msg1) {
ToastUtils.showLong(msg);
}
};
我们最后要在AndroidManifest.xml中添加它的网络请求权限
<!-- 访问网络,网络定位需要上网 -->
<uses-permission android:name="android.permission.INTERNET" />
还有如果你在测试的时候遇到了android webservice ksoap 报错 org.xmlpull.v1.XmlPullParserException: expected: START_TAG
首先你得手机必须要连接外网,或者打开流量