簡介:java
SoapObject : 指定webservice的命名空間和調用的方法名
SoapSerializationEnvelope :生成調用Webservice方法的SOAP請求信息
HttpTransportSE : 建立HttpTransportsSE對象。經過HttpTransportsSE類的構造方法能夠指定WebService的WSDL文檔的URL
使用call方法調用WebService方法
使用getResponse方法得到WebService方法的返回結果android
正文:web
首先下載KSOAP包:ksoap2-android-assembly-2.5.2-jar-with-dependencies.jar包 下載地址瀏覽器
而後新建android項目:並把下載的KSOAP包放在android項目的lib目錄下:右鍵->build path->configure build path--選擇Libraries,如圖:服務器
同時,只添加jar包肯能是不夠的,須要添加class folder,便可以再工程的libs文件夾中加入下載的KSOAP包,如圖:ide
環境配好以後能夠用下面七個步驟來調用WebService方法:網站
第一:實例化SoapObject對象,指定webService的命名空間(從相關WSDL文檔中能夠查看命名空間),以及調用方法名稱。如:ui
/命名空間 privatestatic final String serviceNameSpace="http://WebXml.com.cn/"; //調用方法(得到支持的城市) privatestatic final String getSupportCity="getSupportCity"; //實例化SoapObject對象 SoapObject request=new SoapObject(serviceNameSpace, getSupportCity);
第二步:假設方法有參數的話,設置調用方法參數:this
request.addProperty("參數名稱","參數值");
第三步:設置SOAP請求信息(參數部分爲SOAP協議版本號,與你要調用的webService中版本號一致):spa
//得到序列化的Envelope SoapSerializationEnvelope envelope=new SoapSerializationEnvelope(SoapEnvelope.VER11); envelope.bodyOut=request;
第四步:註冊Envelope:
(new MarshalBase64()).register(envelope);
第五步:構建傳輸對象,並指明WSDL文檔URL:
/請求URL privatestatic final String serviceURL="http://www.webxml.com.cn/webservices/weatherwebservice.asmx"; //Android傳輸對象 AndroidHttpTransport transport=new AndroidHttpTransport(serviceURL); transport.debug=true;
第六步:調用WebService(其中參數爲1:命名空間+方法名稱,2:Envelope對象):
transport.call(serviceNameSpace+getWeatherbyCityName, envelope);
第七步:解析返回數據:
if(envelope.getResponse()!=null){ return parse(envelope.bodyIn.toString()); }
------------------------------------------------------------------------------------------------------------------------------------------
例子:具體詳見http://blog.csdn.net/zd_1471278687/article/details/11925349
這裏有個地址提供webService天氣預報的服務網站,在瀏覽器中輸入網站:http://www.webxml.com.cn/webservices/weatherwebservice.asmx能夠看到該網站提供的
調用方法,點進去以後能夠看到調用時須要輸入的參數,固然有的不須要參數,例如:getSupportProvince ,而getSupportCity須要輸入查找的省份名,getWeatherbyCityName 須要輸入查找的城市名。接下來咱們就利用這三個接口得到數據,並作出顯示:
得到本天氣預報Web Service支持的洲,國內外省份和城市信息:
public class MainActivity extends Activity { // WSDL文檔中的命名空間 private static final String targetNameSpace = "http://WebXml.com.cn/"; // WSDL文檔中的URL private static final String WSDL = "http://webservice.webxml.com.cn/WebServices/WeatherWebService.asmx?wsdl"; // 須要調用的方法名(得到本天氣預報Web Services支持的洲、國內外省份和城市信息) private static final String getSupportProvince = "getSupportProvince"; private List<Map<String,String>> listItems; private ListView mListView; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); listItems = new ArrayList<Map<String,String>>(); mListView = (ListView) findViewById(R.id.province_list); new NetAsyncTask().execute(); mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() { @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { String mProvinceName = listItems.get(position).get("province"); Log.d("ProvinceName", mProvinceName); Intent intent = new Intent(); intent.putExtra("Pname", mProvinceName); intent.setClass(MainActivity.this, CityActivity.class); startActivity(intent); } }); } class NetAsyncTask extends AsyncTask<Object, Object, String> { @Override protected void onPostExecute(String result) { if (result.equals("success")) { //列表適配器 SimpleAdapter simpleAdapter = new SimpleAdapter(MainActivity.this, listItems, R.layout.province_item, new String[] {"province"}, new int[]{R.id.province}); mListView.setAdapter(simpleAdapter); } super.onPostExecute(result); } @Override protected String doInBackground(Object... params) { // 根據命名空間和方法獲得SoapObject對象 SoapObject soapObject = new SoapObject(targetNameSpace, getSupportProvince); // 經過SOAP1.1協議獲得envelop對象 SoapSerializationEnvelope envelop = new SoapSerializationEnvelope( SoapEnvelope.VER11); // 將soapObject對象設置爲envelop對象,傳出消息 envelop.dotNet = true; envelop.setOutputSoapObject(soapObject); // 或者envelop.bodyOut = soapObject; HttpTransportSE httpSE = new HttpTransportSE(WSDL); // 開始調用遠程方法 try { httpSE.call(targetNameSpace + getSupportProvince, envelop); // 獲得遠程方法返回的SOAP對象 SoapObject resultObj = (SoapObject) envelop.getResponse(); // 獲得服務器傳回的數據 int count = resultObj.getPropertyCount(); for (int i = 0; i < count; i++) { Map<String,String> listItem = new HashMap<String, String>(); listItem.put("province", resultObj.getProperty(i).toString()); listItems.add(listItem); } } catch (IOException e) { e.printStackTrace(); return "IOException"; } catch (XmlPullParserException e) { e.printStackTrace(); return "XmlPullParserException"; } return "success"; } } }