安卓调用Webservice服务

最近因为老师让写一个关于webservice的小项目,无论调用什么服务都可以。我就随意找了个将摄氏度转换成华氏度的服务来写。

我刚开始也不知道webservice是什么,但是网上有很多资料,于是我也就开始了学习,以下是我自己比较通俗的理解。

首先,我们要理解什么是webservice以及用到了哪些知识:

Webservice并不是安卓独有的服务,在很多平台都会用到这个服务。在android中,webservice被用来在application之间传递、交换、获取信息,webservice是基于http协议的,通过network调用远程服务器上面的方法得到想要的数据或信息。通过Webservice要实现通信和数据交流必须通过信息协议,soap就是其中一种。

接下来我们实现一个简单调用Webservice的例子:

       一、首先要下载ksoap库以支持所需服务, http://code.google.com/p/ksoap2-android/进入此网址后进入downloads页面,找到latest release……点击后面jar包的链接,进入之后一定要view raw file右键链接另存为jar包,其他方式下载可能会导致下载的jar包不正确而无法引入到程序当中。我就是直接点的downloads页面的链接另存为jar包之后,导入项目出现了红色感叹号,一直找不到原因,也没出现任何的错误信息,这是个坑,最后在网上搜索各种jar包的导入方法无意间看到了不一样的下载方法,结果试了下成功了。

       二、然后我们打开Eclipse创建新的Android工程,点击Eclipse菜单栏的File-〉New-〉Android Application project。如果没有Android ApplicationProject选项就进入Other,选择Android文件夹中的Android Application Project选项,然后Ok。

Android零度调节 android 温度_android

接下来会看到下列界面,选择Android程序的版本以及填写名称:目前最高版本是21,我们选择API19版本就可以。特别说明:在Android4.0之后不可以在主UI线程中直接引用webservice服务,会导致NullPointer错误。我还没遇到其他错误,所以目前只知道这个。

Android零度调节 android 温度_Android零度调节_02

然后一路点击next直到finish,此时一个新的没有ksoap库的Android程序已经建立完毕。

接下来,我们将下载好的ksoap库jar包放进工程的libs文件夹中。如果工程本身没有libs文件夹就新建一个(或者可以右击工程进入-〉Build Path-〉ConfigureBuild Path中的Libraries中添加jar包)。添加完ksoap库之后我们就可以使用它为我们提供的webservice的服务了。

 

Android零度调节 android 温度_soap_03

三、新写一个Android的布局文件,在工程文件中找到res-〉layout-〉activity_main.xml,双击打开此布局文件,这个布局文件就是用来显示Android工程的界面。在这个布局文件中修改代码达到我们想要的效果。这个调用webservice的服务相信很多人都写过,因为非常简单,类似手机号码归属地查询等属于最基础的服务。

Android零度调节 android 温度_android_04

Android零度调节 android 温度_web服务_05

  

布局文件代码:

<RelativeLayoutxmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.androidwebservicedemo.MainActivity">
 
<TextView
android:id="@+id/title"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:textSize="25sp"
android:text=""/>
 
<Button
android:id="@+id/Convert"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/inputInfo"
android:layout_marginTop="38dp"
android:text=""/>
 
<TextView
android:id="@+id/result"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/Convert"
android:layout_marginTop="18dp"
android:textSize="20sp"
android:text=""/>
 
<EditText
android:id="@+id/inputInfo"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/title"
android:hint=""
android:ems="10"/>
</RelativeLayout>

工程文件布置完成之后,我们就要在MainActivity中进行Webservice的调用。

       四、在调用Webservice之前,我们还要明白WSDL是什么,它是一种描述Web服务的XML语言,描述了Web服务的功能、接口、参数以及返回值等,它是一种实在的xml文档。我们通过Soap协议来调用WSDL服务功能。举个WSDL的例子,下面是一份WSDL的文档:

Android零度调节 android 温度_Android零度调节_06


用红色框圈中了四处,从上到下依次为:WSDL的命名空间,调用webservice时候用到的方法名,以及方法中需要传入的两个参数名称及其类型。在网上可以查到我们需要webservice服务中wsdl文档的URL。

我们要做的是将摄氏度转换成华氏度的Android工程,可以直接在网上搜索这个功能的wsdl文档。如下所示,标注了命名空间和相关方法:

Android零度调节 android 温度_webservice_07


现在我们开始调用webservice:声明命名空间,调用服务的方法名以及调用wsdl的Url,通常SoapAction的地址是命名空间加上方法名。

关键代码:

//declare the namespace, methodName and  url of wsdl we called.
   private  final String NAMESPACE="http://www.w3schools.com/webservices/";
   private  final String URL="http://www.w3schools.com/webservices/tempconvert.asmx";
//usually ,the soap_action is namespace plus method's name
   private  final String SOAP_ACTION="http://www.w3schools.com/webservices/CelsiusToFahrenheit";
   private  final String METHOD_NAME="CelsiusToFahrenheit";

接下来我们新建一个线程来调用这个服务,在线程中执行getFarenheit(celsius)这个方法。之后我们创建一个名为getFarenheit(String)的方法,在其中调用webservice服务。

//we called this service in an AdyncTask class.
@Override
      protected Void doInBackground(String...params) {
// TODO Auto-generated method stub
celsius);
         Log.i("background---calculation","-----");
         return  null;
      }

首先实例化SoapObject对象,传入命名空间和方法名参数,然后设置Property对象,携带要传递的数据给服务器(名称,值,类型等),将其放在SoapObject中传递给实例化的Envelope对象,SoapEnvelope.VER11是版本号。很多时候在调用webservice时会发生异常,这个时候可以尝试将SoapEnvelope.VER11更改成SoapEnvelope.VER12或者SoapEnvelope.VER10。然后新建HttpTransportSE传输对象,利用call函数进行传输并返回response结果。

//getFarenheit Method is to call wsdl's CelsiusToFahrenheit service
   public  void getFarenheit(String celsius)
   {
//create request
request = new SoapObject(NAMESPACE,METHOD_NAME);
//property which holds input parameters
celsiusPi = new PropertyInfo();
//set name, value and type
celsiusPi.setName("Celsius");
celsiusPi.setValue(celsius);
celsiusPi.setType(double.class);
//add the property to request object
request.addProperty(celsiusPi);
//create envelope
envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
envelope.dotNet =true;
//set output soap object
envelope.setOutputSoapObject(request);
//create http call object
androidHttpTransport =new HttpTransportSE(URL);
      try{
//involve web service
androidHttpTransport.call(SOAP_ACTION,envelope);
//get the response
response = (SoapPrimitive)envelope.getResponse();
farenheit =  response.toString();
      }catch(Exception  e)
      {
e.printStackTrace();
      }
   }

这个小的将摄氏度转化成华氏度的程序就完成了。调用Webservice非常方便快捷,远程的服务器有很多方法,我们只是调用了其中一个服务器的一个方法。熟悉了Webservice的操作之后,可以自己写服务,然后在Android端调用自己生成wsdl文档的方法进行数据操作。