ksoap2調用webservice

package com.fei.test;

import org.ksoap2.SoapEnvelope;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransportSE;

public class Test {
        public void getRemoteInfo(String phoneSec) {  
        // 命名空間  
        String nameSpace = "http://WebXml.com.cn/";  
        // 調用的方法名稱  
        String methodName = "getMobileCodeInfo";  
        // EndPoint  
        String endPoint = "http://webservice.webxml.com.cn/WebServices/MobileCodeWS.asmx";  
        // SOAP Action  
        String soapAction = "http://WebXml.com.cn/getMobileCodeInfo";  
  
        // 指定WebService的命名空間和調用的方法名  
        SoapObject rpc = new SoapObject(nameSpace, methodName);  
  
        // 設置需調用WebService接口需要傳入的兩個參數mobileCode、userId  
        rpc.addProperty("mobileCode", phoneSec);  
        rpc.addProperty("userId", "");  
  
        // 生成調用WebService方法的SOAP請求信息,並指定SOAP的版本  
        SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER10);  
  
        envelope.bodyOut = rpc;  
        // 設置是否調用的是dotNet開發的WebService  
        envelope.dotNet = true;  
        // 等價於envelope.bodyOut = rpc;  
        envelope.setOutputSoapObject(rpc);  
  
        HttpTransportSE transport = new HttpTransportSE(endPoint);  
        try {  
            // 調用WebService  
            transport.call(soapAction, envelope);  
        } catch (Exception e) {  
            e.printStackTrace();  
        }  
  
        // 獲取返回的數據  
        SoapObject object = (SoapObject) envelope.bodyIn;  
        // 獲取返回的結果  
        String result = object.getProperty(0).toString();  
  
        // 將WebService返回的結果顯示在TextView中  
        resultView.setText(result);  
    }  
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章