ANDROID調用webservice帶soapheader驗證

來源:互聯網
上載者:User

標籤:

最近的一個項目中調用webservice介面,需要驗證soapheader,現將解決方案記錄如下:(網上資料出處太多,就不做引用,原作者如看到,如有必要添加請通知)

1、先看介面

POST /webserver/ValideWebService.asmx HTTP/1.1Host: IP地址Content-Type: text/xml; charset=utf-8Content-Length: lengthSOAPAction: "http://命名空間/Login"<?xml version="1.0" encoding="utf-8"?><soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">  <soap:Header>    <MySoapHeader xmlns="http://命名空間/">      <ProjectID>string</ProjectID>    </MySoapHeader>  </soap:Header>  <soap:Body>    <Login xmlns="http://命名空間/">      <loginName>string</loginName>      <passowrd>string</passowrd>    </Login>  </soap:Body></soap:Envelope>

 

驗證時需要驗證header和body兩部分,需要引入第三方jar包,ksoap2-android-assembly-2.5.2-jar-with-dependencies.jar。下面就是我驗證使用的方法,網上有許多,只不過無法驗證,“拿來”修改一下,做個記錄,供以後查看,也方便大家參閱。

先聲明以下;//命名空間    private static final String NAMESPACE = "http://命名空間/";    //服務地址    private static String URL = "http://IP地址或者網域名稱/webserver/ValideWebService.asmx";    //調用的方法名    private static final String METHOD_NAME = "Login";    //此處是命名空間+方法名    private static String SOAP_ACTION = "http://命名空間/Login";    private SoapObject  detail;

由於2.3以上無法在主線程中直接存取網路,所以在需要的地方開啟一個子線程,這裡我在點擊按鈕登入的時候需要,因此寫在onclick()方法下:

new Thread() {                @Override                public void run() {                    // TODO Auto-generated method stub                      super.run();                    try {                        SoapObject rpc = new SoapObject(NAMESPACE, METHOD_NAME);                        //此處2個propertyinfo,是Login方法所需的參數,代碼下面貼出asmx代碼                        PropertyInfo pi = new PropertyInfo();                        pi.setName("loginName");                        pi.setValue(cardNumStr);                        rpc.addProperty(pi);                        pi=new PropertyInfo();                        pi.setName("passowrd");                        pi.setValue(passwordStr);                        rpc.addProperty(pi);                        //soapheader在這裡                        Element[] header = new Element[1];                        header[0] = new Element().createElement(NAMESPACE, "MySoapHeader");                        Element username = new Element().createElement(NAMESPACE, "ProjectID");                        username.addChild(Node.TEXT, "這裡是值");                        header[0].addChild(Node.ELEMENT, username);                        SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);                        envelope.headerOut = header;                        envelope.bodyOut = rpc;                        envelope.dotNet = true;                        envelope.setOutputSoapObject(rpc);                        HttpTransportSE ht = new HttpTransportSE(URL);                        ht.call(SOAP_ACTION, envelope);                        //SoapObject                        detail =(SoapObject) envelope.getResponse();                        System.out.println("返回的結果"+ detail.toString());                    }catch (Exception e){                        System.out.println("錯誤訊息:"+ e.getMessage());                    }                    Message msg = handler.obtainMessage();                    msg.obj=detail;                    handler.sendMessage(msg);                }            }.start();

上面的cardNumStr和passwordStr是我從文本輸入框擷取的值。訪問網路從介面通過驗證然後獲得傳回值,對返回的資料進行處理就可以了。

用SoapObject,要不返回的detail為null。

 

private Handler handler = new Handler() {        public void handleMessage(Message msg) {            //這裡做你的UI處理           };    };

 

ANDROID調用webservice帶soapheader驗證

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.