为了让Android应用与远程服务器之间进行交互,可以借助于Java的RMI技术,但这要求远程服务器必须采用Java实现;也要以采用CORBA技术,但这种技术显得于过复杂;除此之外,WebService是一种不错的选择。
关于WebServcie在网上有很多的介绍,在阿堂的技术博客上也有一些文章的介绍,这里我就不多说了,不懂的网友可去网上查阅资料。
下面,阿堂主要介绍Android应用调用WebService的步骤,并且后面附上一Demo的主要代码文件。
Google为Android平台开发WebService客户端提供了ksoap2-android项目,但这个项目并未直接集成在Android平台中,还需要开发人员下载。
下载地址:
http://code.google.com/p/ksoap2-android/站点,该站点有介绍下载ksoap2-android项目的方法。下载过程,阿堂就不再说明了。
使用ksoap2-android调用webservice操作的步骤如下
(1)创建HttpTransportSE对象,该对象用于调用Webservice操作。
(2)创建SoapSerializationEnvelop
e对象。(使用SoapSerializationEnvelop
e对象的bodyOut属性传给服务器,服务器响应生成的SOAP消息也通过SoapSerializationEnvelop
e对象的bodyin属性来获取)
(3)创建SoapObject对象,创建该对象时需要传入所要调用WebService的命名空间。
(4)如果用参数需要传给WebServcie服务端,调用SoapObject对象的addProperty(String name,Objectvalue)方法来设置参数,该方法的name参数指定参数名;value参数指定参数值。
(5)调用SoapSerializationEnvelop
e的setOutputSoapObject()方法,或者直接对bodyOut属性赋值,将前两步创建的SoapObject对象设为SoapSerializationEnvelpe
的传出SOAP消息体。
(6)调用对象的call()方法,并以SoapSerializationEnvelpe
作为参数调用远程WebServcie。
(7)调用完成后,访问SoapSerializationEnvelop
e对象的bodyIn属性,该属性返回一个SoapObjectd对象,该对象就代表了WebService的返回消息。解析该SoapObject对象,即可获取调用Web Service的返回值。
下面,请随要阿堂看一个实例:调用WebService实现天气预报
在开发天气预报的Android应用之前,首先找到一个可以对外提供天气预报的WebService,通过搜索, 发现
http://webservice.webxml.com.cn/WebServices/WeatherWS.asmx站点,可以对外提供天气预报的WebService,因此程序将会调用该站点的Web Service来实现天气预报。
请看阿堂操作时的截图效果
请看三个主要文件(请网友们针对下面的代码,好好体会使用ksoap2-android调用webservice操作的步骤)
public classWebServiceUtil
{
// 定义WebService的命名空间
static finalString SERVICE_NS = "
http://WebXml.com.cn/";
// 定义WebService提供服务的URL
static finalString SERVICE_URL =
"
http://webservice.webxml.com.cn/WebServices/WeatherWS.asmx";
// 调用远程WebService获取省份列表
publicstatic List getProvinceList()
{
//调用的方法
StringmethodName = "getRegionProvince";
//创建HttpTransportSE传输对象
HttpTransportSE ht = new HttpTransportSE(SERVICE_URL);
ht.debug =true;
//使用SOAP1.1协议创建Envelop对象
SoapSerializationEnvelop
e envelope = newSoapSerializationEnvelop
e(
SoapEnvelope.VER11);
//实例化SoapObject对象
SoapObjectsoapObject = new SoapObject(SERVICE_NS, methodName);
envelope.bodyOut = soapObject;
//设置与.Net提供的Web Service保持较好的兼容性
envelope.dotNet = true;
try
{
// 调用WebService
ht.call(SERVICE_NS + methodName, envelope);
if(envelope.getResponse() != null)
{
//获取服务器响应返回的SOAP消息
SoapObjectresult = (SoapObject) envelope.bodyIn;
SoapObjectdetail = (SoapObject) result.getProperty(methodName
+"Result");
//解析服务器响应的SOAP消息。
returnparseProvinceOrCity(detail);
}
}
catch(IOException e)
{
e.printStackTrace();
}
catch(XmlPullParserException e)
{
e.printStackTrace();
}
returnnull;
}
//根据省份获取城市列表
publicstatic List getCityListByProvince(String province)
{
//调用的方法
StringmethodName = "getSupportCityString";
//创建HttpTransportSE传输对象
HttpTransportSE ht = new HttpTransportSE(SERVICE_URL);
ht.debug =true;
//实例化SoapObject对象
SoapObjectsoapObject = new SoapObject(SERVICE_NS, methodName);
//添加一个请求参数
soapObject.addProperty("theRegionCode", province);
//使用SOAP1.1协议创建Envelop对象
SoapSerializationEnvelop
e envelope = newSoapSerializationEnvelop
e(
SoapEnvelope.VER11);
envelope.bodyOut = soapObject;
//设置与.Net提供的Web Service保持较好的兼容性
envelope.dotNet = true;
try
{
// 调用WebService
ht.call(SERVICE_NS + methodName, envelope);
if(envelope.getResponse() != null)
{
//获取服务器响应返回的SOAP消息
SoapObjectresult = (SoapObject) envelope.bodyIn;
SoapObjectdetail = (SoapObject) result.getProperty(methodName
+"Result");
//解析服务器响应的SOAP消息。
returnparseProvinceOrCity(detail);
}
}
catch(IOException e)
{
e.printStackTrace();
}
catch(XmlPullParserException e)
{
e.printStackTrace();
}
returnnull;
}
privatestatic List parseProvinceOrCity(SoapObject detail)
{
ArrayListresult = new ArrayList();
for (int i =0; i < detail.getPropertyCount(); i++)
{
//解析出每个省份
result.add(detail.getProperty(i).toString().split(",")[0]);
}
returnresult;
}
publicstatic SoapObject getWeatherByCity(String cityName)
{
StringmethodName = "getWeather";
HttpTransportSE ht = new HttpTransportSE(SERVICE_URL);
ht.debug =true;
SoapSerializationEnvelop
e envelope = newSoapSerializationEnvelop
e(
SoapEnvelope.VER11);
SoapObjectsoapObject = new SoapObject(SERVICE_NS, methodName);
soapObject.addProperty("theCityCode", cityName);
envelope.bodyOut = soapObject;
//设置与.Net提供的Web Service保持较好的兼容性
envelope.dotNet = true;
try
{
ht.call(SERVICE_NS + methodName, envelope);
SoapObjectresult = (SoapObject) envelope.bodyIn;
SoapObjectdetail = (SoapObject) result.getProperty(methodName
+"Result");
returndetail;
}
catch(Exception e)
{
e.printStackTrace();
}
returnnull;
}
}
----------------------------------------------------------------------------------------------------------------
public classListAdapter extends BaseAdapter
{
privateContext context;
private Listvalues;
publicListAdapter(Context context , List values)
{
this.context= context;
this.values= values;
}
@Override
public intgetCount()
{
returnvalues.size();
}
@Override
publicObject getItem(int position)
{
returnvalues.get(position);
}
@Override
public longgetItemId(int position)
{
returnposition;
}
@Override
public ViewgetView(int position, View convertView, ViewGroup parent)
{
TextViewtext = new TextView(context);
text.setText(values.get(position));
text.setTextSize(20);
text.setTextColor(Color.BLACK);
returntext;
}
}
----------------------------------------------------------------------------------------------------------------
public classMyWeather extends Activity
{
privateSpinner provinceSpinner;
privateSpinner citySpinner;
privateImageView todayWhIcon1;
privateImageView todayWhIcon2;
privateTextView textWeatherToday;
privateImageView tomorrowWhIcon1;
privateImageView tomorrowWhIcon2;
privateTextView textWeatherTomorrow;
privateImageView afterdayWhIcon1;
privateImageView afterdayWhIcon2;
privateTextView textWeatherAfterday;
privateTextView textWeatherCurrent;
@Override
public voidonCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
todayWhIcon1= (ImageView) findViewById(R.id.todayWhIcon1);
todayWhIcon2= (ImageView) findViewById(R.id.todayWhIcon2);
textWeatherToday = (TextView)findViewById(R.id.weatherToday);
tomorrowWhIcon1 = (ImageView)findViewById(R.id.tomorrowWhIcon1);
tomorrowWhIcon2 = (ImageView)findViewById(R.id.tomorrowWhIcon2);
textWeatherTomorrow = (TextView)findViewById(R.id.weatherTomorrow);
afterdayWhIcon1 = (ImageView)findViewById(R.id.afterdayWhIcon1);
afterdayWhIcon2 = (ImageView)findViewById(R.id.afterdayWhIcon2);
textWeatherAfterday = (TextView)findViewById(R.id.weatherAfterday);
textWeatherCurrent = (TextView)findViewById(R.id.weatherCurrent);
//获取程序界面中选择省份、城市的Spinner组件
provinceSpinner = (Spinner) findViewById(R.id.province);
citySpinner= (Spinner) findViewById(R.id.city);
// 调用远程WebService获取省份列表
Listprovinces = WebServiceUtil.getProvinceList();
ListAdapteradapter = new ListAdapter(this, provinces);
//使用Spinner显示省份列表
provinceSpinner.setAdapter(adapter);
//当省份Spinner的选择项被改变时
provinceSpinner.setOnItemSelectedListene
r(newOnItemSelectedListener()
{
@Override
public voidonItemSelected(AdapterView
转载地址:http://blog.sina.com.cn/s/blog_4c925dca0101b2ya.html