android調用web service接口例子

下面給書具體的實例: 
mian.xml很簡單就是兩個編輯框: 
android

複製代碼代碼以下:web


<?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調用了服務器端返回普通字符串的方法) 
網絡

複製代碼代碼以下:app


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(); 



ide


在AndroidManifest.xml進行Activity的註冊和並添加訪問網絡的權限 
spa

複製代碼代碼以下:debug


<?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> 
orm


運行後的結果如圖所示: 
xml


下面咱們來試着調用回傳符合對象的方法: 
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()方法,這裏主要看返回值的層次,看下面的結果應該就能明白了,根據括號的層次來進行肯定 

相關文章
相關標籤/搜索