基于Android的WebService开发例子

来源:互联网 发布:mac下载iphone应用 编辑:程序博客网 时间:2024/06/05 15:59

WebService提供服务网站:http://webservice.webxml.com.cn/WebServices/MobileCodeWS.asmx


开发时需要结合网站提供的Soap的请求示例,例如请求示例:

POST /WebServices/WeatherWS.asmx HTTP/1.1Host: webservice.webxml.com.cnContent-Type: text/xml; charset=utf-8Content-Length: lengthSOAPAction: "http://WebXml.com.cn/getRegionProvince"<?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>    <getRegionProvince xmlns="http://WebXml.com.cn/" />  </soap:Body></soap:Envelope>

Activity如下:

package com.zeph.android.webservice.example;import java.io.IOException;import java.util.HashMap;import java.util.Map;import org.ksoap2.SoapEnvelope;import org.ksoap2.SoapFault;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;import android.app.Activity;import android.os.Bundle;public class WebServiceExampleActivity extends Activity {@Overridepublic void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.main);getCityCodeMap();}/** * 获得中国省份、直辖市、地区和与之对应的ID * @return */public Map<String, String> getCityCodeMap() {// 提供web service服务的asmx文件的urlString url = "http://webservice.webxml.com.cn/WebServices/WeatherWS.asmx";// soap对象的命名空间String namespace = "http://WebXml.com.cn/";// soap对象的名字String name = "getRegionProvince";// soap1.1版本中该soap要求的ActionString soapAction = "http://WebXml.com.cn/getRegionProvince";// soap对象,参数为soap的名字和soap的命名空间SoapObject soap = new SoapObject(namespace, name);// Base64是网络上最常见的用于传输8Bit字节代码的编码方式之一// Base64编码可用于在HTTP环境下传递较长的标识信息。MarshalBase64 base64 = new MarshalBase64();// Envelope对象,设置soap的版本是soap1.1版本SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);//设置envelope中body的值envelope.bodyOut = soap;// 给envelope注册 Base64base64.register(envelope);//创建基于JavaSE的Http传输层对象HttpTransportSE ht = new HttpTransportSE(url);try {//调用web service服务ht.call(soapAction, envelope);} catch (IOException e) {e.printStackTrace();} catch (XmlPullParserException e) {e.printStackTrace();}try {if (!envelope.getResponse().equals(null)) {return cityCodeParser(envelope.getResponse().toString());}} catch (SoapFault e) {e.printStackTrace();}return null;}/** * 获得中国省份、直辖市、地区和与之对应的ID解析器 * @param data * @return */public Map<String, String> cityCodeParser(String data) {String extractData = data.substring(data.indexOf("=") + 1, data.length() - 1).replace("string=", "").trim();String[] citiesNameValue = extractData.split(";");Map<String, String> map = new HashMap<String, String>();for (int i = 0; i < citiesNameValue.length; i++) {String[] cityNameValue = citiesNameValue[i].trim().split(",");map.put(cityNameValue[0], cityNameValue[1]);}return map;}}