怎么在Android中調(diào)用WebService?針對這個問題,這篇文章詳細(xì)介紹了相對應(yīng)的分析和解答,希望可以幫助更多想解決這個問題的小伙伴找到更簡單易行的方法。
成都創(chuàng)新互聯(lián)公司專注于西藏網(wǎng)站建設(shè)服務(wù)及定制,我們擁有豐富的企業(yè)做網(wǎng)站經(jīng)驗。 熱誠為您提供西藏營銷型網(wǎng)站建設(shè),西藏網(wǎng)站制作、西藏網(wǎng)頁設(shè)計、西藏網(wǎng)站官網(wǎng)定制、微信小程序服務(wù),打造西藏網(wǎng)絡(luò)公司原創(chuàng)品牌,更為您提供西藏網(wǎng)站排名全網(wǎng)營銷落地服務(wù)。
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進行下載;將下載的ksoap2-android-assembly-2.4-jar-with-dependencies.jar包復(fù)制到Eclipse工程的lib目錄中,當(dāng)然也可以放在其他的目錄里。同時在Eclipse工程中引用這個jar包。
具體調(diào)用調(diào)用webservice的方法為:
(1) 指定webservice的命名空間和調(diào)用的方法名,如:
SoapObject request =new SoapObject(http://service,"getName");
SoapObject類的第一個參數(shù)表示W(wǎng)ebService的命名空間,可以從WSDL文檔中找到WebService的命名空間。第二個參數(shù)表示要調(diào)用的WebService方法名。
(2) 設(shè)置調(diào)用方法的參數(shù)值,如果沒有參數(shù),可以省略,設(shè)置方法的參數(shù)值的代碼如下:
Request.addProperty("param1","value"); Request.addProperty("param2","value");
要注意的是,addProperty方法的第1個參數(shù)雖然表示調(diào)用方法的參數(shù)名,但該參數(shù)值并不一定與服務(wù)端的WebService類中的方法參數(shù)名一致,只要設(shè)置參數(shù)的順序一致即可。
(3) 生成調(diào)用Webservice方法的SOAP請求信息。該信息由SoapSerializationEnvelope對象描述,代碼為:
SoapSerializationEnvelope envelope=new SoapSerializationEnvelope(SoapEnvelope.VER11); Envelope.bodyOut = request;
創(chuàng)建SoapSerializationEnvelope對象時需要通過SoapSerializationEnvelope類的構(gòu)造方法設(shè)置SOAP協(xié)議的版本號。該版本號需要根據(jù)服務(wù)端WebService的版本號設(shè)置。在創(chuàng)建SoapSerializationEnvelope對象后,不要忘了設(shè)置SOAPSoapSerializationEnvelope類的bodyOut屬性,該屬性的值就是在第一步創(chuàng)建的SoapObject對象。
(4) 創(chuàng)建HttpTransportsSE對象。通過HttpTransportsSE類的構(gòu)造方法可以指定WebService的WSDL文檔的URL:
HttpTransportSE ht=new HttpTransportSE("http://192.168.18.17:80 /axis2/service/SearchNewsService?wsdl");
(5)使用call方法調(diào)用WebService方法,代碼:
ht.call(null,envelope);
Call方法的第一個參數(shù)一般為null,第2個參數(shù)就是在第3步創(chuàng)建的SoapSerializationEnvelope對象。
(6)使用getResponse方法獲得WebService方法的返回結(jié)果,代碼:
SoapObject soapObject =( SoapObject) envelope.getResponse();
以下為簡單的實現(xiàn)一個天氣查看功能的例子:
public class WebService extends Activity { private static final String NAMESPACE = "http://WebXml.com.cn/"; // WebService地址 private static String URL = "http://www.webxml.com.cn/ webservices/weatherwebservice.asmx"; private static final String METHOD_NAME = "getWeatherbyCityName"; private static String SOAP_ACTION = "http://WebXml.com.cn/ getWeatherbyCityName"; private String weatherToday; private Button okButton; private SoapObject detail; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); okButton = (Button) findViewById(R.id.ok); okButton.setOnClickListener(new Button.OnClickListener() { public void onClick(View v) { showWeather(); } }); } private void showWeather() { String city = "武漢"; getWeather(city); } @SuppressWarnings("deprecation") public void 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(); } } private void 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風(fēng)力:" + detail.getProperty(7).toString() + "\n"; System.out.println("weatherToday is " + weatherToday); Toast.makeText(WebService.this, weatherToday, Toast.LENGTH_LONG).show(); } }
關(guān)于怎么在Android中調(diào)用WebService問題的解答就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,如果你還有很多疑惑沒有解開,可以關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道了解更多相關(guān)知識。
標(biāo)題名稱:怎么在Android中調(diào)用WebService
網(wǎng)頁地址:http://chinadenli.net/article14/jsejge.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供動態(tài)網(wǎng)站、網(wǎng)站改版、靜態(tài)網(wǎng)站、虛擬主機、移動網(wǎng)站建設(shè)、小程序開發(fā)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)