Android(安卓)中使用SOAP协议
16lz
2021-01-24
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());// HeadObject[] 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>");// BodyObject[] 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>");// Sendout.write(new String(soapXmlOut.toString().getBytes("UTF-8")));// Flush and closeout.flush();out.close();// Get responseBufferedReader 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(安卓)Studio通过插件自动生成Pracelable的模板代码
- Android(安卓)NestedScrolling嵌套滚动的示例代码
- android widget 开发实例 : 桌面便签程序的实现具体解释和源代码
- ANDROID 完美退出 APP 方法
- Android(安卓)Toolbar详解-实现Material Design 风格的导航栏
- 2011-2-14 | Android(安卓)Handler
- Kotlin&Anko, 扔掉XML开发Android应用
- ANE 在 Android(安卓)上的应用
- 深入浅出RxJava——在Android中使用响应式编程