Android 中使用SOAP协议

    SOAP协议:简单对象访问协议(Simple Object Access Protocol)是一种轻量的、简单的、基于 XML 的协议,它被设计成在 WEB 上交换结构化的和固化的信息。

    在Android上使用SOAP协议比较常用的方式是第三方的库,比如:kSOAP 2:http://ksoap2.sourceforge.net。

    下面给出一个使用OutputStreamWriter实现的SOAP协议,比较简单,直接上主要代码:

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.URL;
import java.net.URLConnection;
import java.util.HashMap;

public class SopaObject {

	private String nameSapce = null;

	private String method = null;

	private StringBuffer soapXmlOut = null;

	private StringBuilder soapXmlIn = null;

	private HashMap<String, String> headMap = null;

	private HashMap<String, String> bodyMap = null;

	private String responseCode = null;

	private String responseMessage = null;

	public SopaObject(String nameSapce, String method) {

		this.nameSapce = nameSapce;

		this.method = method;

		headMap = new HashMap<String, String>();

		bodyMap = new HashMap<String, String>();

		soapXmlOut = new StringBuffer();
		soapXmlOut.append("<?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/\">");

	}

	public void setSoapHead(HashMap<String, String> headMap) {

		this.headMap = headMap;

	}

	public void setSoapBody(HashMap<String, String> bodyMap) {

		this.bodyMap = bodyMap;

	}

	public void request(String urlPath) {

		try {

			URL url = new URL(urlPath);
			URLConnection connection = url.openConnection();
			connection.setDoOutput(true);
			connection.setRequestProperty("Pragma:", "no-cache");
			connection.setRequestProperty("Cache-Control", "no-cache");
			connection.setRequestProperty("Content-Type", "text/xml");

			OutputStreamWriter out = new OutputStreamWriter(connection.getOutputStream());

			// Head
			Object[] headKeys = headMap.keySet().toArray();
			soapXmlOut.append("<soap:Header><Credentials xmlns=\"" + nameSapce + "\">");
			for (Object key : headKeys) {
				soapXmlOut.append("<" + key + ">" + headMap.get(key) + "</" + key + ">");
			}
			soapXmlOut.append("</Credentials></soap:Header>");

			// Body
			Object[] bodyKeys = bodyMap.keySet().toArray();
			soapXmlOut.append("<soap:Body><" + method + " xmlns=\"" + nameSapce + "\">");
			for (Object key : bodyKeys) {
				soapXmlOut.append("<" + key + ">" + bodyMap.get(key) + "</" + key + ">");
			}
			soapXmlOut.append("</" + method + "></soap:Body>");
			soapXmlOut.append("</soap:Envelope>");

			// Send
			out.write(new String(soapXmlOut.toString().getBytes("UTF-8")));

			// Flush and close
			out.flush();
			out.close();

			// Get response
			BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
			soapXmlIn = new StringBuilder();
			String line = null;
			for (line = bufferedReader.readLine(); line != null; line = bufferedReader.readLine()) {
				soapXmlIn.append(line);
			}
			
			System.out.println("wangleyiang:" + soapXmlIn.toString());
			
		} catch (IOException e) {
			e.printStackTrace();
		}

	}

}

     测试一下,使用一个基于WebService的气象服务:http://www.webservicex.net/globalweather.asmx,测试代码如下:

        SopaObject object = new SopaObject("http://www.webservicex.net", "GetCitiesByCountry");
        
        HashMap<String, String> map = new HashMap<String, String>();
    	map.put("CountryName", "China");
        object.setSoapBody(map);
        
        object.request("http://www.webservicex.net/globalweather.asmx");

     SoapObject的方法比较少,意思比较明确,就不做说明了!:)

 

 

你可能感兴趣的:(android,kSOAP 2,简单对象访问协议,SOAP协议)