使用Ksoap2調用Web Service加入SoapHeader,ksoap2soapheader

來源:互聯網
上載者:User

使用Ksoap2調用Web Service加入SoapHeader,ksoap2soapheader

關於這個問題,如果使用百度都是前篇一律的代碼,好不容易上了google才找到完整的方法,這裡講所有的代碼都貼出來與大家分享。

首先是.NET寫的後台代碼

/// <summary>    /// SoapHeader    /// </summary>    public class OwnSoapHeader:SoapHeader    {        public string UserName { get; set; }        public string Password { get; set; }    }/// <summary>    /// WebService1 的摘要說明    /// </summary>    [WebService(Namespace = "http://tempuri.org/")]    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]    [System.ComponentModel.ToolboxItem(false)]    // 若要允許使用 ASP.NET AJAX 從指令碼中調用此 Web 服務,請取消注釋以下行。     // [System.Web.Script.Services.ScriptService]    public class WebService1 : System.Web.Services.WebService    {        public OwnSoapHeader OwnSoapHeader;        [WebMethod]        [SoapHeader("OwnSoapHeader")]        public string Login()        {            if (OwnSoapHeader != null)            {                return OwnSoapHeader.UserName + "," + OwnSoapHeader.Password;            }            return "尚未登入";        }    }

接下來是在瀏覽器中瀏覽查看我的介面,這裡面有很重要的資訊,包含了SoapHeader的名字以及參數

隨後就是使用Ksoap2調用我的介面了

private void DoLogin() {        new Thread() {            @Override            public void run() {                Looper.prepare();                // TODO Auto-generated method stub                String sNameSpace = "http://tempuri.org/";                String sMethodName = "Login";                String sActionString = "http://tempuri.org/Login";                String sURL = "http://192.168.1.101:8088/WebService1.asmx";                SoapObject rpc = new SoapObject(sNameSpace, sMethodName);          //在這裡加入了SoapHeader                Element[] header = new Element[1];
        //OwnSoapHeader與紅色標記處名字一致 header[0] = new Element().createElement(sNameSpace, "OwnSoapHeader ");
          //UserName紅色標記處名字一致 Element username = new Element().createElement(sNameSpace, "UserName"); username.addChild(Node.TEXT, "admin"); header[0].addChild(Node.ELEMENT, username);
          //Password紅色標記處名字一致 Element pass = new Element().createElement(sNameSpace, "Password"); pass.addChild(Node.TEXT, "123"); header[0].addChild(Node.ELEMENT, pass); SoapSerializationEnvelope envelope = new SoapSerializationEnvelope( SoapEnvelope.VER12); envelope.headerOut = header; envelope.bodyOut = rpc; envelope.dotNet = true; envelope.setOutputSoapObject(rpc); HttpTransportSE ht = new HttpTransportSE(sURL); SoapObject soapObject = null; try { ht.call(sActionString, envelope); soapObject = (SoapObject) envelope.bodyIn; } catch (IOException ex) { // TODO Auto-generated catch block ex.printStackTrace(); } catch (XmlPullParserException ex) { // TODO Auto-generated catch block ex.printStackTrace(); } Bundle result = new Bundle(); if (soapObject != null) { result.putString("result", soapObject.toString()); } else { result.putString("result", sURL); } Message msg = new Message(); msg.setData(result); msg.what = 1; handler.handleMessage(msg); Looper.loop(); } }.start(); } Handler handler = new Handler() { @Override public void handleMessage(Message msg) { // TODO Auto-generated method stub if (msg.what == 1) { Bundle result = msg.getData(); String text = result.getString("result"); Toast.makeText(MainActivity.this, text, Toast.LENGTH_LONG) .show(); } } };

 至此已經全部OK了,能夠接收到服務端回傳的使用者資料

 


Android 使用KSOAP2調用WebService

android 利用ksoap2方式串連webservice(2010-04-16 16:36:25)轉載標籤:androidksoap2webserviceit 分類:Android
利用J2SE的ksoap2標準,我也來做一個山寨版本的android串連webservice。因為soap封裝的關係,android application在接收到資料後不能夠正確的按照J2SE的標準來擷取。

在運用之前,我們先要引導兩個jar進入工程的buildpath

這兩個jar包都可以在網上查到下載,引導完後再做一項準備工作。弄清楚發行的webservice的地址,以及封裝的方式。比如:

webservice介面: 192.168.0.2:8080/axis2/services/Manager?wsdl (順便說明一下,在android當中,不能寫localhost,必須寫清楚PC機當前的網路IP)
webservice封裝: ws.apache.org/axis2

都瞭解了過後,說明已經做好準備了。
下面就介紹一下android如何擷取webservice封裝資料。。

引入ksoap2中以封裝好的類
import org.ksoap2.SoapEnvelope;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.AndroidHttpTransport;

在類中定義webservice的介面地址以及解析方式並且定義要調用的webservice中的函數
private static final String URL = " 192.168.0.2:8080/axis2/services/Manager?wsdl";
private static final String NAMESPACE = " ws.apache.org/axis2";
private static final String METHOD_NAME = "GetMyFriends";

這個資訊我們可以在webservice中查到
<xs:element name="Ge......餘下全文>>
 
android使用ksoap2調用net 的webservice

我用過一些包是本身就有錯誤的 重新下一個吧 到官網最好
 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.