国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 系統 > Android > 正文

android調用WebService實例分析

2020-04-11 11:23:49
字體:
來源:轉載
供稿:網友

本文實例講述了android調用WebService的方法。分享給大家供大家參考。具體如下:

WebService是一種基于SOAP協議的遠程調用標準,通過webservice可以將不同操作系統平臺、不同語言、不同技術整合到一塊。在Android SDK中并沒有提供調用WebService的庫,因此,需要使用第三方的SDK來調用WebService。PC版本的WEbservice客戶端庫非常豐富,例如Axis2,CXF等,但這些開發包對于Android系統過于龐大,也未必很容易移植到Android系統中。因此,這些開發包并不是在我們的考慮范圍內。適合手機的WebService客戶端的SDK有一些,比較常用的有Ksoap2,可以從http://code.google.com/p/ksoap2-android/downloads/list進行下載;將下載的ksoap2-android-assembly-2.4-jar-with-dependencies.jar包復制到Eclipse工程的lib目錄中,當然也可以放在其他的目錄里。同時在Eclipse工程中引用這個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 {  //顯示結果的listview   ListView listView=null;   //輸入文本框   EditText provinceEdit=null;   //用于存放數據的集合list   List<Map<String, Object>> data=null;   //提示對話框   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);  //為搜索按鈕添加單擊監聽事件   searchButton.setOnClickListener(new OnClickListener(){    public void onClick(View v) {      //響應按鈕單擊事件的函數      ResponseOnClick();     }  }); } //響應按鈕單擊事件的函數  public void ResponseOnClick(){   //創建一個線程   HttpThread thread=new HttpThread(handler);   //構造請求參數   HashMap <String ,Object> params=new HashMap<String ,Object>();   try{    CharSequence etValue=provinceEdit.getText();    String name="";    if(etValue!=null){     //字符轉碼     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";   // 開始新線程進行WebService請求   thread.doStart(url, nameSpace, methodName, params);  }  /**  * 捕獲消息隊列  *  */  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);    }   }  };  /**  * 線程類  * @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;   //構造函數   public HttpThread(Handler hander){    handle=hander;   }   /**   * 啟動線程   */   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, "提示","正在請求請稍等……", true);    this.start();    }   /**   * 線程運行   */   @Override   public void run() {    // TODO Auto-generated method stub    System.out.println("jack");    super.run();    try{     //web service請求     SoapObject result=(SoapObject) CallWebService();     //構造數據     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取消進度對話框      progressDialog.dismiss();      //構造消息      Message message=handle.obtainMessage();      Bundle b=new Bundle();      b.putStringArrayList("data", list);     message.setData(b);      handle.sendMessage(message);     }   }catch(Exception ex){    ex.printStackTrace();   }finally{   }  }  /**   * 請求web service   */   protected Object CallWebService(){    String SOAP_ACTION = nameSpace + methodName;    //創建SoapObject實例    SoapObject request=new SoapObject(nameSpace,methodName);    //生成調用web service方法的soap請求消息    SoapSerializationEnvelope envelope=new SoapSerializationEnvelope(SoapEnvelope.VER11);    //設置.net web service    envelope.dotNet=true;    //發送請求    envelope.setOutputSoapObject(request);    //請求參數    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請求    androidHttpTrandsport.call(SOAP_ACTION, envelope);     //得到返回結果    result=(SoapObject) envelope.getResponse();   }catch(Exception ex){    ex.printStackTrace();   }    return result;   }  } }

希望本文所述對大家的Android程序設計有所幫助。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 浏阳市| 米脂县| 鲁甸县| 崇仁县| 寿宁县| 醴陵市| 奈曼旗| 杭锦后旗| 偏关县| 山丹县| 闸北区| 镇江市| 田东县| 清镇市| 环江| 海淀区| 南召县| 修文县| 德惠市| 互助| 随州市| 巴林右旗| 浮梁县| 贞丰县| 英吉沙县| 澄迈县| 墨脱县| 托里县| 灌云县| 邵阳县| 江西省| 黑龙江省| 新余市| 色达县| 荣昌县| 上犹县| 三原县| 湾仔区| 黄石市| 阳山县| 革吉县|