1、Android与服务器端数据交互作者:东子哥来源:博客园发布时间: 2011-04-24 21:43阅读: 2358 次上一节中我们通过http协议,采用HttpClient向服务器端action请求数据。当然调用服务器端方法获取数据并不止这一种。WebService也可以为我们提供所需数据,那么什么是webService呢?,它是一种基于SAOP协议的远程调用标准,通过webservice可以将不同操作系统平台,不同语言,不同技术整合到一起。我们在PC机器java客户端中,需要一些库,比如XFire,Axis2,CXF等等来支持访问WebService,但是这些库并不适合我们资源有限的and
2、roid手机客户端,做过JAVA ME的人都知道有KSOAP这个第三方的类库,可以帮助我们获取服务器端webService调用,当然KSOAP已经提供了基于android版本的jar包了,那么我们就开始吧:首先下载KSOAP包:ksoap2-android-assembly-2.5.2-jar-with-dependencies.jar包然后新建android项目:并把下载的KSOAP包放在android项目的lib目录下:右键-build path-configure build path-选择Libraries,如图:以下分为七个步骤来调用WebService方法:第一:实例化SoapOb
3、ject 对象,指定webService的命名空间(从相关WSDL文档中可以查看命名空间),以及调用方法名称。如:/命名空间private static final String serviceNameSpace=http:/WebX/调用方法(获得支持的城市)private static final String getSupportCity=getSupportCity;/实例化SoapObject对象SoapObject request=new SoapObject(serviceNameSpace, getSupportCity);第二步:假设方法有参数的话,设置调用方法参数:requ
4、est.addProperty(参数名称,参数值);第三步:设置SOAP请求信息(参数部分为SOAP协议版本号,与你要调用的webService中版本号一致):/获得序列化的EnvelopeSoapSerializationEnvelope envelope=new SoapSerializationEnvelope(SoapEnvelope.VER11);envelope.bodyOut=request;第四步:注册Envelope:(new MarshalBase64().register(envelope);第五步:构建传输对象,并指明WSDL文档URL:/请求URLprivate st
5、atic final String serviceURL=/Android传输对象AndroidHttpTransport transport=new AndroidHttpTransport(serviceURL);transport.debug=true;第六步:调用WebService(其中参数为1:命名空间+方法名称,2:Envelope对象):transport.call(serviceNameSpace+getWeatherbyCityName, envelope);第七步:解析返回数据:if(envelope.getResponse()!=null) return parse(e
6、nvelope.bodyIn.toString(); /* * 解析XML * param str * return */ private static List parse(String str) String temp; List list=new ArrayList(); if(str!=null & str.length()0) int start=str.indexOf(string); int end=str.lastIndexOf(;); temp=str.substring(start, end-3); String test=temp.split(;); for(int i=
7、0;itest.length;i+) if(i=0) temp=testi.substring(7); else temp=testi.substring(8); int index=temp.indexOf(,); list.add(temp.substring(0, index); return list; 这样就成功啦。那么现在我们就来测试下吧,这里有个地址提供webService天气预报的服务的,我这里只提供获取城市列表:/命名空间private static final String serviceNameSpace=http:/WebX/请求URLprivate static fi
8、nal String serviceURL=/调用方法(获得支持的城市)private static final String getSupportCity=getSupportCity;/调用城市的方法(需要带参数)private static final String getWeatherbyCityName=getWeatherbyCityName;/调用省或者直辖市的方法(获得支持的省份或直辖市)private static final String getSupportProvince=getSupportProvince;然后你可以在浏览器中输入地址(WSDL):serviceUR
9、L,你会看到一些可供调用的方法:我们选择获取国内外主要城市或者省份的方法吧:getSupportProvice,然后调用,你会发现浏览器返回给我们的是xml文档: - ArrayOfString xmlns:xsi=http:/www.w3.org/2001/XMLSchema-instance xmlns:xsd=http:/www.w3.org/2001/XMLSchema xmlns=http:/WebX 直辖市 特别行政区 黑龙江 吉林 辽宁 内蒙古 河北 河南 山东 山西 江苏 安徽 陕西 宁夏 甘肃 青海 湖北 湖南 浙江 江西 福建 贵州 四川 广东 广西 云南 海南 新疆 西藏
10、 台湾 亚洲 欧洲 非洲 北美洲 南美洲 大洋洲 我们可以用 listview来显示:那么下面我将给出全部代码:public class WebServiceHelper /WSDL文档中的命名空间 private static final String targetNameSpace=http:/WebX /WSDL文档中的URL private static final String WSDL= /需要调用的方法名(获得本天气预报Web Services支持的洲、国内外省份和城市信息) private static final String getSupportProvince=getSu
11、pportProvince; /需要调用的方法名(获得本天气预报Web Services支持的城市信息,根据省份查询城市集合:带参数) private static final String getSupportCity=getSupportCity; /根据城市或地区名称查询获得未来三天内天气情况、现在的天气实况、天气和生活指数 private static final String getWeatherbyCityName=getWeatherbyCityName; /* * 获得州,国内外省份和城市信息 * return */ public List getProvince() List
12、 provinces=new ArrayList(); String str=; SoapObject soapObject=new SoapObject(targetNameSpace,getSupportProvince); /request.addProperty(参数, 参数值);调用的方法参数与参数值(根据具体需要可选可不选) SoapSerializationEnvelope envelope=new SoapSerializationEnvelope(SoapEnvelope.VER11); envelope.dotNet=true; envelope.setOutputSoap
13、Object(soapObject);/envelope.bodyOut=request; AndroidHttpTransport httpTranstation=new AndroidHttpTransport(WSDL); /或者HttpTransportSE httpTranstation=new HttpTransportSE(WSDL); try httpTranstation.call(targetNameSpace+getSupportProvince, envelope); SoapObject result=(SoapObject)envelope.getResponse(
14、); /下面对结果进行解析,结构类似json对象 /str=(String) result.getProperty(6).toString(); int count=result.getPropertyCount(); for(int index=0;indexcount;index+) provinces.add(result.getProperty(index).toString(); catch (IOException e) / TODO Auto-generated catch block e.printStackTrace(); catch (XmlPullParserExcept
15、ion e) / TODO Auto-generated catch block e.printStackTrace(); return provinces; /* * 根据省份或者直辖市获取天气预报所支持的城市集合 * param province * return */ public List getCitys(String province) List citys=new ArrayList(); SoapObject soapObject=new SoapObject(targetNameSpace,getSupportCity); soapObject.addProperty(byP
16、rovinceName, province); SoapSerializationEnvelope envelope=new SoapSerializationEnvelope(SoapEnvelope.VER11); envelope.dotNet=true; envelope.setOutputSoapObject(soapObject); AndroidHttpTransport httpTransport=new AndroidHttpTransport(WSDL); try httpTransport.call(targetNameSpace+getSupportCity, enve
17、lope); SoapObject result=(SoapObject)envelope.getResponse(); int count=result.getPropertyCount(); for(int index=0;indexcount;index+) citys.add(result.getProperty(index).toString(); catch (IOException e) / TODO Auto-generated catch block e.printStackTrace(); catch (XmlPullParserException e) / TODO Au
18、to-generated catch block e.printStackTrace(); return citys; /* * 根据城市信息获取天气预报信息 * param city * return */ public WeatherBean getWeatherByCity(String city) WeatherBean bean=new WeatherBean(); SoapObject soapObject=new SoapObject(targetNameSpace,getWeatherbyCityName); soapObject.addProperty(theCityName
19、,city);/调用的方法参数与参数值(根据具体需要可选可不选) SoapSerializationEnvelope envelope=new SoapSerializationEnvelope(SoapEnvelope.VER11); envelope.dotNet=true; envelope.setOutputSoapObject(soapObject);/envelope.bodyOut=request; AndroidHttpTransport httpTranstation=new AndroidHttpTransport(WSDL); /或者HttpTransportSE htt
20、pTranstation=new HttpTransportSE(WSDL); try httpTranstation.call(targetNameSpace+getWeatherbyCityName, envelope); SoapObject result=(SoapObject)envelope.getResponse(); /下面对结果进行解析,结构类似json对象 bean=parserWeather(result); catch (IOException e) / TODO Auto-generated catch block e.printStackTrace(); catch
21、 (XmlPullParserException e) / TODO Auto-generated catch block e.printStackTrace(); return bean; /* * 解析返回的结果 * param soapObject */ protected WeatherBean parserWeather(SoapObject soapObject) WeatherBean bean=new WeatherBean(); ListMap list=new ArrayListMap(); Map map=new HashMap(); /城市名 bean.setCityN
22、ame(soapObject.getProperty(1).toString(); /城市简介 bean.setCityDescription(soapObject.getProperty(soapObject.getPropertyCount()-1).toString(); /天气实况+建议 bean.setLiveWeather(soapObject.getProperty(10).toString()+n+soapObject.getProperty(11).toString(); /其他数据 /日期, String date=soapObject.getProperty(6).toS
23、tring(); /- String weatherToday=今天: + date.split( )0; weatherToday+=n天气:+ date.split( )1; weatherToday+=n气温:+soapObject.getProperty(5).toString(); weatherToday+=n风力:+soapObject.getProperty(7).toString(); weatherToday+=n; List icons=new ArrayList(); icons.add(parseIcon(soapObject.getProperty(8).toStr
24、ing(); icons.add(parseIcon(soapObject.getProperty(9).toString(); map.put(weatherDay, weatherToday); map.put(icons,icons); list.add(map); /- map=new HashMap(); date=soapObject.getProperty(13).toString(); String weatherTomorrow=明天: + date.split( )0; weatherTomorrow+=n天气:+ date.split( )1; weatherTomorr
25、ow+=n气温:+soapObject.getProperty(12).toString(); weatherTomorrow+=n风力:+soapObject.getProperty(14).toString(); weatherTomorrow+=n; icons=new ArrayList(); icons.add(parseIcon(soapObject.getProperty(15).toString(); icons.add(parseIcon(soapObject.getProperty(16).toString(); map.put(weatherDay, weatherTomorrow); map.put(icons,icons); list.add(map); /- map=new HashMap(); date=soapObject.getProperty(18).toString(); String weatherAfterTomorrow=后天: + date.split( )0; weatherAfterTo
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1