之前主要做客户端UI交互,很少处理数据和接触服务端,但现在的移动设备根本不可能离得开网络连接,数据的交换。最近学习的是在android端如何去调用远程WebService,都说WebService是一种基于SOAP协议的远程调用标准,对于这个协议理解不深,知道webservice可以将不同操作系统平台、不同语言、不同技术整合到一块,android SDK没有直接调用webservice的库,最常用的是借助ksoap2-android这个第三方SDK,点击打开链接,然后和其他第三方jar包一样导入android项目中即可。
对于WebService服务器具体的编写,现阶段自己不了解,暂且使用网络免费的WebService进行学习http://www.webxml.com.cn/zh_cn/index.aspx,利用这个来写一个简单的国内天气预报的查询,代码编写参照《疯狂Android讲义》相关部分,自己做部分修改。
-------------------------------------------------------------------------------------------------------------
以下是几个主要的方法:
1.创建HttpTransportSE传输对象:HttpTransportSE ht = new HttpTransportSE(SERVICE_URL); SERVICE_URL是webservice提供服务的url
2.使用SOAP1.1协议创建Envelop对象:SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11); 设置SOAP协议的版本号,根据服务端WebService的版本号设置。
3.实例化SoapObject对象:SoapObject soapObject = new SoapObject(SERVICE_NAMESPACE, methodName); 第一个参数表示WebService的命名空间,可以从WSDL文档中找到WebService的命名空间。第二个参数表示要调用的WebService方法名。
4.设置调用方法的参数值,如果没有参数,可以省略:例如soapObject.addProperty("theCityCode", cityName);
5.记得设置bodyout属性 envelope.bodyOut = soapObject;
6.调用webservice:ht.call(SERVICE_NAMESPACE+methodName, envelope);
7.获取服务器响应返回的SOAP消息:
SoapObject result = (SoapObject) envelope.bodyIn;
SoapObject detail = (SoapObject) result.getProperty(methodName+"Result");
-------------------------------------------------------------------------------------------------------------
在这个天气预报的demo中需要多次调用webservice,将方法写在一个工具类中,代码如下:
package com.example.webservicedemo;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import org.ksoap2.SoapEnvelope;
import org.ksoap2.SoapFault;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransportSE;
import org.xmlpull.v1.XmlPullParserException;
public class WebServiceUtil {
// 定义webservice的命名空间
public static final String SERVICE_NAMESPACE = "http://WebXml.com.cn/";
// 定义webservice提供服务的url
public static final String SERVICE_URL = "http://webservice.webxml.com.cn/WebServices/WeatherWS.asmx";
// 调用远程webservice获取省份列表
public static List<String> getProvinceList() {
// 调用 的方法
String methodName = "getRegionProvince";
// 创建HttpTransportSE传输对象
HttpTransportSE ht = new HttpTransportSE(SERVICE_URL);
try {
ht.debug = true;
// 使用SOAP1.1协议创建Envelop对象
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
SoapEnvelope.VER11);
// 实例化SoapObject对象
SoapObject soapObject = new SoapObject(SERVICE_NAMESPACE,
methodName);
envelope.bodyOut = soapObject;
// 设置与.NET提供的webservice保持较好的兼容性
envelope.dotNet = true;
// 调用webservice
ht.call(SERVICE_NAMESPACE + methodName, envelope);
if (envelope.getResponse() != null) {
// 获取服务器响应返回的SOAP消息
SoapObject result = (SoapObject) envelope.bodyIn;
SoapObject detail = (SoapObject) result.getProperty(methodName
+ "Result");
// 解析服务器响应的SOAP消息
return parseProvinceOrCity(detail);
}
} catch (SoapFault e) {
// TODO Auto-generated catch block
e.printStackTrace();
} 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> getCityListsByProvince(String province) {
// 调用的方法
String methodName = "getSupportCityString";
// 创建httptransportSE传输对象
HttpTransportSE ht = new HttpTransportSE(SERVICE_URL);
ht.debug = true;
// 实例化SoapObject对象
SoapObject soapObject = new SoapObject(SERVICE_NAMESPACE, methodName);
// 添加一个请求参数
soapObject.addProperty("theRegionCode", province);
// 使用soap1.1协议创建envelop对象
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
SoapEnvelope.VER11);
envelope.bodyOut = soapObject;
// 设置与.NET提供的webservice保持较好的兼容性
envelope.dotNet = true;
// 调用webservice
try {
ht.call(SERVICE_NAMESPACE + methodName, envelope);
if (envelope.getResponse() != null) {
// 获取服务器响应返回的SOAP消息
SoapObject result = (SoapObject) envelope.bodyIn;
SoapObject detail = (SoapObject) result.getProperty(methodName
+ "Result");
// 解析服务器响应的SOAP消息
return parseProvinceOrCity(detail);
}
} catch (SoapFault e) {
// TODO Auto-generated catch block
e.printStackTrace();
} 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> parseProvinceOrCity(SoapObject detail) {
ArrayList<String> result = new ArrayList<String>();
for (int i = 0; i < detail.getPropertyCount(); i++) {
// 解析出每个省份
result.add(detail.getProperty(i).toString().split(",")[0]);
}
return result;
}
// 根据城市字符串获取相应天气情况
public static SoapObject getWeatherByCity(String cityName) {
String methodName = "getWeather";
HttpTransportSE ht = new HttpTransportSE(SERVICE_URL);
ht.debug = true;
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
SoapEnvelope.VER11);
SoapObject soapObject = new SoapObject(SERVICE_NAMESPACE, methodName);
soapObject.addProperty("theCityCode", cityName);
envelope.bodyOut = soapObject;
envelope.dotNet = true;
try {
ht.call(SERVICE_NAMESPACE + methodName, envelope);
SoapObject result = (SoapObject) envelope.bodyIn;
SoapObject detail = (SoapObject) result.getProperty(methodName
+ "Result");
return detail;
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (XmlPullParserException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
}
上面这个便是主要实现远程调用webservice的代码,其他实现在activity中完成即可,但是这里也会涉及到一个问题,就是Android多线程问题,在调用webservice时,为了防止ANR的出现,不能在主线程中进行,需要另开子线程执行,,因为子线程涉及到UI更新,Android主线程是线程不安全的,也就是说,更新UI只能在主线程中更新,子线程中操作是危险的.便要涉及Handler的使用了,实在是不才,自己更多时候习惯使用AsyncTask来代替Handler(还是要重新琢磨下Handler的使用方法),因为操作起来比较简单方便,下面是项目中异步操作类中的一个:
package com.example.webservicedemo;
import java.util.List;
import android.os.AsyncTask;
import android.widget.Spinner;
public class ProvinceAsyncTask extends AsyncTask<String, String, List<String>> {
private MainActivity activity;
private List<String> provinces;
private Spinner sp_province;
public ProvinceAsyncTask (MainActivity activity,Spinner sp_province){
this.activity = activity;
this.sp_province = sp_province;
}
@Override
protected List<String> doInBackground(String... params) {
// TODO Auto-generated method stub
provinces = WebServiceUtil.getProvinceList();
return null;
}
@Override
protected void onPostExecute(List<String> result) {
// TODO Auto-generated method stub
ListAdapter adapter = new ListAdapter(activity, provinces);
sp_province.setAdapter(adapter);
}
}
可以看出来比较简单,在doInBackground中执行调用webservice,在onPostExecute中执行列表刷新展示
最后附上该demo的代码,希望可以对初涉及android调用webservice的朋友有帮助
http://download.csdn.net/detail/eyu8874521/5609627