首先,说一下适用范围:
当使用java版的webservice的时候,可以直接使用KSOAP2这个类库,直接调用其方法就可以了。具体方法可以自行Google。
当使用.NET版的webservice的时候,KSOAP2这个类库不是很好用,我研究了一个下午没研究明白(可能是方法问题吧,欢迎大家共同交流)。 目的是获取并解析复杂类型的返回值。
1.HttpConnSoap
[java]
<span style="font-size:14px;">package com.bottle.stockmanage;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;
public class HttpConnSoap {
public ArrayList<String> GetWebServre(String methodName, ArrayList<String> Parameters, ArrayList<String> ParValues) {
ArrayList<String> Values = new ArrayList<String>();
//ServerUrl是指webservice的url
//10.0.2.2是让android模拟器访问本地(PC)服务器,不能写成127.0.0.1
//11125是指端口号,即挂载到IIS上的时候开启的端口
//Service1.asmx是指提供服务的页面
String ServerUrl = "http://10.0.2.2:11125/Service1.asmx";
//String soapAction="http://tempuri.org/LongUserId1";
String soapAction = "http://tempuri.org/" + methodName;
//String data = "";
String soap = "<?xml version=\"1.0\" encoding=\"utf-8\"?>"
+ "<soap:Envelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\">"
+ "<soap:Body />";
String tps, vps, ts;
String mreakString = "";
mreakString = "<" + methodName + " xmlns=\"http://tempuri.org/\">";
for (int i = 0; i < Parameters.size(); i++) {
tps = Parameters.get(i).toString();
//设置该方法的参数为.net webService中的参数名称
vps = ParValues.get(i).toString();
ts = "<" + tps + ">" + vps + "</" + tps + ">";
mreakString = mreakString + ts;
}
mreakString = mreakString + "</" + methodName + ">";
/*
+"<HelloWorld xmlns=\"http://tempuri.org/\">"
+"<x>string11661</x>"
+"<SF1>string111</SF1>"
+ "</HelloWorld>"
*/
String soap2 = "</soap:Envelope>";
String requestData = soap + mreakString + soap2;
//System.out.println(requestData);
try {
URL url = new URL(ServerUrl);
HttpURLConnection con = (HttpURLConnection) url.openConnection();
byte[] bytes = requestData.getBytes("utf-8");
con.setDoInput(true);
con.setDoOutput(true);
con.setUseCaches(false);
con.setConnectTimeout(6000);// 设置超时时间
con.setRequestMethod("POST");
con.setRequestProperty("Content-Type", "text/xml;charset=utf-8");
con.setRequestProperty("SOAPAction", soapAction);
con.setRequestProperty("Content-Length", "" + bytes.length);
OutputStream outStream = con.getOutputStream();
outStream.write(bytes);
outStream.flush();
outStream.close();
InputStream inStream = con.getInputStream();
//data=parser(inStream);
//System.out.print("11");
Values = inputStreamtovaluelist(inStream, methodName);
//System.out.println(Values.size());
return Values;
} catch (Exception e) {
System.out.print("2221");
return null;
}
}
public ArrayList<String> inputStreamtovaluelist(InputStream in, String MonthsName) throws IOException {
StringBuffer out = new StringBuffer();
String s1 = "";
byte[] b = new byte[4096];
ArrayList<String> Values = new ArrayList<String>();
Values.clear();
for (int n; (n = in.read(b)) != -1;) {
s1 = new String(b, 0, n);
out.append(s1);
}
System.out.println(out);
补充:移动开发 , Android ,