近日在项目中写了个OPhone 调用Webservice的工具类,主要是使用了KSoap2来实现的,代码如下:
近日在项目中写了个OPhone 调用Webservice的工具类,主要是使用了KSoap2来实现的,代码如下:
1
package
com.ictehi.ophone.util;
2
3 import java.io.IOException;
4 import java.util.Iterator;
5 import java.util.Map;
6
7 import org.ksoap2.SoapEnvelope;
8 import org.ksoap2.SoapFault;
9 import org.ksoap2.serialization.SoapObject;
10 import org.ksoap2.serialization.SoapSerializationEnvelope;
11 import org.ksoap2.transport.AndroidHttpTransport;
12 import org.ksoap2.transport.HttpTransportSE;
13 import org.xmlpull.v1.XmlPullParserException;
14
15 import android.util.Log;
16
17 public class WSUtils {
18
19 public WSUtils() {
20 }
21
22 /** *//**
23 * @author Eric.Lu
24 * @param endPoint
25 * @param methodName
26 * @param params
27 * @param wsdl
28 * @return SoapObject
29 */
30 public static SoapObject callWS(String nameSpace, String methodName,
31 Map params, String wsdl) {
32
33 final String SOAP_ACTION=nameSpace+methodName;
34 SoapObject request = new SoapObject(nameSpace, methodName);
35 SoapObject soapResult=null;
36
37 if(params !=null && !params.isEmpty()){
38 for(Iterator it=params.entrySet().iterator();it.hasNext();){//遍历MAP
39 Map.Entry<String, Object> e=(Map.Entry<String, Object>) it.next();
40 request.addProperty(e.getKey().toString(),e.getValue());
41 }
42 }
43
44 /** *//**
45 * 设置Soap版本
46 * 类型:VER1.0,VER1.1,VER1.2
47 */
48 // SoapSerializationEnvelope envelope=new SoapSerializationEnvelope(SoapEnvelope.VER10);//SOAP 1.0
49 SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);// SOAP 1.1
50 // SoapSerializationEnvelope envelope=new SoapSerializationEnvelope(SoapEnvelope.VER12);//SOAP 1.2
51
52 envelope.dotNet=true;//是否是dotNet WebService
53 envelope.bodyOut = request;
54
55// HttpTransportSE ht = new HttpTransportSE(wsdl);
56 AndroidHttpTransport androidHT=new AndroidHttpTransport(wsdl);
57 try {
58// ht.call(SOAP_ACTION, envelope);
59 androidHT.call(SOAP_ACTION,envelope);
60 } catch (IOException e) {
61 Log.e("IOException:", e.getMessage());
62 } catch (XmlPullParserException e1) {
63 Log.e("XmlPullParserException", e1.getMessage());
64 }
65 try {
66 soapResult=(SoapObject)envelope.getResponse();
67 } catch (SoapFault e) {
68 Log.e("SoapFault",e.getMessage());
69 }
70 return soapResult;
71 }
72}
73
74
75 本文来自CSDN博客,转载请标明出处:http: // blog.csdn.net/kylixlu/archive/2010/03/12/5372846.aspx
调用这个工具类的方法如下:
2
3 import java.io.IOException;
4 import java.util.Iterator;
5 import java.util.Map;
6
7 import org.ksoap2.SoapEnvelope;
8 import org.ksoap2.SoapFault;
9 import org.ksoap2.serialization.SoapObject;
10 import org.ksoap2.serialization.SoapSerializationEnvelope;
11 import org.ksoap2.transport.AndroidHttpTransport;
12 import org.ksoap2.transport.HttpTransportSE;
13 import org.xmlpull.v1.XmlPullParserException;
14
15 import android.util.Log;
16
17 public class WSUtils {
18
19 public WSUtils() {
20 }
21
22 /** *//**
23 * @author Eric.Lu
24 * @param endPoint
25 * @param methodName
26 * @param params
27 * @param wsdl
28 * @return SoapObject
29 */
30 public static SoapObject callWS(String nameSpace, String methodName,
31 Map params, String wsdl) {
32
33 final String SOAP_ACTION=nameSpace+methodName;
34 SoapObject request = new SoapObject(nameSpace, methodName);
35 SoapObject soapResult=null;
36
37 if(params !=null && !params.isEmpty()){
38 for(Iterator it=params.entrySet().iterator();it.hasNext();){//遍历MAP
39 Map.Entry<String, Object> e=(Map.Entry<String, Object>) it.next();
40 request.addProperty(e.getKey().toString(),e.getValue());
41 }
42 }
43
44 /** *//**
45 * 设置Soap版本
46 * 类型:VER1.0,VER1.1,VER1.2
47 */
48 // SoapSerializationEnvelope envelope=new SoapSerializationEnvelope(SoapEnvelope.VER10);//SOAP 1.0
49 SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);// SOAP 1.1
50 // SoapSerializationEnvelope envelope=new SoapSerializationEnvelope(SoapEnvelope.VER12);//SOAP 1.2
51
52 envelope.dotNet=true;//是否是dotNet WebService
53 envelope.bodyOut = request;
54
55// HttpTransportSE ht = new HttpTransportSE(wsdl);
56 AndroidHttpTransport androidHT=new AndroidHttpTransport(wsdl);
57 try {
58// ht.call(SOAP_ACTION, envelope);
59 androidHT.call(SOAP_ACTION,envelope);
60 } catch (IOException e) {
61 Log.e("IOException:", e.getMessage());
62 } catch (XmlPullParserException e1) {
63 Log.e("XmlPullParserException", e1.getMessage());
64 }
65 try {
66 soapResult=(SoapObject)envelope.getResponse();
67 } catch (SoapFault e) {
68 Log.e("SoapFault",e.getMessage());
69 }
70 return soapResult;
71 }
72}
73
74
75 本文来自CSDN博客,转载请标明出处:http: // blog.csdn.net/kylixlu/archive/2010/03/12/5372846.aspx
1
HashMap
<
String, Object
>
params
=
new
HashMap
<
String, Object
>
();
2 params.put( " theCityName " , peoples[position]);
3 String wsdl = " webservice的wsdl地址 " ;
4 String nameSpace = " wsdl中的namespace " ;
5 String methodName = " 方法名 " ; //
6 // SoapObject result = WSUtils.callWS(nameSpace, methodName,
7 // wsdl,peoples[position]);
8 SoapObject result = WSUtils.callWS(nameSpace, methodName, params, wsdl);
9
10
11 本文来自CSDN博客,转载请标明出处:http: // blog.csdn.net/kylixlu/archive/2010/03/12/5372846.aspx
2 params.put( " theCityName " , peoples[position]);
3 String wsdl = " webservice的wsdl地址 " ;
4 String nameSpace = " wsdl中的namespace " ;
5 String methodName = " 方法名 " ; //
6 // SoapObject result = WSUtils.callWS(nameSpace, methodName,
7 // wsdl,peoples[position]);
8 SoapObject result = WSUtils.callWS(nameSpace, methodName, params, wsdl);
9
10
11 本文来自CSDN博客,转载请标明出处:http: // blog.csdn.net/kylixlu/archive/2010/03/12/5372846.aspx
MSN: