android調用web service(cxf)執行個體應用詳解

來源:互聯網
上載者:User

Google為ndroid平台開發Web Service提供了支援,提供了Ksoap2-android相關架包
1.下載該夾包可以直接登入http://code.google.com/p/ksoap2-android/,現在該網站已經提供了直接的下載,只要點擊下載連結就可以下載了;
我現在的是ksoap2-android-assembly-2.6.5-jar-with-dependencies.jar

2.好了,現在我們就可以進行建立項目來進行測試了,首先我們先建立java服務端,這裡的一些前期準備我就不說了(比如與spring的整合等樣本),
由於這裡重點是android用戶端,java伺服器端就直接給代碼了

Interface:(這裡提供了兩個方法,一個傳遞的是簡單字串,另一個傳遞的是符合對象+集合) 複製代碼 代碼如下:package xidian.sl.service.webService;
import javax.jws.WebParam;
import javax.jws.WebService;
import javax.jws.soap.SOAPBinding;
import javax.jws.soap.SOAPBinding.Style;
import xidian.sl.service.impl.webService.StudentList;
@WebService
@SOAPBinding(style = Style.RPC)
public interface TestService {
public String getUser(@WebParam(name = "name")String name);
public StudentList getStuList();
}

implement: 複製代碼 代碼如下:package xidian.sl.service.impl.webService;
import java.util.ArrayList;
import java.util.List;
import javax.jws.WebService;
import xidian.sl.entity.Students;
import xidian.sl.service.webService.TestService;
@WebService(endpointInterface = "xidian.sl.service.webService.TestService")
public class TestServiceImpl implements TestService {
@Override
public String getUser(String name) {
System.out.println("用戶端傳遞的名字為 = "+name);
return name;
}
@Override
public StudentList getStuList() {
System.out.println("該方法被調用");
List<Students> stuList = new ArrayList<Students>();
//第一個學生
Students stu1 = new Students();
stu1.setStuName("沈浪");
stu1.setStuNum("1006010054");
stu1.setStuSex("男");
stuList.add(stu1);
//第二個學生
Students stu2 = new Students();
stu2.setStuName("香香");
stu2.setStuNum("1006010043");
stu2.setStuSex("女");
stuList.add(stu2);
//將List集合封裝成一個對象才能在webService中進行傳遞
StudentList studentList = new StudentList();
studentList.setStuList(stuList);
return studentList;
}
}

list的封裝對象 複製代碼 代碼如下:package xidian.sl.service.impl.webService;
import java.util.List;
import xidian.sl.entity.Students;
public class StudentList {
private List<Students> stuList;
public List<Students> getStuList() {
return stuList;
}
public void setStuList(List<Students> stuList) {
this.stuList = stuList;
}
}

然後在srping的整合設定檔中進行如下配置即可(預設web.xml中已經進行配置) 複製代碼 代碼如下:<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:jaxws="http://cxf.apache.org/jaxws"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://cxf.apache.org/jaxws
http://cxf.apache.org/schemas/jaxws.xsd">
<import resource="classpath:META-INF/cxf/cxf.xml" /> <!-- 這些xml檔案在cxf-2.5.0.jar的META-INF目錄下-->
<!--<import resource="classpath:META-INF/cxf/cxf-extension-soap.xml" />
警告提示已經廢棄了cxf-extension-soap.xml檔案-->
<import resource="classpath:META-INF/cxf/cxf-servlet.xml" />
<!-- 這裡佈建服務介面,後面描述
id:指在spring配置的bean的ID.
Implementor:指明具體的實作類別.
Address:指明這個web service的相對位址
-->
<!-- 測試 -->
<bean id="testServiceImpl" class="xidian.sl.service.impl.webService.TestServiceImpl" >
</bean>
<jaxws:endpoint id="testService"
implementor="#testServiceImpl"
address="/test" />
<!-- 開啟tomcat伺服器 ,訪問http://localhost:8080/WebExam/services/test?wsdl
http://localhost:8080/WebExam是本項目的訪問地址
services是由於web.xml配置所得,test是由於Spring設定檔中的address屬性所得
-->
</beans>

