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
具體調用調用webservice的方法爲:android
(1) 指定webservice的命名空間和調用的方法名,如:web
SoapObject request =new SoapObject(http://service,」getName」);
SoapObject類的第一個參數表示WebService的命名空間,能夠從WSDL文檔中找到WebService的命名空間。第二個參數表示要調用的WebService方法名。ide
(2) 設置調用方法的參數值,若是沒有參數,能夠省略,設置方法的參數值的代碼以下:this
Request.addProperty(「param1」,」value」); Request.addProperty(「param2」,」value」);
要注意的是,addProperty方法的第1個參數雖然表示調用方法的參數名,但該參數值並不必定與服務端的WebService類中的方法參數名一致,只要設置參數的順序一致便可。google
(3) 生成調用Webservice方法的SOAP請求信息。該信息由SoapSerializationEnvelope對象描述,代碼爲:操作系統
SoapSerializationEnvelope envelope=new SoapSerializationEnvelope(SoapEnvelope.VER11); Envelope.bodyOut = request;
建立SoapSerializationEnvelope對象時須要經過SoapSerializationEnvelope類的構造方法設置 SOAP協議的版本號。該版本號須要根據服務端WebService的版本號設置。在建立SoapSerializationEnvelope對象後,不 要忘了設置SOAPSoapSerializationEnvelope類的bodyOut屬性,該屬性的值就是在第一步建立的SoapObject對 象。debug
(4) 建立HttpTransportsSE對象。經過HttpTransportsSE類的構造方法能夠指定WebService的WSDL文檔的URL:code
HttpTransportSE ht=new HttpTransportSE(「http://192.168.18.17:80 /axis2/service/SearchNewsService?wsdl」);
(5)使用call方法調用WebService方法,代碼:xml
ht.call(null,envelope);
Call方法的第一個參數通常爲null,第2個參數就是在第3步建立的SoapSerializationEnvelope對象。
(6)使用getResponse方法得到WebService方法的返回結果,代碼:
SoapObject soapObject =( SoapObject) envelope.getResponse();
如下爲簡單的實現一個天氣查看功能的例子:
publicclass WebService extends Activity { privatestaticfinal String NAMESPACE ="http://WebXml.com.cn/"; // WebService地址 privatestatic String URL ="http://www.webxml.com.cn/ webservices/weatherwebservice.asmx"; privatestaticfinal String METHOD_NAME ="getWeatherbyCityName"; privatestatic String SOAP_ACTION ="http://WebXml.com.cn/ getWeatherbyCityName"; private String weatherToday; private Button okButton; private SoapObject detail; @Override publicvoid onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); okButton = (Button) findViewById(R.id.ok); okButton.setOnClickListener(new Button.OnClickListener() { publicvoid onClick(View v) { showWeather(); } }); } privatevoid showWeather() { String city ="武漢"; getWeather(city); } @SuppressWarnings("deprecation") publicvoid getWeather(String cityName) { try { System.out.println("rpc------"); SoapObject rpc =new SoapObject(NAMESPACE, METHOD_NAME); System.out.println("rpc"+ rpc); System.out.println("cityName is "+ cityName); rpc.addProperty("theCityName", cityName); AndroidHttpTransport ht =new AndroidHttpTransport(URL); ht.debug =true; SoapSerializationEnvelope envelope =new SoapSerializationEnvelope( SoapEnvelope.VER11); envelope.bodyOut = rpc; envelope.dotNet =true; envelope.setOutputSoapObject(rpc); ht.call(SOAP_ACTION, envelope); SoapObject result = (SoapObject) envelope.bodyIn; detail = (SoapObject) result .getProperty("getWeatherbyCityNameResult"); System.out.println("result"+ result); System.out.println("detail"+ detail); Toast.makeText(WebService.this, detail.toString(), Toast.LENGTH_LONG).show(); parseWeather(detail); return; } catch (Exception e) { e.printStackTrace(); } } privatevoid parseWeather(SoapObject detail) throws UnsupportedEncodingException { String date = detail.getProperty(6).toString(); weatherToday ="今天:"+ date.split("")[0]; weatherToday = weatherToday +"\n天氣:"+ date.split("")[1]; weatherToday = weatherToday +"\n氣溫:" + detail.getProperty(5).toString(); weatherToday = weatherToday +"\n風力:" + detail.getProperty(7).toString() +"\n"; System.out.println("weatherToday is "+ weatherToday); Toast.makeText(WebService.this, weatherToday, Toast.LENGTH_LONG).show(); } }