1、SOAP消息
简单对象访问协议(Simple Object Access Protocol,SOAP)是一种标准化的通信规范,主要用于Web服务(Web Service)。
SOAP使用Internet应用层协议作为其传输协议。SMTP及HTTP协议都可以用来传输SOAP消息,SOAP亦可以通过HTTPS传输。一条SOAP消息就是一个普通的XML文档,包含下列元素:
(1)必须的Envelope元素,可把此XML文档标识为一条SOAP消息。
(2)可选的Header元素,包含头部信息。
(3)必须的Body元素,包含所有的调用和响应消息。
(4)可选的Fault元素,提供有关在处理此消息时发生错误的信息。
SOAP消息的重要语法规则如下:
(1)SOAP消息必须使用XML来编码。
(2)SOAP消息必须使用SOAP Envelope命名空间。
(3)SOAP消息必须使用SOAP Encoding命名空间。
(4)SOAP消息不能包含DTD引用。
(5)SOAP消息不能包含XML处理指令。
2、调用WebService
SOAP调用WebService的具体步骤如下:
步骤一:添加ksoap2包。可以从网址http://code.google.com/p/ksoap2-android/下载,然后将下载的ksoap2-android-assembly-2.4-jar-with-dependencies.jar包复制到Eclipse工程的lib目录中,当然也可以放在其他的目录里。在Eclipse中引用这个jar包。
步骤二:指定WebService的命名空间和调用的方法名,如:
SoapObject request = new SoapObject(http://service, “getName”);
SoapObject类的第一个参数表示WebService的命名空间,可以从WSDL文档中找到WebService的命名空间;第二个参数表示要调用的WebService方法名。
步骤三:设置调用方法的参数值,如果没有参数,可以省略。设置方法的参数值的代码如下:
request.addProperty(“param1”,”value”);
request.addProperty(“param2”,”value”);
要注意的是,addProperty方法的第一个参数虽然表示调用方法的参数名,但该参数值并不一定与服务端的WebService类中的方法参数名一致,只要设置参数的顺序一致即可。
步骤四:生成调用WebService方法的SOAP请求信息。该信息由SoapSerializationEnvelope对象描述,代码如下:
SoapSerializationEnvelope envelope=new SoapSerializationEnvelope(SoapEnvelope.VER11);
envelope.bodyOut=request;
envelope.dotNet=true;
创建SoapSerializationEnvelope对象时需要通过SoapSerializationEnvelope类的构造方法设置SOAP协议的版本号。该版本号需要根据服务端WebService的版本号设置。在创建SoapSerializationEnvelope对象后,不要忘了设置SoapSerializationEnvelope类的bodyOut属性,该属性的值就是在步骤2创建的SoapObject对象。
步骤五:创建HttpTransportSE对象。通过HttpTransportSE类的构造方法可以指定WebService的WSDL文档的URL。
HttpTransportSE ht=new HttpTransportSE (“http://……asmx?wsdl”);
步骤六:使用call方法调用WebService方法,代码如下:
ht.call(null,envelope);
call方法的第一个参数一般为null,第2个参数就是在步骤四创建的SoapSerializationEnvelope对象。
步骤七:使用getResponse方法获得WebService方法的返回结果,代码如下:
SoapObject soapObject=(SoapObject)envelope.getResponse();
步骤八:解析返回的内容。
3、代码示例
public class WebServiceProvider {
static final String NAMESPACE = "http://WebXml.com.cn/";
static String endPoint;
static String methodName;
static String soapAction;
public static String getPhoneSource(String phoneNum) {
methodName = "getMobileCodeInfo";
endPoint = "http://webservice.webxml.com.cn/WebServices/MobileCodeWS.asmx";
soapAction = NAMESPACE + methodName;
SoapObject rpc = new SoapObject(NAMESPACE, methodName);
// 设置需调用WebService接口需要传入的两个参数mobileCode、userId,不可以随便写,必须和提供的参数名相同
rpc.addProperty("mobileCode", phoneNum);
rpc.addProperty("userID", "");
// 生成调用WebService方法的SOAP请求信息,并指定SOAP的版本
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
SoapEnvelope.VER11);
envelope.bodyOut = rpc;
// 设置是否调用的是dotNet开发的WebService
envelope.dotNet = true;
// 等价于envelope.bodyOut = rpc;
HttpTransportSE transport = new HttpTransportSE(endPoint);
// 调用WebService
try {
transport.call(soapAction, envelope);
} catch (Exception e) {
e.printStackTrace();
}
// 获取返回的数据
SoapObject object = (SoapObject) envelope.bodyIn;
// 获取返回的结果
String result = object.getProperty("getMobileCodeInfoResult")
.toString();
return result;
}
public static String[] getProvinceList() {
methodName = "getRegionProvince";
endPoint = "http://webservice.webxml.com.cn/WebServices/WeatherWS.asmx";
soapAction = NAMESPACE + methodName;
String[] provinceStrings;
SoapObject rpc = new SoapObject(NAMESPACE, methodName);
// 生成调用WebService方法的SOAP请求信息,并指定SOAP的版本
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
SoapEnvelope.VER11);
envelope.bodyOut = rpc;
// 设置是否调用的是dotNet开发的WebService
envelope.dotNet = true;
HttpTransportSE transport = new HttpTransportSE(endPoint);
// 调用WebService
try {
transport.call(soapAction, envelope);
} catch (Exception e) {
e.printStackTrace();
}
// 获取返回的数据
SoapObject object = (SoapObject) envelope.bodyIn;
SoapObject provinceSoapObject = (SoapObject) object
.getProperty("getRegionProvinceResult");
provinceStrings = new String[provinceSoapObject.getPropertyCount()];
for (int i = 0; i < provinceSoapObject.getPropertyCount(); i++) {
String provinceString = provinceSoapObject.getProperty(i)
.toString().split(",")[0];
provinceStrings[i] = provinceString;
}
return provinceStrings;
}
public static String[] getCityList(String provinceString) {
methodName = "getSupportCityString ";
endPoint = "http://webservice.webxml.com.cn/WebServices/WeatherWS.asmx";
soapAction = NAMESPACE + methodName;
String[] cityStrings;
SoapObject rpc = new SoapObject(NAMESPACE, methodName);
// 设置需调用WebService接口需要传入的两个参数mobileCode、userId,不可以随便写,必须和提供的参数名相同
rpc.addProperty("theRegionCode", provinceString);
rpc.addProperty("userID", "你的ID");
// 生成调用WebService方法的SOAP请求信息,并指定SOAP的版本
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
SoapEnvelope.VER11);
envelope.bodyOut = rpc;
// 设置是否调用的是dotNet开发的WebService
envelope.dotNet = true;
HttpTransportSE transport = new HttpTransportSE(endPoint);
// 调用WebService
try {
transport.call(soapAction, envelope);
} catch (Exception e) {
e.printStackTrace();
}
// 获取返回的数据
SoapObject object = (SoapObject) envelope.bodyIn;
SoapObject citySoapObject = (SoapObject) object
.getProperty("getSupportCityStringResult");
cityStrings = new String[citySoapObject.getPropertyCount()];
for (int i = 0; i < citySoapObject.getPropertyCount(); i++) {
String cityString = citySoapObject.getProperty(i).toString()
.split(",")[0];
cityStrings[i] = cityString;
}
return cityStrings;
}
public static HashMap<String, String> getWeather(String cityString) {
methodName = "getWeather";
endPoint = "http://webservice.webxml.com.cn/WebServices/WeatherWS.asmx";
soapAction = NAMESPACE + methodName;
HashMap<String, String> weatherInfos;
SoapObject rpc = new SoapObject(NAMESPACE, methodName);
// 设置需调用WebService接口需要传入的两个参数mobileCode、userId,不可以随便写,必须和提供的参数名相同
rpc.addProperty("theCityCode", cityString);
rpc.addProperty("theUserID", "你的ID");
// 生成调用WebService方法的SOAP请求信息,并指定SOAP的版本
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
SoapEnvelope.VER11);
envelope.bodyOut = rpc;
// 设置是否调用的是dotNet开发的WebService
envelope.dotNet = true;
HttpTransportSE transport = new HttpTransportSE(endPoint);
// 调用WebService
try {
transport.call(soapAction, envelope);
} catch (Exception e) {
e.printStackTrace();
}
// 获取返回的数据
SoapObject object = (SoapObject) envelope.bodyIn;
SoapObject weatherSoapObject = (SoapObject) object
.getProperty("getWeatherResult");
String todayDate = weatherSoapObject.getPropertyAsString(3);// 日期
String todayTemp = "气温:"+weatherSoapObject.getPropertyAsString(4).split(";")[0].split(":")[2];// 温度
String todayWind = weatherSoapObject.getPropertyAsString(4).split(";")[1];// 风力
String todayHumidity=weatherSoapObject.getPropertyAsString(4).split(";")[2];// 湿度
String todayImage = weatherSoapObject.getPropertyAsString(10);//图片
String tomorrowDate=weatherSoapObject.getPropertyAsString(12).split(" ")[0];// 明日时间
String tomorrowWeather=weatherSoapObject.getPropertyAsString(12).split(" ")[1];// 明日天气
String tomorrowImage = weatherSoapObject.getPropertyAsString(15);//明日图片
String tomorrowTemp = weatherSoapObject.getPropertyAsString(13);// 明日温度
String tomorrow2Date=weatherSoapObject.getPropertyAsString(17).split(" ")[0];// 后天时间
String tomorrow2Weather=weatherSoapObject.getPropertyAsString(17).split(" ")[1];// 明日天气
String tomorrow2Image = weatherSoapObject.getPropertyAsString(20);//后天图片
String tomorrow2Temp = weatherSoapObject.getPropertyAsString(18);// 后天温度
String tomorrow3Date=weatherSoapObject.getPropertyAsString(22).split(" ")[0];// 大后天时间
String tomorrow3Weather=weatherSoapObject.getPropertyAsString(22).split(" ")[1];// 明日天气
String tomorrow3Image = weatherSoapObject.getPropertyAsString(25);//大后天图片
String tomorrow3Temp = weatherSoapObject.getPropertyAsString(23);// 大后天温度
weatherInfos = new HashMap<String, String>();
weatherInfos.put("todayDate", todayDate);
weatherInfos.put("todayTemp", todayTemp);
weatherInfos.put("todayWind", todayWind);
weatherInfos.put("todayHumidity", todayHumidity);
weatherInfos.put("todayImage", todayImage);
weatherInfos.put("tomorrowDate", tomorrowDate);
weatherInfos.put("tomorrowWeather", tomorrowWeather);
weatherInfos.put("tomorrowImage", tomorrowImage);
weatherInfos.put("tomorrowTemp", tomorrowTemp);
weatherInfos.put("tomorrow2Date", tomorrow2Date);
weatherInfos.put("tomorrow2Weather", tomorrow2Weather);
weatherInfos.put("tomorrow2Image", tomorrow2Image);
weatherInfos.put("tomorrow2Temp", tomorrow2Temp);
weatherInfos.put("tomorrow3Date", tomorrow3Date);
weatherInfos.put("tomorrow3Weather", tomorrow3Weather);
weatherInfos.put("tomorrow3Image", tomorrow3Image);
weatherInfos.put("tomorrow3Temp", tomorrow3Temp);
return weatherInfos;
}
public static ArrayList<HashMap<String, String>> getFlightTimeList(String startCity,String lastCity,String flightDate)
{
methodName = "getDomesticAirlinesTime";
endPoint = "http://webservice.webxml.com.cn/webservices/DomesticAirline.asmx";
soapAction = NAMESPACE + methodName;
ArrayList<HashMap<String, String>> flights=new ArrayList<HashMap<String, String>>();
SoapObject rpc = new SoapObject(NAMESPACE, methodName);
// 设置需调用WebService接口需要传入的两个参数mobileCode、userId,不可以随便写,必须和提供的参数名相同
rpc.addProperty("startCity", startCity);
rpc.addProperty("lastCity", lastCity);
rpc.addProperty("theDate", flightDate);
rpc.addProperty("userID", "你的ID");
// 生成调用WebService方法的SOAP请求信息,并指定SOAP的版本
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
SoapEnvelope.VER11);
envelope.bodyOut = rpc;
// 设置是否调用的是dotNet开发的WebService
envelope.dotNet = true;
HttpTransportSE transport = new HttpTransportSE(endPoint);
// 调用WebService
try {
transport.call(soapAction, envelope);
} catch (Exception e) {
e.printStackTrace();
}
// 获取返回的数据
SoapObject object = (SoapObject) envelope.bodyIn;
SoapObject airLinesTimeSoapObject = (SoapObject) object
.getProperty("getDomesticAirlinesTimeResult");
SoapObject airLinesSoapObject=(SoapObject)((SoapObject)airLinesTimeSoapObject.getProperty(1))
.getProperty(0);
HashMap<String, String> mMap=new HashMap<String, String>();
mMap.put("航空公司", "航空公司");
mMap.put("出发时间", "出发时间");
mMap.put("到达时间", "到达时间");
mMap.put("出发机场", "出发机场");
mMap.put("到达机场", "到达机场");
flights.add(mMap);
for(int i=0;i<airLinesSoapObject.getPropertyCount();i++)
{
SoapObject airLineSoapObject=(SoapObject) airLinesSoapObject.getProperty(i);
HashMap<String, String> flightHashMap=new HashMap<String, String>();
flightHashMap.put("航空公司",airLineSoapObject.getProperty("Company") .toString());
flightHashMap.put("出发机场",airLineSoapObject.getProperty("StartDrome") .toString());
flightHashMap.put("到达机场",airLineSoapObject.getProperty("ArriveDrome") .toString());
flightHashMap.put("出发时间",airLineSoapObject.getProperty("StartTime") .toString());
flightHashMap.put("到达时间",airLineSoapObject.getProperty("ArriveTime") .toString());
flights.add(flightHashMap);
}
return flights;
}
public static ArrayList<HashMap<String, String>> getTrainTimeList(String startStation,String arriveStation)
{
methodName = "getStationAndTimeByStationName";
endPoint = "http://webservice.webxml.com.cn/WebServices/TrainTimeWebService.asmx";
soapAction = NAMESPACE + methodName;
ArrayList<HashMap<String, String>> trains=new ArrayList<HashMap<String, String>>();
SoapObject rpc = new SoapObject(NAMESPACE, methodName);
// 设置需调用WebService接口需要传入的两个参数mobileCode、userId,不可以随便写,必须和提供的参数名相同
rpc.addProperty("StartStation", startStation);
rpc.addProperty("ArriveStation", arriveStation);
rpc.addProperty("UserID", "你的ID");
// 生成调用WebService方法的SOAP请求信息,并指定SOAP的版本
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
SoapEnvelope.VER11);
envelope.bodyOut = rpc;
// 设置是否调用的是dotNet开发的WebService
envelope.dotNet = true;
HttpTransportSE transport = new HttpTransportSE(endPoint);
// 调用WebService
try {
transport.call(soapAction, envelope);
} catch (Exception e) {
e.printStackTrace();
}
// 获取返回的数据
SoapObject object = (SoapObject) envelope.bodyIn;
SoapObject trainsSoapObject = (SoapObject) object
.getProperty("getStationAndTimeByStationNameResult");
SoapObject trainsTimeSoapObject=(SoapObject)((SoapObject)trainsSoapObject.getProperty(1))
.getProperty(0);
HashMap<String, String> mMap=new HashMap<String, String>();
mMap.put("车次", "车次");
mMap.put("出发时间", "出发时间");
mMap.put("到达时间", "到达时间");
mMap.put("时长", "时长");
trains.add(mMap);
for(int i=0;i<trainsTimeSoapObject.getPropertyCount();i++)
{
SoapObject trainTimeSoapObject=(SoapObject) trainsTimeSoapObject.getProperty(i);
HashMap<String, String> trainHashMap=new HashMap<String, String>();
trainHashMap.put("车次",trainTimeSoapObject.getProperty("TrainCode") .toString());
trainHashMap.put("出发时间",trainTimeSoapObject.getProperty("StartTime") .toString());
trainHashMap.put("到达时间",trainTimeSoapObject.getProperty("ArriveTime") .toString());
trainHashMap.put("时长",trainTimeSoapObject.getProperty("UseDate") .toString());
trains.add(trainHashMap);
}
return trains;
}
}