3.到此伺服器端的已經建立完全,我們可以測試下:開啟tomcat,然後在瀏覽器中輸入http://localhost:8090/WebExam/services/test?wsdl可以查看wsdl

現在我們就可以開始建立android用戶端了
建立一個項目後匯入ksoap2-android-assembly-2.6.5-jar-with-dependencies.jar,這裡要特別注意:匯入包的方式不要選擇項目右鍵---->build path---->
add external archives...,如果使用這種方式表面上好像是匯入了包,但還是沒有辦法引用到,然後啟動項目後一直會報:

我們還是選擇和開發web一樣的方式,就是在項目下建立lib或者libs檔案夾,然後將jar直接複製到該檔案夾中,IDE會協助直接引入的:


這樣就正確無誤了,不再會報類無法引用到了
android中通過webservice調用伺服器端其實還是很簡單的,只要按部就班的按照下面步驟進行即可:
(1)建立HttpTransportSE對象,該對象用於調用WebService操作

複製代碼 代碼如下:HttpTransportSE ht = new HttpTransportSE(SERVICE_URL);

(2)建立SoapSerializationEnvelope對象 複製代碼 代碼如下:SoapSerializationEnvelope envelope = new SoapSerializationEnvelope
(SoapEnvelope.VER11);

(3)建立SoapObject對象,建立該對象時需要傳入所要調用的Web Service的命名空間和WebService方法名 複製代碼 代碼如下:SoapObject request = new SoapObject(SERVICE_NS, methodName);

(4)如果有參數傳給Web Service伺服器端,調用SoapObject對象的addProperty(String name, Object value)方法來設定參數,該方法的name參數指定參數名
注意:參數名不一定要與服務端的方法中的參數名相同,只要對應順序相同即可;value參數指定參數值 複製代碼 代碼如下:request.addProperty("name", "1006010054");

(5)調用SoapSerializationEnvelope的setOutputSoapObject()方法,或者直接對bodyOut屬性賦值,將前兩步建立的SoapObject對象設為SoapSerializationEnvelope的傳出SOAP訊息體 複製代碼 代碼如下:envelope.bodyOut = request;

(6)調用對象的call()方法,並以SoapSerializationEnvelope作為參數調用遠端web service 複製代碼 代碼如下:ht.call(null, envelope);

(7)掉用完成後,訪問SoapSerializationEnvelope對象的bodyIn屬性,該屬性返回一個SoapObject對象,該對象就代表Web service的返回訊息,解析該對象,即可獲得調用web service的傳回值複製代碼 代碼如下:SoapObject result = (SoapObject) envelope.bodyIn;
String name = result.getProperty(0).toString();

下面給書具體的執行個體
mian.xml很簡單就是兩個編輯框: 複製代碼 代碼如下:<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello" />
<EditText
android:id="@+id/editText1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10" >
<requestFocus />
</EditText>
<EditText
android:id="@+id/editText2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10" />
</LinearLayout>

