android呼叫web service(cxf)例項應用詳解

android呼叫web service(cxf)例項應用詳解
1 Star2 Stars3 Stars4 Stars5 Stars 給文章打分!
Loading...

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()方法,這裡主要看返回值的層次,看下面的結果應該就能明白了,根據括號的層次來進行確定

您可能感興趣的文章:

Android基於Service的音樂播放器Android通過startService播放背景音樂Android中實現開機自動啟動服務(service)例項Android中的Service相關全面總結android教程之service使用方法示例詳解Android應用程式四大元件之使用AIDL如何實現跨程序呼叫Serviceandroid使用Messenger繫結Service的多種實現方法Android建立服務之started service詳細介紹基於Android Service 生命週期的詳細介紹Android基於service實現音樂的後臺播放功能示例

相關文章

Android 開發 最新文章