2013-04-10 100 views
0

我的隊友在Asp.NET中開發了一個應用程序,並在其中編寫了Web服務。我的主管告訴我從Android調用這些服務。我在Google上進行了大量搜索,但我無法理解如何從Android調用Web服務。如何在android中調用web服務

Asp.Net Web服務是:

「GenerateTreeFromXMLWebService.asmx」 Web服務類

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Web; 
using System.Web.Services; 
using System.IO; 
using System.Xml.Linq; 

namespace UnitTestWebApp 
{ 
    /// <summary> 
    /// Summary description for GenerateTreeFromXMLWebService 
    /// </summary> 
    [WebService(Namespace = "http://tempuri.org/")] 
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)] 
    [System.ComponentModel.ToolboxItem(false)] 
    // To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line. 
    // [System.Web.Script.Services.ScriptService] 
    public class GenerateTreeFromXMLWebService : System.Web.Services.WebService 
    { 
     Node node = new Node(); 

     XDocument xDoc = new XDocument(); 
     string xml; //to save the xml data value 

     // this method is for reading xml from file and storing it in a string 
     public GenerateTreeFromXMLWebService() 
     { 
      xml = File.ReadAllText("E:\\Unit Testing\\AdWordsTestsnew.xml"); 
      xDoc = XDocument.Parse(xml); 
     } 

     [WebMethod] 
     public string HelloWorld() 
     { 
      return "Hello World"; 
     } 
     //this method is used to separate parent nodes with their child from xml file 
     [WebMethod] 
     public Node generateXMLTree() 
     { 

      XElement rootElement = xDoc.Element("Node"); 



      node.ResultName = rootElement.Attribute("result").Value; 
      node.MethodName = rootElement.Attribute("name").Value; 

      if (rootElement.HasElements) 
       generateChild(node, rootElement); 

      return node; 

     } 
     //this method is used for making child nodes 
     void generateChild(Node node, XElement rootElement) 
     { 

      foreach (XElement a in rootElement.Elements("Node")) 
      { 
       Node K = new Node(); 
       K.ResultName = a.Attribute("result").Value; 
       K.MethodName = a.Attribute("name").Value; 

       node.AddChild(K); 
       //this recursion is for making inner child node of the child functions 
       generateChild(K, a); 
      } 
     } 

    } 
} 

Service.cs類:

namespace GenerateTree 
{ 
    // NOTE: You can use the "Rename" command on the "Refactor" menu to change the interface name "IService1" in both code and config file together. 
    [ServiceContract] 
    public interface IService1 
    { 
     [OperationContract] 
     string GetData(int value); 

     [OperationContract] 
     CompositeType GetDataUsingDataContract(CompositeType composite); 

     // TODO: Add your service operations here 
    } 

    // Use a data contract as illustrated in the sample below to add composite types to service operations 
    [DataContract] 
    public class CompositeType 
    { 
     bool boolValue = true; 
     string stringValue = "Hello "; 

     [DataMember] 
     public bool BoolValue 
     { 
      get { return boolValue; } 
      set { boolValue = value; } 
     } 

     [DataMember] 
     public string StringValue 
     { 
      get { return stringValue; } 
      set { stringValue = value; } 
     } 
    } 
} 

我怎麼能調用這些服務?任何教程鏈接將不勝感激。

+0

在發佈問題之前,您必須先挖掘一些文檔,是嗎? – RobinHood 2013-04-10 07:10:42

+0

是的,我有。 http://www.codeproject.com/Articles/112381/Step-by-Step-Method-to-Access-Webservice-from-Andr http://www.c-sharpcorner.com/UploadFile/88b6e5/how在android中使用soap/ 和其他文件,但不知道該怎麼做。 – Alven 2013-04-10 07:17:24

回答