WSDL接口调用
import java.io.IOException; import java.util.Iterator; import java.util.Map; import org.ksoap2.SoapEnvelope; import org.ksoap2.serialization.MarshalBase64; import org.ksoap2.serialization.SoapObject; import org.ksoap2.serialization.SoapSerializationEnvelope; import org.ksoap2.transport.HttpTransportSE; import org.xmlpull.v1.XmlPullParserException; public class WebServiceAction { public static SoapObject getSoapObject(String Wsdl, String methodName, Map<String, Object> inputmap, String NAMESPACE) { SoapObject reqSoapObject = new SoapObject(NAMESPACE, methodName); //遍历input Iterator iter = inputmap.entrySet().iterator(); while (iter.hasNext()) { Map.Entry entry = (Map.Entry) iter.next(); Object key = entry.getKey(); Object val = entry.getValue(); reqSoapObject.addProperty((String) key, val); } SoapSerializationEnvelope envelope = new SoapSerializationEnvelope( SoapEnvelope.VER11); envelope.bodyOut = reqSoapObject; envelope.dotNet = false; envelope.encodingStyle="UTF-8"; (new MarshalBase64()).register(envelope); HttpTransportSE transport = new HttpTransportSE(Wsdl, 20000); transport.debug = true; try { transport.call(NAMESPACE + methodName, envelope); if (envelope.getResponse() != null) { SoapObject get_soap = (SoapObject) envelope.getResponse(); return get_soap; } } catch (IOException e) { e.printStackTrace(); } catch (XmlPullParserException e) { e.printStackTrace(); } return null; } public static Object getSoapObjectResponse(String Wsdl, String methodName, Map<String, Object> inputmap, String NAMESPACE) { SoapObject reqSoapObject = new SoapObject(NAMESPACE, methodName); // 遍历inputmap Iterator iter = inputmap.entrySet().iterator(); while (iter.hasNext()) { Map.Entry entry = (Map.Entry) iter.next(); Object key = entry.getKey(); Object val = entry.getValue(); reqSoapObject.addProperty((String) key, val); } SoapSerializationEnvelope envelope = new SoapSerializationEnvelope( SoapEnvelope.VER11); envelope.bodyOut = reqSoapObject; envelope.dotNet = false; (new MarshalBase64()).register(envelope); HttpTransportSE transport = new HttpTransportSE(Wsdl, 30000); transport.debug = true; try { transport.call(NAMESPACE + methodName, envelope); if (envelope.getResponse() != null) { return envelope.getResponse(); } } catch (IOException e) { e.printStackTrace(); } catch (XmlPullParserException e) { e.printStackTrace(); } return null; } }
另一个方法是拼接soap数据