Activity:(該Activity調用了伺服器端返回一般字元串的方法) 複製代碼 代碼如下:package xidian.sl.android.webservice;
import org.ksoap2.SoapEnvelope;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransportSE;
import android.app.Activity;
import android.os.Bundle;
import android.widget.EditText;
public class WebServiceSimpleDemo extends Activity{
final static String SERVICE_NS = "http://webService.service.sl.xidian/";
final static String SERVICE_URL = "http://192.168.1.103:8090/WebExam/services/test";
private EditText txt1;
private EditText txt2;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
txt1 = (EditText) findViewById(R.id.editText1);
txt2 = (EditText) findViewById(R.id.editText2);
//調用的方法
String methodName = "getUser";
//建立httpTransportSE傳輸對象
HttpTransportSE ht = new HttpTransportSE(SERVICE_URL);
ht.debug = true;
//使用soap1.1協議建立Envelop對象
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
//執行個體化SoapObject對象
SoapObject request = new SoapObject(SERVICE_NS, methodName);
/**
* 設定參數,參數名不一定需要跟調用的伺服器端的參數名相同,只需要對應的順序相同即可
* */
request.addProperty("name", "1006010054");
//將SoapObject對象設定為SoapSerializationEnvelope對象的傳出SOAP訊息
envelope.bodyOut = request;
try{
//調用webService
ht.call(null, envelope);
//txt1.setText("看看"+envelope.getResponse());
if(envelope.getResponse() != null){
txt2.setText("有返回");
SoapObject result = (SoapObject) envelope.bodyIn;
String name = result.getProperty(0).toString();
txt1.setText("傳回值 = "+name);
}else{
txt2.setText("無返回");
}
}catch (Exception e) {
e.printStackTrace();
}
}
}

在AndroidManifest.xml進行Activity的註冊和並添加訪問網路的許可權 複製代碼 代碼如下:<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="xidian.sl.android.webservice"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk android:minSdkVersion="10" />
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name" >
<activity
android:name=".WebServiceSimpleDemo"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
<!-- 聲明該應用自身所擁有的許可權 -->
<uses-permission android:name="android.permission.INTERNET" />
</manifest>

運行後的結果:


下面我們來試著調用回傳符合對象的方法:
activity:

複製代碼 代碼如下:package xidian.sl.android.webservice;
import org.ksoap2.SoapEnvelope;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransportSE;
import android.app.Activity;
import android.os.Bundle;
import android.widget.EditText;
public class WebServiceComplexDemo extends Activity{
final static String SERVICE_NS = "http://webService.service.sl.xidian/";
final static String SERVICE_URL = "http://192.168.1.103:8090/WebExam/services/test";
private EditText txt1;
private EditText txt2;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
txt1 = (EditText) findViewById(R.id.editText1);
txt2 = (EditText) findViewById(R.id.editText2);
//調用的方法
String methodName = "getStuList";
//建立httpTransportSE傳輸對象
HttpTransportSE ht = new HttpTransportSE(SERVICE_URL);
ht.debug = true;
//使用soap1.1協議建立Envelop對象
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
//執行個體化SoapObject對象
SoapObject request = new SoapObject(SERVICE_NS, methodName);
/**
* 設定參數,參數名不一定需要跟調用的伺服器端的參數名相同,只需要對應的順序相同即可
* */
//request.addProperty("name", "1006010054");
//將SoapObject對象設定為SoapSerializationEnvelope對象的傳出SOAP訊息
envelope.bodyOut = request;
try{
//調用webService
ht.call(null, envelope);
txt2.setText("回傳的值 :"+envelope.getResponse());
if(envelope.getResponse() != null){
SoapObject result = (SoapObject) envelope.bodyIn;
SoapObject soapChilds = (SoapObject)result.getProperty(0);
StringBuffer sb = new StringBuffer();
for(int i=0; i <soapChilds.getPropertyCount(); i++){
SoapObject soapChildsChilds = (SoapObject)soapChilds.getProperty(i);
sb.append("姓名["+i+"] = "+soapChildsChilds.getProperty(0).toString()+"\n");
sb.append("學號["+i+"] = "+soapChildsChilds.getProperty(1).toString()+"\n");
sb.append("性別["+i+"] = "+soapChildsChilds.getProperty(2).toString()+"\n"+"\n");
}
txt1.setText(sb.toString());
}else{
txt1.setText("無返回");
}
}catch (Exception e) {
e.printStackTrace();
}
}
}

區別就是對於傳回值的處理上,使用幾次getPropert()方法,這裡主要看傳回值的層次,看下面的結果應該就能明白了,根據括弧的層次來進行確定

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.