---恢复内容开始---
继总结过web service后,再总结一下android中使用web service。
在android中使用web service需要使用到一个第三方开源的类库——ksoap2。他是一个SOAP Web Service客户端开发包。是android平台上高效、轻量级的SOAP开发包。
使用SOAP需要引入第三方的jar包:ksoap2-android-assembly-2.5.4-jar-with-dependencies.jar。如何加入jar包就不在赘述。下面讲一下soap使用web service的步骤。
调用web service的步骤:
1、实例化SOAP对象,指定soap的命名空间。
//目标域名空间(即提供Web Service服务的域名空间) private static final String TARGETNAMESPACE = "http://WebXml.com.cn/"; //需要调用的方法名(获得本天气预报Web Services支持的洲、国内外省份和城市信息) private static final String GETSUPPORTPROVINCE = "getSupportProvince"; //实例化SOAP对象,指定命名空间和调用的方法 SoapObject soapObject = new SoapObject(TARGETNAMESPACE,GETSUPPORTPROVINCE);
2、如果方法有参数,添加参数及参数值。
//设置方法需要使用的参数,以及参数值 soapObject.addProperty("theRegionCode",province);
3、设置SOAP请求信息,参数为SOAP的版本(版本只能低于服务端指定的版本)。
//使用SOAP1.1协议创建Envelop对象 SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER10); //设置与.Dot提供的Web Service服务保持良好的兼容性 envelope.dotNet = true; //将初始化的soapObject放入(序列化)使用SOAP1.1协议创建Envelop对象中 envelope.setOutputSoapObject(soapObject);
4、构建传输对象,指定WSDL文档中给定的URL。
//WSDL文档中给定的URL private static final String WSDL = "http://www.webxml.com.cn/webservices/weatherwebservice.asmx"; //构建传输对象,指定URL HttpTransportSE httpTransportSE = new HttpTransportSE(WSDL); //AndroidHttpTransport transport=new AndroidHttpTransport(serviceURL); // transport.debug=true;
5、调用web service(参数1:命名空间+方法名;参数2:envelop对象)
httpTransportSE.call(TARGETNAMESPACE+GETSUPPORTPROVINCE,soapSerializationEnvelope);
6、处理返回结果
SoapObject result = (SoapObject) soapSerializationEnvelope.getResponse(); int count = result.getPropertyCount(); for (int i = 0;i < count;i++){ privinces.add(result.getProperty(i).toString()); System.out.println(result.getProperty(i).toString()); }
以下就是本人写的调用天气预报服务的代码:
import org.ksoap2.SoapEnvelope; import org.ksoap2.serialization.SoapObject; import org.ksoap2.serialization.SoapSerializationEnvelope; import org.ksoap2.transport.HttpTransportSE; import org.xmlpull.v1.XmlPullParserException; import java.io.IOException; import java.util.ArrayList; import java.util.List; /** * Created by wangyan on 2016/1/17. */ public class WebServiceHelper { //目标域名空间(即提供Web Service服务的域名空间) private static final String TARGETNAMESPACE = "http://WebXml.com.cn/"; //WSDL文档中给定的URL private static final String WSDL = "http://www.webxml.com.cn/webservices/weatherwebservice.asmx"; //需要调用的方法名(获得本天气预报Web Services支持的洲、国内外省份和城市信息) private static final String GETSUPPORTPROVINCE = "getSupportProvince"; //需要调用的方法名(获得本天气预报Web Services支持的城市信息,根据省份查询城市集合:带参数) private static final String GETSUPPORTCITY = "getSupportCity"; //根据城市或地区名称查询获得未来三天内天气情况、现在的天气实况、天气和生活指数 private static final String GETWEATHERBYCITYNAME = "getWeatherbyCityName"; /** * 获得洲、国内外省份和某些城市 * @return */ public List getProvince(){ List<String> privinces = new ArrayList<String>(); //实例化SOAP对象,指定命名空间和调用的方法 SoapObject soapObject = new SoapObject(TARGETNAMESPACE,GETSUPPORTPROVINCE); //使用SOAP1.1协议创建Envelop对象 SoapSerializationEnvelope soapSerializationEnvelope = new SoapSerializationEnvelope(SoapEnvelope.VER10); //设置与.Dot提供的Web Service服务保持良好的兼容性 soapSerializationEnvelope.dotNet = true; //将初始化的soapObject放入(序列化)使用SOAP1.1协议创建Envelop对象中 soapSerializationEnvelope.setOutputSoapObject(soapObject); //构建传输对象,指定URL HttpTransportSE httpTransportSE = new HttpTransportSE(WSDL); try { httpTransportSE.call(TARGETNAMESPACE+GETSUPPORTPROVINCE,soapSerializationEnvelope); SoapObject result = (SoapObject) soapSerializationEnvelope.getResponse(); int count = result.getPropertyCount(); for (int i = 0;i < count;i++){ privinces.add(result.getProperty(i).toString()); System.out.println(result.getProperty(i).toString()); } } catch (IOException e) { e.printStackTrace(); } catch (XmlPullParserException e) { e.printStackTrace(); } return privinces; } /** * 根据省得到城市 * @param province * @return */ public List<String> getCitysByProvince(String province){ List<String> citylist = new ArrayList<String>(); SoapObject soapObject = new SoapObject(TARGETNAMESPACE,GETSUPPORTCITY); //设置方法需要使用的参数,以及参数值 soapObject.addProperty("theRegionCode",province); SoapSerializationEnvelope soapSerializationEnvelope = new SoapSerializationEnvelope(SoapEnvelope.VER10); soapSerializationEnvelope.dotNet = true; soapSerializationEnvelope.setOutputSoapObject(soapObject); HttpTransportSE httpTransportSE = new HttpTransportSE(WSDL); try { httpTransportSE.call(TARGETNAMESPACE + GETSUPPORTCITY,soapSerializationEnvelope); SoapObject result = (SoapObject) soapSerializationEnvelope.getResponse(); int count = result.getPropertyCount(); if (result != null && count > 0){ for (int i = 0; i < count; i++){ citylist.add(result.getProperty(i).toString()); System.out.println(result.getProperty(i).toString()); } } } catch (IOException e) { e.printStackTrace(); } catch (XmlPullParserException e) { e.printStackTrace(); } return citylist; } /** * 根据城市获得天气信息 * @param city * @return */ public List<String> getWeatherByCity(String city){ List<String> weatherlist = new ArrayList<String>(); SoapObject soapObject = new SoapObject(TARGETNAMESPACE,GETWEATHERBYCITYNAME); soapObject.addProperty("theCityName", city); SoapSerializationEnvelope soapSerializationEnvelope = new SoapSerializationEnvelope(SoapEnvelope.VER10); soapSerializationEnvelope.dotNet = true; soapSerializationEnvelope.setOutputSoapObject(soapObject); HttpTransportSE httpTransportSE = new HttpTransportSE(WSDL); try { httpTransportSE.call(TARGETNAMESPACE + GETWEATHERBYCITYNAME, soapSerializationEnvelope); SoapObject result = (SoapObject) soapSerializationEnvelope.getResponse(); if (result != null){ int count = result.getPropertyCount(); for (int i = 0; i < count;i++){ //可以用天气类来更好的处理得到的天气信息 weatherlist.add(result.getProperty(i).toString()); } } } catch (IOException e) { e.printStackTrace(); } catch (XmlPullParserException e) { e.printStackTrace(); } return weatherlist; } }
---恢复内容结束---
继总结过web service后,再总结一下android中使用web service。
在android中使用web service需要使用到一个第三方开源的类库——ksoap2。他是一个SOAP Web Service客户端开发包。是android平台上高效、轻量级的SOAP开发包。
使用SOAP需要引入第三方的jar包:ksoap2-android-assembly-2.5.4-jar-with-dependencies.jar。如何加入jar包就不在赘述。下面讲一下soap使用web service的步骤。
调用web service的步骤:
1、实例化SOAP对象,指定soap的命名空间。
//目标域名空间(即提供Web Service服务的域名空间) private static final String TARGETNAMESPACE = "http://WebXml.com.cn/"; //需要调用的方法名(获得本天气预报Web Services支持的洲、国内外省份和城市信息) private static final String GETSUPPORTPROVINCE = "getSupportProvince"; //实例化SOAP对象,指定命名空间和调用的方法 SoapObject soapObject = new SoapObject(TARGETNAMESPACE,GETSUPPORTPROVINCE);
2、如果方法有参数,添加参数及参数值。
//设置方法需要使用的参数,以及参数值 soapObject.addProperty("theRegionCode",province);
3、设置SOAP请求信息,参数为SOAP的版本(版本只能低于服务端指定的版本)。
//使用SOAP1.1协议创建Envelop对象 SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER10); //设置与.Dot提供的Web Service服务保持良好的兼容性 envelope.dotNet = true; //将初始化的soapObject放入(序列化)使用SOAP1.1协议创建Envelop对象中 envelope.setOutputSoapObject(soapObject);
4、构建传输对象,指定WSDL文档中给定的URL。
//WSDL文档中给定的URL private static final String WSDL = "http://www.webxml.com.cn/webservices/weatherwebservice.asmx"; //构建传输对象,指定URL HttpTransportSE httpTransportSE = new HttpTransportSE(WSDL); //AndroidHttpTransport transport=new AndroidHttpTransport(serviceURL); // transport.debug=true;
5、调用web service(参数1:命名空间+方法名;参数2:envelop对象)
httpTransportSE.call(TARGETNAMESPACE+GETSUPPORTPROVINCE,soapSerializationEnvelope);
6、处理返回结果
SoapObject result = (SoapObject) soapSerializationEnvelope.getResponse(); int count = result.getPropertyCount(); for (int i = 0;i < count;i++){ privinces.add(result.getProperty(i).toString()); System.out.println(result.getProperty(i).toString()); }
以下就是本人写的调用天气预报服务的代码:
import org.ksoap2.SoapEnvelope; import org.ksoap2.serialization.SoapObject; import org.ksoap2.serialization.SoapSerializationEnvelope; import org.ksoap2.transport.HttpTransportSE; import org.xmlpull.v1.XmlPullParserException; import java.io.IOException; import java.util.ArrayList; import java.util.List; /** * Created by wangyan on 2016/1/17. */ public class WebServiceHelper { //目标域名空间(即提供Web Service服务的域名空间) private static final String TARGETNAMESPACE = "http://WebXml.com.cn/"; //WSDL文档中给定的URL private static final String WSDL = "http://www.webxml.com.cn/webservices/weatherwebservice.asmx"; //需要调用的方法名(获得本天气预报Web Services支持的洲、国内外省份和城市信息) private static final String GETSUPPORTPROVINCE = "getSupportProvince"; //需要调用的方法名(获得本天气预报Web Services支持的城市信息,根据省份查询城市集合:带参数) private static final String GETSUPPORTCITY = "getSupportCity"; //根据城市或地区名称查询获得未来三天内天气情况、现在的天气实况、天气和生活指数 private static final String GETWEATHERBYCITYNAME = "getWeatherbyCityName"; /** * 获得洲、国内外省份和某些城市 * @return */ public List getProvince(){ List<String> privinces = new ArrayList<String>(); //实例化SOAP对象,指定命名空间和调用的方法 SoapObject soapObject = new SoapObject(TARGETNAMESPACE,GETSUPPORTPROVINCE); //使用SOAP1.1协议创建Envelop对象 SoapSerializationEnvelope soapSerializationEnvelope = new SoapSerializationEnvelope(SoapEnvelope.VER10); //设置与.Dot提供的Web Service服务保持良好的兼容性 soapSerializationEnvelope.dotNet = true; //将初始化的soapObject放入(序列化)使用SOAP1.1协议创建Envelop对象中 soapSerializationEnvelope.setOutputSoapObject(soapObject); //构建传输对象,指定URL HttpTransportSE httpTransportSE = new HttpTransportSE(WSDL); try { httpTransportSE.call(TARGETNAMESPACE+GETSUPPORTPROVINCE,soapSerializationEnvelope); SoapObject result = (SoapObject) soapSerializationEnvelope.getResponse(); int count = result.getPropertyCount(); for (int i = 0;i < count;i++){ privinces.add(result.getProperty(i).toString()); System.out.println(result.getProperty(i).toString()); } } catch (IOException e) { e.printStackTrace(); } catch (XmlPullParserException e) { e.printStackTrace(); } return privinces; } /** * 根据省得到城市 * @param province * @return */ public List<String> getCitysByProvince(String province){ List<String> citylist = new ArrayList<String>(); SoapObject soapObject = new SoapObject(TARGETNAMESPACE,GETSUPPORTCITY); //设置方法需要使用的参数,以及参数值 soapObject.addProperty("theRegionCode",province); SoapSerializationEnvelope soapSerializationEnvelope = new SoapSerializationEnvelope(SoapEnvelope.VER10); soapSerializationEnvelope.dotNet = true; soapSerializationEnvelope.setOutputSoapObject(soapObject); HttpTransportSE httpTransportSE = new HttpTransportSE(WSDL); try { httpTransportSE.call(TARGETNAMESPACE + GETSUPPORTCITY,soapSerializationEnvelope); SoapObject result = (SoapObject) soapSerializationEnvelope.getResponse(); int count = result.getPropertyCount(); if (result != null && count > 0){ for (int i = 0; i < count; i++){ citylist.add(result.getProperty(i).toString()); System.out.println(result.getProperty(i).toString()); } } } catch (IOException e) { e.printStackTrace(); } catch (XmlPullParserException e) { e.printStackTrace(); } return citylist; } /** * 根据城市获得天气信息 * @param city * @return */ public List<String> getWeatherByCity(String city){ List<String> weatherlist = new ArrayList<String>(); SoapObject soapObject = new SoapObject(TARGETNAMESPACE,GETWEATHERBYCITYNAME); soapObject.addProperty("theCityName", city); SoapSerializationEnvelope soapSerializationEnvelope = new SoapSerializationEnvelope(SoapEnvelope.VER10); soapSerializationEnvelope.dotNet = true; soapSerializationEnvelope.setOutputSoapObject(soapObject); HttpTransportSE httpTransportSE = new HttpTransportSE(WSDL); try { httpTransportSE.call(TARGETNAMESPACE + GETWEATHERBYCITYNAME, soapSerializationEnvelope); SoapObject result = (SoapObject) soapSerializationEnvelope.getResponse(); if (result != null){ int count = result.getPropertyCount(); for (int i = 0; i < count;i++){ //可以用天气类来更好的处理得到的天气信息 weatherlist.add(result.getProperty(i).toString()); } } } catch (IOException e) { e.printStackTrace(); } catch (XmlPullParserException e) { e.printStackTrace(); } return weatherlist; } }
---恢复内容开始---
继总结过web service后,再总结一下android中使用web service。
在android中使用web service需要使用到一个第三方开源的类库——ksoap2。他是一个SOAP Web Service客户端开发包。是android平台上高效、轻量级的SOAP开发包。
使用SOAP需要引入第三方的jar包:ksoap2-android-assembly-2.5.4-jar-with-dependencies.jar。如何加入jar包就不在赘述。下面讲一下soap使用web service的步骤。
调用web service的步骤:
1、实例化SOAP对象,指定soap的命名空间。
//目标域名空间(即提供Web Service服务的域名空间) private static final String TARGETNAMESPACE = "http://WebXml.com.cn/"; //需要调用的方法名(获得本天气预报Web Services支持的洲、国内外省份和城市信息) private static final String GETSUPPORTPROVINCE = "getSupportProvince"; //实例化SOAP对象,指定命名空间和调用的方法 SoapObject soapObject = new SoapObject(TARGETNAMESPACE,GETSUPPORTPROVINCE);
2、如果方法有参数,添加参数及参数值。
//设置方法需要使用的参数,以及参数值 soapObject.addProperty("theRegionCode",province);
3、设置SOAP请求信息,参数为SOAP的版本(版本只能低于服务端指定的版本)。
//使用SOAP1.1协议创建Envelop对象 SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER10); //设置与.Dot提供的Web Service服务保持良好的兼容性 envelope.dotNet = true; //将初始化的soapObject放入(序列化)使用SOAP1.1协议创建Envelop对象中 envelope.setOutputSoapObject(soapObject);
4、构建传输对象,指定WSDL文档中给定的URL。
//WSDL文档中给定的URL private static final String WSDL = "http://www.webxml.com.cn/webservices/weatherwebservice.asmx"; //构建传输对象,指定URL HttpTransportSE httpTransportSE = new HttpTransportSE(WSDL); //AndroidHttpTransport transport=new AndroidHttpTransport(serviceURL); // transport.debug=true;
5、调用web service(参数1:命名空间+方法名;参数2:envelop对象)
httpTransportSE.call(TARGETNAMESPACE+GETSUPPORTPROVINCE,soapSerializationEnvelope);
6、处理返回结果
SoapObject result = (SoapObject) soapSerializationEnvelope.getResponse(); int count = result.getPropertyCount(); for (int i = 0;i < count;i++){ privinces.add(result.getProperty(i).toString()); System.out.println(result.getProperty(i).toString()); }
以下就是本人写的调用天气预报服务的代码:
import org.ksoap2.SoapEnvelope; import org.ksoap2.serialization.SoapObject; import org.ksoap2.serialization.SoapSerializationEnvelope; import org.ksoap2.transport.HttpTransportSE; import org.xmlpull.v1.XmlPullParserException; import java.io.IOException; import java.util.ArrayList; import java.util.List; /** * Created by wangyan on 2016/1/17. */ public class WebServiceHelper { //目标域名空间(即提供Web Service服务的域名空间) private static final String TARGETNAMESPACE = "http://WebXml.com.cn/"; //WSDL文档中给定的URL private static final String WSDL = "http://www.webxml.com.cn/webservices/weatherwebservice.asmx"; //需要调用的方法名(获得本天气预报Web Services支持的洲、国内外省份和城市信息) private static final String GETSUPPORTPROVINCE = "getSupportProvince"; //需要调用的方法名(获得本天气预报Web Services支持的城市信息,根据省份查询城市集合:带参数) private static final String GETSUPPORTCITY = "getSupportCity"; //根据城市或地区名称查询获得未来三天内天气情况、现在的天气实况、天气和生活指数 private static final String GETWEATHERBYCITYNAME = "getWeatherbyCityName"; /** * 获得洲、国内外省份和某些城市 * @return */ public List getProvince(){ List<String> privinces = new ArrayList<String>(); //实例化SOAP对象,指定命名空间和调用的方法 SoapObject soapObject = new SoapObject(TARGETNAMESPACE,GETSUPPORTPROVINCE); //使用SOAP1.1协议创建Envelop对象 SoapSerializationEnvelope soapSerializationEnvelope = new SoapSerializationEnvelope(SoapEnvelope.VER10); //设置与.Dot提供的Web Service服务保持良好的兼容性 soapSerializationEnvelope.dotNet = true; //将初始化的soapObject放入(序列化)使用SOAP1.1协议创建Envelop对象中 soapSerializationEnvelope.setOutputSoapObject(soapObject); //构建传输对象,指定URL HttpTransportSE httpTransportSE = new HttpTransportSE(WSDL); try { httpTransportSE.call(TARGETNAMESPACE+GETSUPPORTPROVINCE,soapSerializationEnvelope); SoapObject result = (SoapObject) soapSerializationEnvelope.getResponse(); int count = result.getPropertyCount(); for (int i = 0;i < count;i++){ privinces.add(result.getProperty(i).toString()); System.out.println(result.getProperty(i).toString()); } } catch (IOException e) { e.printStackTrace(); } catch (XmlPullParserException e) { e.printStackTrace(); } return privinces; } /** * 根据省得到城市 * @param province * @return */ public List<String> getCitysByProvince(String province){ List<String> citylist = new ArrayList<String>(); SoapObject soapObject = new SoapObject(TARGETNAMESPACE,GETSUPPORTCITY); //设置方法需要使用的参数,以及参数值 soapObject.addProperty("theRegionCode",province); SoapSerializationEnvelope soapSerializationEnvelope = new SoapSerializationEnvelope(SoapEnvelope.VER10); soapSerializationEnvelope.dotNet = true; soapSerializationEnvelope.setOutputSoapObject(soapObject); HttpTransportSE httpTransportSE = new HttpTransportSE(WSDL); try { httpTransportSE.call(TARGETNAMESPACE + GETSUPPORTCITY,soapSerializationEnvelope); SoapObject result = (SoapObject) soapSerializationEnvelope.getResponse(); int count = result.getPropertyCount(); if (result != null && count > 0){ for (int i = 0; i < count; i++){ citylist.add(result.getProperty(i).toString()); System.out.println(result.getProperty(i).toString()); } } } catch (IOException e) { e.printStackTrace(); } catch (XmlPullParserException e) { e.printStackTrace(); } return citylist; } /** * 根据城市获得天气信息 * @param city * @return */ public List<String> getWeatherByCity(String city){ List<String> weatherlist = new ArrayList<String>(); SoapObject soapObject = new SoapObject(TARGETNAMESPACE,GETWEATHERBYCITYNAME); soapObject.addProperty("theCityName", city); SoapSerializationEnvelope soapSerializationEnvelope = new SoapSerializationEnvelope(SoapEnvelope.VER10); soapSerializationEnvelope.dotNet = true; soapSerializationEnvelope.setOutputSoapObject(soapObject); HttpTransportSE httpTransportSE = new HttpTransportSE(WSDL); try { httpTransportSE.call(TARGETNAMESPACE + GETWEATHERBYCITYNAME, soapSerializationEnvelope); SoapObject result = (SoapObject) soapSerializationEnvelope.getResponse(); if (result != null){ int count = result.getPropertyCount(); for (int i = 0; i < count;i++){ //可以用天气类来更好的处理得到的天气信息 weatherlist.add(result.getProperty(i).toString()); } } } catch (IOException e) { e.printStackTrace(); } catch (XmlPullParserException e) { e.printStackTrace(); } return weatherlist; } }
---恢复内容结束---
继总结过web service后,再总结一下android中使用web service。
在android中使用web service需要使用到一个第三方开源的类库——ksoap2。他是一个SOAP Web Service客户端开发包。是android平台上高效、轻量级的SOAP开发包。
使用SOAP需要引入第三方的jar包:ksoap2-android-assembly-2.5.4-jar-with-dependencies.jar。如何加入jar包就不在赘述。下面讲一下soap使用web service的步骤。
调用web service的步骤:
1、实例化SOAP对象,指定soap的命名空间。
//目标域名空间(即提供Web Service服务的域名空间) private static final String TARGETNAMESPACE = "http://WebXml.com.cn/"; //需要调用的方法名(获得本天气预报Web Services支持的洲、国内外省份和城市信息) private static final String GETSUPPORTPROVINCE = "getSupportProvince"; //实例化SOAP对象,指定命名空间和调用的方法 SoapObject soapObject = new SoapObject(TARGETNAMESPACE,GETSUPPORTPROVINCE);
2、如果方法有参数,添加参数及参数值。
//设置方法需要使用的参数,以及参数值 soapObject.addProperty("theRegionCode",province);
3、设置SOAP请求信息,参数为SOAP的版本(版本只能低于服务端指定的版本)。
//使用SOAP1.1协议创建Envelop对象 SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER10); //设置与.Dot提供的Web Service服务保持良好的兼容性 envelope.dotNet = true; //将初始化的soapObject放入(序列化)使用SOAP1.1协议创建Envelop对象中 envelope.setOutputSoapObject(soapObject);
4、构建传输对象,指定WSDL文档中给定的URL。
//WSDL文档中给定的URL private static final String WSDL = "http://www.webxml.com.cn/webservices/weatherwebservice.asmx"; //构建传输对象,指定URL HttpTransportSE httpTransportSE = new HttpTransportSE(WSDL); //AndroidHttpTransport transport=new AndroidHttpTransport(serviceURL); // transport.debug=true;
5、调用web service(参数1:命名空间+方法名;参数2:envelop对象)
httpTransportSE.call(TARGETNAMESPACE+GETSUPPORTPROVINCE,soapSerializationEnvelope);
6、处理返回结果
SoapObject result = (SoapObject) soapSerializationEnvelope.getResponse(); int count = result.getPropertyCount(); for (int i = 0;i < count;i++){ privinces.add(result.getProperty(i).toString()); System.out.println(result.getProperty(i).toString()); }
以下就是本人写的调用天气预报服务的代码:
import org.ksoap2.SoapEnvelope; import org.ksoap2.serialization.SoapObject; import org.ksoap2.serialization.SoapSerializationEnvelope; import org.ksoap2.transport.HttpTransportSE; import org.xmlpull.v1.XmlPullParserException; import java.io.IOException; import java.util.ArrayList; import java.util.List; /** * Created by wangyan on 2016/1/17. */ public class WebServiceHelper { //目标域名空间(即提供Web Service服务的域名空间) private static final String TARGETNAMESPACE = "http://WebXml.com.cn/"; //WSDL文档中给定的URL private static final String WSDL = "http://www.webxml.com.cn/webservices/weatherwebservice.asmx"; //需要调用的方法名(获得本天气预报Web Services支持的洲、国内外省份和城市信息) private static final String GETSUPPORTPROVINCE = "getSupportProvince"; //需要调用的方法名(获得本天气预报Web Services支持的城市信息,根据省份查询城市集合:带参数) private static final String GETSUPPORTCITY = "getSupportCity"; //根据城市或地区名称查询获得未来三天内天气情况、现在的天气实况、天气和生活指数 private static final String GETWEATHERBYCITYNAME = "getWeatherbyCityName"; /** * 获得洲、国内外省份和某些城市 * @return */ public List getProvince(){ List<String> privinces = new ArrayList<String>(); //实例化SOAP对象,指定命名空间和调用的方法 SoapObject soapObject = new SoapObject(TARGETNAMESPACE,GETSUPPORTPROVINCE); //使用SOAP1.1协议创建Envelop对象 SoapSerializationEnvelope soapSerializationEnvelope = new SoapSerializationEnvelope(SoapEnvelope.VER10); //设置与.Dot提供的Web Service服务保持良好的兼容性 soapSerializationEnvelope.dotNet = true; //将初始化的soapObject放入(序列化)使用SOAP1.1协议创建Envelop对象中 soapSerializationEnvelope.setOutputSoapObject(soapObject); //构建传输对象,指定URL HttpTransportSE httpTransportSE = new HttpTransportSE(WSDL); try { httpTransportSE.call(TARGETNAMESPACE+GETSUPPORTPROVINCE,soapSerializationEnvelope); SoapObject result = (SoapObject) soapSerializationEnvelope.getResponse(); int count = result.getPropertyCount(); for (int i = 0;i < count;i++){ privinces.add(result.getProperty(i).toString()); System.out.println(result.getProperty(i).toString()); } } catch (IOException e) { e.printStackTrace(); } catch (XmlPullParserException e) { e.printStackTrace(); } return privinces; } /** * 根据省得到城市 * @param province * @return */ public List<String> getCitysByProvince(String province){ List<String> citylist = new ArrayList<String>(); SoapObject soapObject = new SoapObject(TARGETNAMESPACE,GETSUPPORTCITY); //设置方法需要使用的参数,以及参数值 soapObject.addProperty("theRegionCode",province); SoapSerializationEnvelope soapSerializationEnvelope = new SoapSerializationEnvelope(SoapEnvelope.VER10); soapSerializationEnvelope.dotNet = true; soapSerializationEnvelope.setOutputSoapObject(soapObject); HttpTransportSE httpTransportSE = new HttpTransportSE(WSDL); try { httpTransportSE.call(TARGETNAMESPACE + GETSUPPORTCITY,soapSerializationEnvelope); SoapObject result = (SoapObject) soapSerializationEnvelope.getResponse(); int count = result.getPropertyCount(); if (result != null && count > 0){ for (int i = 0; i < count; i++){ citylist.add(result.getProperty(i).toString()); System.out.println(result.getProperty(i).toString()); } } } catch (IOException e) { e.printStackTrace(); } catch (XmlPullParserException e) { e.printStackTrace(); } return citylist; } /** * 根据城市获得天气信息 * @param city * @return */ public List<String> getWeatherByCity(String city){ List<String> weatherlist = new ArrayList<String>(); SoapObject soapObject = new SoapObject(TARGETNAMESPACE,GETWEATHERBYCITYNAME); soapObject.addProperty("theCityName", city); SoapSerializationEnvelope soapSerializationEnvelope = new SoapSerializationEnvelope(SoapEnvelope.VER10); soapSerializationEnvelope.dotNet = true; soapSerializationEnvelope.setOutputSoapObject(soapObject); HttpTransportSE httpTransportSE = new HttpTransportSE(WSDL); try { httpTransportSE.call(TARGETNAMESPACE + GETWEATHERBYCITYNAME, soapSerializationEnvelope); SoapObject result = (SoapObject) soapSerializationEnvelope.getResponse(); if (result != null){ int count = result.getPropertyCount(); for (int i = 0; i < count;i++){ //可以用天气类来更好的处理得到的天气信息 weatherlist.add(result.getProperty(i).toString()); } } } catch (IOException e) { e.printStackTrace(); } catch (XmlPullParserException e) { e.printStackTrace(); } return weatherlist; } }