欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

android調(diào)用WebService實例分析

 更新時間:2015年10月12日 14:22:33   作者:天高空  
這篇文章主要介紹了android調(diào)用WebService的方法,以實例形式較為詳細(xì)的分析了WebService的調(diào)用原理與具體使用方法,具有一定參考借鑒價值,需要的朋友可以參考下

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

WebService是一種基于SOAP協(xié)議的遠(yuǎn)程調(diào)用標(biāo)準(zhǔn),通過webservice可以將不同操作系統(tǒng)平臺、不同語言、不同技術(shù)整合到一塊。在Android SDK中并沒有提供調(diào)用WebService的庫,因此,需要使用第三方的SDK來調(diào)用WebService。PC版本的WEbservice客戶端庫非常豐富,例如Axis2,CXF等,但這些開發(fā)包對于Android系統(tǒng)過于龐大,也未必很容易移植到Android系統(tǒng)中。因此,這些開發(fā)包并不是在我們的考慮范圍內(nèi)。適合手機的WebService客戶端的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)然也可以放在其他的目錄里。同時在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 {
  //顯示結(jié)果的listview 
  ListView listView=null; 
  //輸入文本框 
  EditText provinceEdit=null; 
  //用于存放數(shù)據(jù)的集合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);
  //為搜索按鈕添加單擊監(jiān)聽事件 
  searchButton.setOnClickListener(new OnClickListener(){
    public void onClick(View v) { 
     //響應(yīng)按鈕單擊事件的函數(shù) 
     ResponseOnClick(); 
    }
  });
 }
 //響應(yīng)按鈕單擊事件的函數(shù) 
 public void ResponseOnClick(){ 
  //創(chuàng)建一個線程 
  HttpThread thread=new HttpThread(handler); 
  //構(gòu)造請求參數(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"; 
  // 開始新線程進(jìn)行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; 
  //構(gòu)造函數(shù) 
  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(); 
    //構(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)度對話框 
     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{
   }
  }
  /** 
  * 請求web service 
  */ 
  protected Object CallWebService(){ 
   String SOAP_ACTION = nameSpace + methodName; 
   //創(chuàng)建SoapObject實例 
   SoapObject request=new SoapObject(nameSpace,methodName); 
   //生成調(diào)用web service方法的soap請求消息 
   SoapSerializationEnvelope envelope=new SoapSerializationEnvelope(SoapEnvelope.VER11); 
   //設(shè)置.net web service 
   envelope.dotNet=true; 
   //發(fā)送請求 
   envelope.setOutputSoapObject(request); 
   //請求參數(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請求
    androidHttpTrandsport.call(SOAP_ACTION, envelope); 
    //得到返回結(jié)果
    result=(SoapObject) envelope.getResponse();
   }catch(Exception ex){
    ex.printStackTrace();
   } 
   return result; 
  } 
 } 
}

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

相關(guān)文章

最新評論