最近有一个项目,客户端与服务器的交互是webService方式。现将ksoap2android调用webService的方法记录如下,最后再讨论一下Http方式直接调用的方法。
以下的调用都是java代码,移植到android下不需要改动任何内容。
例子中调用的webService为http://webservice.webxml.com.cn/webservices/qqOnlineWebService.asmx?wsdl
一、使用ksoap2android方法调用 。代码如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
|
/**
* 以soap方式调用WebService
* @param mothed
* @param propertyInfos
* @return
*/
publicstaticStringcallWebServiceBySoap(Stringmothed,
PropertyInfo...propertyInfos){
// 指定WebService的命名空间和函数名
SoapObject soapObject=newSoapObject(NAMESPACE,mothed);
for(PropertyInfo info:propertyInfos){
soapObject.addProperty(info);
}
SoapSerializationEnvelope envelope=newSoapSerializationEnvelope(
SoapEnvelope.VER11);
envelope.dotNet=true;
// 创建HttpTransportSE对象,通过HttpTransportSE类的构造方法可以指定WebService的url
HttpTransportSE transport=newHttpTransportSE(URL);
transport.debug=true;
envelope.bodyOut=transport;
envelope.setOutputSoapObject(soapObject);
SoapObject result=null;
// 使用call方法调用WebService方法
try{
transport.call(NAMESPACE+mothed,envelope);
result=(SoapObject)envelope.bodyIn;
}catch(IOExceptione){
// TODO Auto-generated catch block
e.printStackTrace();
}catch(XmlPullParserExceptione){
// TODO Auto-generated catch block
e.printStackTrace();
}
returnresult.getProperty(0).toString();
}
|
二、使用Http方式调用。
Http调用WebService的时候,需要将调用的参数信息组合成为字符串,并且将其附加到Http报头中。所以,当有时候的确需要用Http方式调用的时候,可以先用ksoap调用一次,同时使用抓包工具分析请求的报头。然后再编写代码,以提高调用的成功率。注:抓包工具可以用HttpAnalyzerStdV7,比较好操作。
我在调用的时候,将参数信息写到了xml文件中,当然也可以自己构造一个字符串。这里就使用字符串的方法。
1、编写构造xml参数结构的方法:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
|
/**
* 传入一个调用的方法名和参数列表,得到一个xml字符串
*
* @param mothedName
* 调用webservice的方法名
* @param paramsMap
* 参数列表
* @return
*/
publicstaticStringgetPostPropertyXml(StringnameSpace,
StringmothedName,Map<String,String>paramsMap){
StringBuffer stringBuffer=newStringBuffer();
stringBuffer.append("<?xml version=\"1.0\"encoding=\"utf-8\"?>");
stringBuffer
.append("<soap:Envelope xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\"");
stringBuffer
.append(" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"");
stringBuffer
.append(" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\"><soap:Body>");
stringBuffer.append("<"+mothedName+" xmlns=\""+nameSpace+"\">");
for(Map.Entry<String,String>my:paramsMap.entrySet()){
stringBuffer.append("<"+my.getKey()+">"+my.getValue()+"</"
+my.getKey()+">");
}
stringBuffer.append("</"+mothedName+">");
stringBuffer.append("</soap:Body></soap:Envelope>");
returnstringBuffer.toString();
}
|
2、编写Http方式调用的方法:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
|
/**
* 以http的方式调用WebService
*
* @param urlString
* webservice的地址
* @param mothedName
* 调用的方法名,用于构造 soapaction
* @param xmls
* 提交的xml结构参数列表
* @throws Exception
*/
publicstaticStringcallWebServiceByHttp(StringurlString,
StringnameSpace,StringmothedName,Map<String,String>map)
throwsException{
URL url=newURL(urlString);
HttpURLConnection httpConn=(HttpURLConnection)url.openConnection();
byte[]buf=getPostPropertyXml(nameSpace,mothedName,map).getBytes();// 先构造xml字符串
httpConn.setRequestProperty("Content-Length",
String.valueOf(buf.length));
httpConn.setRequestProperty("Content-Type","text/xml; charset=utf-8");
httpConn.setRequestProperty("SOAPAction",HttpAPI.NAMESPACE
+mothedName);
httpConn.setRequestMethod("POST");
httpConn.setDoOutput(true);
httpConn.setDoInput(true);
OutputStream out=httpConn.getOutputStream();
out.write(buf);
out.close();
byte[]datas=readInputStream(httpConn.getInputStream());
Stringresult=newString(datas);
returnresult;
}
/**
* 从输入流中读取数据
*
* @param inStream
* @return
* @throws Exception
*/
publicstaticbyte[]readInputStream(InputStream inStream)throwsException{
ByteArrayOutputStream outStream=newByteArrayOutputStream();
byte[]buffer=newbyte[1024];
intlen=0;
while((len=inStream.read(buffer))!=-1){
outStream.write(buffer,0,len);
}
byte[]data=outStream.toByteArray();// 网页的二进制数据
outStream.close();
inStream.close();
returndata;
}
|
三、调用的代码:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
|
/**
* soap调用方法
*/
PropertyInfo propertyInfo=newPropertyInfo();
propertyInfo.setName("qqCode");
propertyInfo.setValue("8698053");
StringsoapResult=callWebServiceBySoap(HttpAPI.URL,
HttpAPI.NAMESPACE,"qqCheckOnline",propertyInfo);
System.out.println(soapResult);
/**
* http方式调用方法
*/
Map<String,String>map=newLinkedHashMap<String,String>();
map.put("qqCode","8698053");
StringxmlResult=callWebServiceByHttp(HttpAPI.URL,HttpAPI.NAMESPACE,
"qqCheckOnline",map);// 调用webService
System.out.println(xmlResult);
|
HttpAPI类的内容:
|
publicclassHttpAPI{
publicstaticfinalStringNAMESPACE="http://WebXml.com.cn/";
publicstaticfinalStringURL="http://webservice.webxml.com.cn/webservices/qqOnlineWebService.asmx?wsdl";
}
|
最终调用的结果为:
soap方式返回结果:
Http方式返回结果:
|
<?xml version="1.0"encoding="utf-8"?>
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<soap:Body>
<qqCheckOnlineResponse xmlns="http://WebXml.com.cn/">
<qqCheckOnlineResult>Y</qqCheckOnlineResult>
</qqCheckOnlineResponse>
</soap:Body>
</soap:Envelope>
|
两种调用方法的不同点:
soap方式返回的是结果的字符串,而Http方式返回的结果是一个xml,需要自己进行解析。项目中可根据需求选择使用哪种请求方式。
留言列表