android調(diào)用WebService實(shí)例分析
本文實(shí)例講述了android調(diào)用WebService的方法。分享給大家供大家參考。具體如下:
WebService是一種基于SOAP協(xié)議的遠(yuǎn)程調(diào)用標(biāo)準(zhǔn),通過(guò)webservice可以將不同操作系統(tǒng)平臺(tái)、不同語(yǔ)言、不同技術(shù)整合到一塊。在Android SDK中并沒(méi)有提供調(diào)用WebService的庫(kù),因此,需要使用第三方的SDK來(lái)調(diào)用WebService。PC版本的WEbservice客戶(hù)端庫(kù)非常豐富,例如Axis2,CXF等,但這些開(kāi)發(fā)包對(duì)于Android系統(tǒng)過(guò)于龐大,也未必很容易移植到Android系統(tǒng)中。因此,這些開(kāi)發(fā)包并不是在我們的考慮范圍內(nèi)。適合手機(jī)的WebService客戶(hù)端的SDK有一些,比較常用的有Ksoap2,可以從http://code.google.com/p/ksoap2-android/downloads/list進(jìn)行下載;將下載的ksoap2-android-assembly-2.4-jar-with-dependencies.jar包復(fù)制到Eclipse工程的lib目錄中,當(dāng)然也可以放在其他的目錄里。同時(shí)在Eclipse工程中引用這個(gè)jar包。
java代碼如下:
package com.arg;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import android.app.Activity;
import android.app.ProgressDialog;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.SimpleAdapter;
public class CallWebServiceActivity extends Activity {
//顯示結(jié)果的listview
ListView listView=null;
//輸入文本框
EditText provinceEdit=null;
//用于存放數(shù)據(jù)的集合list
List<Map<String, Object>> data=null;
//提示對(duì)話框
ProgressDialog myDialog=null;
//搜索按鈕
Button searchButton=null;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//獲得文本輸入框
provinceEdit=(EditText) this.findViewById(R.id.provinceEdit);
//獲得搜索按鈕
searchButton=(Button) this.findViewById(R.id.searchButton);
//為搜索按鈕添加單擊監(jiān)聽(tīng)事件
searchButton.setOnClickListener(new OnClickListener(){
public void onClick(View v) {
//響應(yīng)按鈕單擊事件的函數(shù)
ResponseOnClick();
}
});
}
//響應(yīng)按鈕單擊事件的函數(shù)
public void ResponseOnClick(){
//創(chuàng)建一個(gè)線程
HttpThread thread=new HttpThread(handler);
//構(gòu)造請(qǐng)求參數(shù)
HashMap <String ,Object> params=new HashMap<String ,Object>();
try{
CharSequence etValue=provinceEdit.getText();
String name="";
if(etValue!=null){
//字符轉(zhuǎn)碼
name=new String(etValue.toString().getBytes(),"UTF-8");
}
params.put("byProvinceName", name);
}catch(Exception ex){
ex.printStackTrace();
}
//
String url="http://webservice.webxml.com.cn/WebServices/WeatherWebService.asmx";
// String url = "http://webservice.webxml.com.cn/WebServices/WeatherWebService.asmx";
String nameSpace = "http://WebXml.com.cn/";
String methodName = "getSupportCity";
// 開(kāi)始新線程進(jìn)行WebService請(qǐng)求
thread.doStart(url, nameSpace, methodName, params);
}
/**
* 捕獲消息隊(duì)列
*
*/
Handler handler=new Handler(){
public void handleMessage(Message m){
ArrayList <String> myList=(ArrayList<String>)m.getData().getStringArrayList("data");
if(myList !=null){
if(data !=null){
data.clear();
}else{
data=new ArrayList<Map <String, Object>>();
}
for(int i=0;i<myList.size();i++){
Map<String, Object> item=new HashMap<String, Object>();
item.put("text", myList.get(i));
data.add(item);
}
/**
* 列表顯示
*
*/
SimpleAdapter simpleAdapter=new SimpleAdapter(CallWebServiceActivity.this
,data,R.layout.listlayout,new String[] {"text"},new int []{R.id.showData});
listView=(ListView) findViewById(R.id.showListView);
listView.setAdapter(simpleAdapter);
}
}
};
/**
* 線程類(lèi)
* @author Administrator
*
*/
public class HttpThread extends Thread{
private Handler handle=null;
String url=null;
String nameSpace=null;
String methodName=null;
HashMap <String ,Object> params=null;
ProgressDialog progressDialog=null;
//構(gòu)造函數(shù)
public HttpThread(Handler hander){
handle=hander;
}
/**
* 啟動(dòng)線程
*/
public void doStart(String url, String nameSpace, String methodName,
HashMap<String, Object> params) {
// TODO Auto-generated method stub
this.url=url;
this.nameSpace=nameSpace;
this.methodName=methodName;
this.params=params;
progressDialog=ProgressDialog.show(CallWebServiceActivity.this, "提示","正在請(qǐng)求請(qǐng)稍等……", true);
this.start();
}
/**
* 線程運(yùn)行
*/
@Override
public void run() {
// TODO Auto-generated method stub
System.out.println("jack");
super.run();
try{
//web service請(qǐng)求
SoapObject result=(SoapObject) CallWebService();
//構(gòu)造數(shù)據(jù)
ArrayList<String> list=null;
if(result !=null && result.getPropertyCount() > 0){
list=new ArrayList<String>();
for(int i=0;i<result.getPropertyCount();i++){
SoapPrimitive value=(SoapPrimitive) result.getProperty(i);
list.add(value.toString());
}
//a取消進(jìn)度對(duì)話框
progressDialog.dismiss();
//構(gòu)造消息
Message message=handle.obtainMessage();
Bundle b=new Bundle();
b.putStringArrayList("data", list);
message.setData(b);
handle.sendMessage(message);
}
}catch(Exception ex){
ex.printStackTrace();
}finally{
}
}
/**
* 請(qǐng)求web service
*/
protected Object CallWebService(){
String SOAP_ACTION = nameSpace + methodName;
//創(chuàng)建SoapObject實(shí)例
SoapObject request=new SoapObject(nameSpace,methodName);
//生成調(diào)用web service方法的soap請(qǐng)求消息
SoapSerializationEnvelope envelope=new SoapSerializationEnvelope(SoapEnvelope.VER11);
//設(shè)置.net web service
envelope.dotNet=true;
//發(fā)送請(qǐng)求
envelope.setOutputSoapObject(request);
//請(qǐng)求參數(shù)
if(params != null && !params.isEmpty() ){
for(Iterator it=params.entrySet().iterator();it.hasNext();){
Map.Entry e=(Entry) it.next();
request.addProperty(e.getKey().toString(),e.getValue());
}
}
//
AndroidHttpTransport androidHttpTrandsport=new AndroidHttpTransport(url);
SoapObject result=null;
try{
//web service請(qǐng)求
androidHttpTrandsport.call(SOAP_ACTION, envelope);
//得到返回結(jié)果
result=(SoapObject) envelope.getResponse();
}catch(Exception ex){
ex.printStackTrace();
}
return result;
}
}
}
希望本文所述對(duì)大家的Android程序設(shè)計(jì)有所幫助。
- 在Android中訪問(wèn)WebService接口的方法
- Android開(kāi)發(fā)調(diào)用WebService的方法示例
- Android 中利用 ksoap2 調(diào)用 WebService的示例代碼
- Android ksoap調(diào)用webservice批量上傳多張圖片詳解
- Android 通過(guò)webservice上傳多張圖片到指定服務(wù)器詳解
- Android通過(guò)ksoap2傳遞復(fù)雜數(shù)據(jù)類(lèi)型及CXF發(fā)布的webservice詳細(xì)介紹
- 在Android中調(diào)用WebService實(shí)例
- Android通過(guò)Webservice操作sqlserver數(shù)據(jù)庫(kù)實(shí)例代碼
- android中soap協(xié)議使用(ksoap調(diào)用webservice)
- android調(diào)用webservice接口獲取信息
相關(guān)文章
Android設(shè)置鬧鐘相對(duì)完善的解決方案
這篇文章主要為大家詳細(xì)介紹了Android設(shè)置鬧鐘相對(duì)完善的解決方案,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-06-06
Android開(kāi)發(fā)自學(xué)筆記(三):APP布局上
這篇文章主要介紹了Android開(kāi)發(fā)自學(xué)筆記(三):APP布局上,本文講解了添加ViewGroup、添加ViewGroup、定義string內(nèi)容、添加Button、運(yùn)行程序查看效果等內(nèi)容,需要的朋友可以參考下2015-04-04
基于GridView和ActivityGroup實(shí)現(xiàn)的TAB分頁(yè)(附源碼)
今天為大家介紹下使用GridView和ActivityGroup實(shí)現(xiàn)的分頁(yè),這里需要將Activity轉(zhuǎn)換成Window,然后再換成成View添加到容器中,具體實(shí)現(xiàn)代碼如下,感興趣的朋友可以參考下哈2013-06-06
Android Root設(shè)備中的su權(quán)限獲取和使用詳解
本篇文章主要介紹了Android Root設(shè)備中的su權(quán)限獲取和使用詳解,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2018-01-01
Android程序開(kāi)發(fā)之ListView 與PopupWindow實(shí)現(xiàn)從左向右滑動(dòng)刪除功能
這篇文章主要介紹了Android程序開(kāi)發(fā)之ListView 與PopupWindow實(shí)現(xiàn)滑動(dòng)刪除功能的相關(guān)資料,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下2016-07-07

