在Android中访问WebService接口的方法
程序员文章站
2023-12-06 10:25:40
需要引入ksoap2-android-assembly-2.5.2-jar-with-dependencies.jar复制代码 代码如下: //webservice的命名...
需要引入ksoap2-android-assembly-2.5.2-jar-with-dependencies.jar
//webservice的命名空间
static final string namespace = "http://impl.service.suncreate.com";
//服务器发布的url
static final string url = http://10.100.3.41/axis2/services/uploadservice;
final string methodname = "upload"; // 函数名
final int sessionid = "111111"; //sessionid
//创建httptransportse对象,通过httptransportse类的构造方法可以指定webservice的url
httptransportse transport = new httptransportse(url);
transport.debug = true;
//指定webservice的命名空间和函数名
soapobject soapobject = new soapobject(namespace, methodname);
//设置调用方法参数的值
soapobject.addproperty("sessionid", sessionid); //sessionid
soapobject.addproperty("data", cds); //cds是需要传递的对象
soapserializationenvelope envelope = new soapserializationenvelope(soapenvelope.ver10);
envelope.bodyout = transport;
envelope.setoutputsoapobject(soapobject);
//使用call方法调用webservice方法
transport.call(null, envelope);
soapobject sb = (soapobject) envelope.bodyin;
string xmlmessage = sb.tostring(); // 获取从服务器端返回的xml字符串
复制代码 代码如下:
//webservice的命名空间
static final string namespace = "http://impl.service.suncreate.com";
//服务器发布的url
static final string url = http://10.100.3.41/axis2/services/uploadservice;
final string methodname = "upload"; // 函数名
final int sessionid = "111111"; //sessionid
//创建httptransportse对象,通过httptransportse类的构造方法可以指定webservice的url
httptransportse transport = new httptransportse(url);
transport.debug = true;
//指定webservice的命名空间和函数名
soapobject soapobject = new soapobject(namespace, methodname);
//设置调用方法参数的值
soapobject.addproperty("sessionid", sessionid); //sessionid
soapobject.addproperty("data", cds); //cds是需要传递的对象
soapserializationenvelope envelope = new soapserializationenvelope(soapenvelope.ver10);
envelope.bodyout = transport;
envelope.setoutputsoapobject(soapobject);
//使用call方法调用webservice方法
transport.call(null, envelope);
soapobject sb = (soapobject) envelope.bodyin;
string xmlmessage = sb.tostring(); // 获取从服务器端返回的xml字符串