在Android中訪問WebService接口的方法
更新時間:2013年05月27日 10:29:33 作者:
最近公司有個項目需要從Android平臺訪問WebService接口,實現(xiàn)向發(fā)布的函數傳遞對象。在網上找了一些資料,發(fā)現(xiàn)使用ksoap2可以調用WebService傳遞對象。
需要引入ksoap2-android-assembly-2.5.2-jar-with-dependencies.jar
//WebService的命名空間
static final String namespace = "http://impl.service.suncreate.com";
//服務器發(fā)布的url
static final String url = http://10.100.3.41/axis2/services/UploadService;
final String methodName = "upload"; // 函數名
final int sessionID = "111111"; //sessionID
//創(chuàng)建HttpTransportSE對象,通過HttpTransportSE類的構造方法可以指定WebService的url
HttpTransportSE transport = new HttpTransportSE(url);
transport.debug = true;
//指定WebService的命名空間和函數名
SoapObject soapObject = new SoapObject(namespace, methodName);
//設置調用方法參數的值
soapObject.addProperty("sessionID", sessionID); //sessionID
soapObject.addProperty("data", cds); //cds是需要傳遞的對象
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER10);
envelope.bodyOut = transport;
envelope.setOutputSoapObject(soapObject);
//使用call方法調用WebService方法
transport.call(null, envelope);
SoapObject sb = (SoapObject) envelope.bodyIn;
String xmlMessage = sb.toString(); // 獲取從服務器端返回的XML字符串
復制代碼 代碼如下:
//WebService的命名空間
static final String namespace = "http://impl.service.suncreate.com";
//服務器發(fā)布的url
static final String url = http://10.100.3.41/axis2/services/UploadService;
final String methodName = "upload"; // 函數名
final int sessionID = "111111"; //sessionID
//創(chuàng)建HttpTransportSE對象,通過HttpTransportSE類的構造方法可以指定WebService的url
HttpTransportSE transport = new HttpTransportSE(url);
transport.debug = true;
//指定WebService的命名空間和函數名
SoapObject soapObject = new SoapObject(namespace, methodName);
//設置調用方法參數的值
soapObject.addProperty("sessionID", sessionID); //sessionID
soapObject.addProperty("data", cds); //cds是需要傳遞的對象
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER10);
envelope.bodyOut = transport;
envelope.setOutputSoapObject(soapObject);
//使用call方法調用WebService方法
transport.call(null, envelope);
SoapObject sb = (SoapObject) envelope.bodyIn;
String xmlMessage = sb.toString(); // 獲取從服務器端返回的XML字符串
您可能感興趣的文章:
- Android開發(fā)調用WebService的方法示例
- Android 中利用 ksoap2 調用 WebService的示例代碼
- Android ksoap調用webservice批量上傳多張圖片詳解
- Android 通過webservice上傳多張圖片到指定服務器詳解
- Android通過ksoap2傳遞復雜數據類型及CXF發(fā)布的webservice詳細介紹
- 在Android中調用WebService實例
- Android通過Webservice操作sqlserver數據庫實例代碼
- android調用WebService實例分析
- android中soap協(xié)議使用(ksoap調用webservice)
- android調用webservice接口獲取信息
相關文章
Android 添加系統(tǒng)設置屬性的實現(xiàn)及步驟
這篇文章主要介紹了Android 添加系統(tǒng)設置屬性的實現(xiàn)及步驟的相關資料,需要的朋友可以參考下2017-07-07
Android中使用ContentProvider管理系統(tǒng)資源的實例
這篇文章主要介紹了Android中使用ContentProvider管理系統(tǒng)資源的實例,講解了ContentProvider對系統(tǒng)中聯(lián)系人及多媒體資源的管理例子,需要的朋友可以參考下2016-04-04
Android Kotlin開發(fā)實例(Hello World!)及語法詳解
這篇文章主要介紹了Android Kotlin開發(fā)實例及語法詳解的相關資料,需要的朋友可以參考下2017-05-05

