客户端,服务器,天气预报

引用:http://www.cnblogs.com/zhangdongzi/archive/2011/04/19/2020688.html

上一节中我们通过http协议,采用HttpClient向服务器端action请求数据。当然调用服务器端方法获取数据并不止这一种。WebService也可以为我们提供所需数据,

那么什么是webService呢?,它是一种基于SAOP协议的远程调用标准,通过webservice可以将不同操作系统平台,不同语言,不同技术整合到一起。

  我们在PC机器java客户端中,需要一些库,比如XFire,Axis2,CXF等等来支持访问WebService,但是这些库并不适合我们资源有限的android手机客户端,做过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方法:

第一:实例化SoapObject 对象,指定webService的命名空间(从相关WSDL文档中可以查看命名空间),以及调用方法名称。如:

View Code
复制代码
// 命名空间
private static final String serviceNameSpace = " http://WebXml.com.cn/ " ;
// 调用方法(获得支持的城市)
private static final String getSupportCity = " getSupportCity " ;

// 实例化SoapObject对象
SoapObject request = new SoapObject(serviceNameSpace, getSupportCity);
复制代码

第二步:假设方法有参数的话,设置调用方法参数

request.addProperty("参数名称","参数值");

第三步:设置SOAP请求信息(参数部分为SOAP协议版本号,与你要调用的webService中版本号一致):

View Code
// 获得序列化的Envelope
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
envelope.bodyOut = request;

第四步:注册Envelope,

(new MarshalBase64()).register(envelope);

第五步:构建传输对象,并指明WSDL文档URL:

View Code
// 请求URL
private static final String serviceURL = " http://www.webxml.com.cn/webservices/weatherwebservice.asmx " ;
// Android传输对象
AndroidHttpTransport transport = new AndroidHttpTransport(serviceURL);
transport.debug = true ;

第六步:调用WebService(其中参数为1:命名空间+方法名称,2:Envelope对象):

View Code

第七步:解析返回数据:

View Code

这样就成功啦。那么现在我们就来测试下吧,这里有个地址提供webService天气预报的服务的,我这里只提供获取城市列表:

View Code

 然后你可以在浏览器中输入地址(WSDL):serviceURL,你会看到一些可供调用的方法:

 我们选择获取国内外主要城市或者省份的方法吧:getSupportProvice,然后调用,你会发现浏览器返回给我们的是xml文档:

View Code

我们可以用 listview来显示:

那么下面我将给出全部代码:

View Code

以及帮助类:

View Code
复制代码
public class WebServiceUtil {

// 命名空间
private static final String serviceNameSpace = " http://WebXml.com.cn/ " ;
// 请求URL
private static final String serviceURL = " http://www.webxml.com.cn/webservices/weatherwebservice.asmx " ;
// 调用方法(获得支持的城市)
private static final String getSupportCity = " getSupportCity " ;
// 调用城市的方法(需要带参数)
private static final String getWeatherbyCityName = " getWeatherbyCityName " ;
// 调用省或者直辖市的方法(获得支持的省份或直辖市)
private static final String getSupportProvince = " getSupportProvince " ;

/** ***********
* @return城市列表
************ */
public static List < String > getCityList(){
// 实例化SoapObject对象
SoapObject request = new SoapObject(serviceNameSpace, getSupportCity);
// 获得序列化的Envelope
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
envelope.bodyOut = request;
( new MarshalBase64()).register(envelope);
// Android传输对象
AndroidHttpTransport transport = new AndroidHttpTransport(serviceURL);
transport.debug = true ;

// 调用
try {
transport.call(serviceNameSpace + getWeatherbyCityName, envelope);
if (envelope.getResponse() != null ){
return parse(envelope.bodyIn.toString());
}

} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (XmlPullParserException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}


return null ;
}


public static List < String > getProviceList(){
// 实例化SoapObject对象
SoapObject request = new SoapObject(serviceNameSpace, getSupportProvince);
// 获得序列化的Envelope
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
envelope.bodyOut = request;
( new MarshalBase64()).register(envelope);
// Android传输对象
AndroidHttpTransport transport = new AndroidHttpTransport(serviceURL);
transport.debug = true ;

// 调用
try {
transport.call(serviceNameSpace + getWeatherbyCityName, envelope);
if (envelope.getResponse() != null ){
return null ;
}

} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (XmlPullParserException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}


return null ;
}

/** ***********
* @param cityName
* @return
************ */
public static String getWeather(String cityName){

return "" ;
}

/** ************
* 解析XML
* @param str
* @return
*/
private static List < String > parse(String str){
String temp;
List < String > list = new ArrayList < String > ();
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 = 0 ;i < test.length;i ++ ){
if (i == 0 ){
temp = test[i].substring( 7 );
} else {
temp = test[i].substring( 8 );
}
int index = temp.indexOf( " , " );
list.add(temp.substring( 0 , index));
}
}
return list;
}

/** *******
* 获取天气
* @param soapObject
*/
private void parseWeather(SoapObject soapObject){
// String date=soapObject.getProperty(6);
}
}
复制代码

以上就是我所作的查询天气预报的全部核心代码了,读者可以根据注释以及本文章了解下具体实现,相信很快就搞明白了,运行结果如下:

 到此结束,下一节主要是socket通信了。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值