Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> 關於Android編程 >> Speaking SOAP with Android——android的soap通信

Speaking SOAP with Android——android的soap通信

編輯:關於Android編程

譯文

段落 原文 譯文 Part1-phrase 1 RESTful web services are currently all the rage. They’re the new black. Everybody’s doing it. Facebook, Twitter, and Amazon all have RESTful services. And if you’ve coded against one of these trendy APIs, you’ll probably turn your nose up at the mere mention of something like SOAP. “That’s SOOO last decade.” Perhaps. But the fact remains that there are a lot of web services still using SOAP technologies. And if you’re a mobile developer working on web service clients, you’re bound to bump into a SOAP server at some point. RESTful架構的web service最近風靡一時。它是新的黑人,每一個人都在做這件事情。臉書,推特,亞馬遜都有RESETful架構的服務。如果對上述流行的API進行編碼,一提到SOAP可能就會對它嗤之以鼻。“那都是上個年代的事情啦。”也許吧。但是事實是,現在還有很多的web service還是在使用SOAP技術。如果你是移動終端的開發者,你必定會在某個時候遇到SOAP服務器。 phrase 2 Unfortunately for Android developers, the current platform SDK provides no built-in support for SOAP. The developer is left to either roll their own solution or search for something from a third party. And as fun as writing your own SOAP library sounds (not), coding and testing takes times. So a custom SOAP solution might not even be practical. As luck would have it, though, there’s a third party SOAP library for Android that’s mature, flexible, open-source, and best of all, completely free. It’s called ksoap2-android. 不幸的是,對於Android開發者來說,目前的SDK開發平台並不提供內置支持SAOP的開發。 開發者要麼自己尋求解決方案,要麼尋找第三方平台的幫助。編寫自己的SOAP庫聽起來那麼有趣(並不是=_=[原作者用詞诙諧]),編碼和測試需要花費時間。所以自定義SOAP解決方案並不具有可操作性。但是,有一個第三方android Soap庫,並且是靈活的、開源的,最重要的是完全免費的。它叫Ksoap2-android。 Part2 Introducing ksoap2-android 介紹ksoap2-android庫 phrase 1 ksoap2-android is a quite a mouthful. It wasn’t always called ksoap2-android, though. And it wasn’t always an Android library. ksoap2-android started out as simply kSOAP way back in 2001. kSOAP targeted embedded J2ME systems. So it was designed with resource constrained environments in mind. After a number of small releases and a major refactor, it was then dubbed kSOAP2 and subsequently started dying on the vine. The maintainers stopped maintaining it. New releases grounded to a halt. ksoap2-android名字相當拗口。但是它並不是一直都叫這個名字的。而且它也不是一直都是作為android庫文件而存在的。早在2001年,ksoap-android值作為kSOAP的方式出現。kSOAP瞄准的是嵌入式J2ME系統。所以它的設計理念是服務於有限資源的環境。在一些小版本和一個主要的重構發布之後,在那時被稱為kSOAP2,但是隨後就開始衰落了。維護人員也停止維護了。新版本的發布也慢慢停止。 phrase 3 Fast forward to 2007. An Android developer by the name of Jorge Jimenez published an Android-specific set of patches for kSOAP2 to the “Android Developers” Google Group. Not long after ksoap2-android was born. A new home was created for the fork (http://code.google.com/p/ksoap2-android/). A new mailing list and wiki were erected. And ksoap2-android has since become THE library for SOAP on Android. 快進到2007年。一個名叫Jorge Jimenez(豪爾赫·吉梅內斯)的android開發人員在谷歌android開發小組的論壇發布了針對android的kSOAP2補丁。不久之後ksoap2-android就誕生了。一個新家園為了這些人而創建http://code.google.com/p/ksoap2-android/ [指一個新的網上論壇]一個新的郵件列表和維基百科被創建。從此ksoap2-android就成了android的SOAP庫。 phrase 3 As of this writing, the latest stable release of ksoap2-android is 2.6.0. 截止到寫這篇文章的是最近的一版文檔版本是ksoap2-android2.6.0 Part3 Getting Started with ksoap2-android 開始使用ksoap2-android phrase 1 This article is meant to be an introduction to ksoap2-android and not SOAP itself. If you’re not familiar with the inner-workings of SOAP, I’ll give a brief overview of it below. But I strongly advise you to bookmark this article and come back to it after having dived into a meatier tutorial on the subject. 這篇文章是介紹ksoap2-android而不是SOAP本身。如果你不了解SOAP內部的工作機制,我會在下面給出一個簡介。但是我強烈地建議你收藏這篇文章,快速進行一個實際的教程以後再回來看。 phrase 2 You can obtain the ksoap2-android library one of two ways. If you prefer to build from source, you can find source download and build instructions here.http://code.google.com/p/ksoap2-android/wiki/SourceCodeHosting 你可以通過兩種方式獲得ksoap2-android庫文件。如果你喜歡從源碼構建,你可以在這裡下載源碼並且構建指令http://code.google.com/p/ksoap2-android/wiki/SourceCodeHosting phrase 3 But the easiest way to get ksoap2-android is to fetch the latest pre-built JAR file. Instructions and links for downloading the JAR can be found here.http://code.google.com/p/ksoap2-android/wiki/HowToUse?tm=2 但是最簡單的得到ksoap2-android的方式是拿到最新的已經預先編譯過的jar文件。下面這個鏈接可以拿到jar文件和指令。http://code.google.com/p/ksoap2-android/wiki/HowToUse?tm=2 Part4 A SOAP Primer SOAP入門 phrase 1 SOAP clients and servers communicate with each other using SOAP messages over some network transport protocol, which is usually HTTP. A SOAP message is merely a blob of XML and it can represent either a request or a response. SOAP客戶端和服務器在網絡傳輸協議的基礎上使用SOAP小心進行通信,通常是HTTP協議。SOAP消息只是一個XML文件,它可以代表一個請求或者是一個響應。 phrase 2 Here’s a small example that illustrates what a typical SOAP message might look like. Note that this is a contrived example and doesn’t represent a real service request. 下面這個簡單例子描述了一個典型的SOAP消息的形式。需要注意的是,這個是一個人為的例子,並不代表實際的服務請求。 例子    


     
     
     
          
               0-374-15012-5
          
     

段落 原文 譯文 phrase 3 The structure of a SOAP message is rigidly defined and is comprised of three major pieces. SOAP消息結構嚴格定義並且由3個部分組成。 phrase 3-1 ·The first piece is the SOAP envelope. This is essentially the container for all of the other SOAP data. The XML element name is Envelope and it’s always the root element. 第一部分是SOAP信封(Envelop,特定稱謂)。這基本上是所有其他的SOAP數據的容器。XML元素名稱是信封,它是根元素。 phrase 3-2 The second piece of a SOAP message is the SOAP header. This contains application-specific metadata relating to the request, such as encoding style, authentication information, etc. It’s considered optional and, in my experience, isn’t used all that often. The XML element name is Header and it’s a direct child of the Envelope element. 第二部分是SOAP的消息頭部,它包含了特定應用程序的與請求有關的元數據的格式,如編碼風格,身份驗證信息等等。它是可選的,根據我的經驗並不經常使用這一部分。XML元素名是Header,它是Envelope元素的直接子元素。 phrase 3-3 The final, and most significant, part of a SOAP message is the SOAP body. This contains the actual message request or response. The content of the SOAP body is application dependent and can be anything as long as its well-formed XML. The XML element name is Body and it’s also a direct child of the Envelope element. The data types used in the SOAP body are often defined in a service type definition such as a WSDL or WADL file. 最後也是最重要的是SOAP消息的主體部分。它包含了實際的請求和返回值的信息。SOAP的主體是依賴於應用程序的,可以是任何東西,只要是一個格式良好的XML文件。XML元素名是 Body,它也是Envelope元素的直接子元素。。在SOAP主體中使用的數據格式通常在諸如WSDL或者WADL文件一類的service格式定義文件中被定義。 phrase 4 One other SOAP element bears mentioning and that’s the SOAP Fault. You can only ever have one SOAP Fault and it appears in the SOAP body. SOAP Faults are used to indicate error conditions. If, for example, you’re trying to communicate with a web service that doesn’t understand the version of SOAP you’re using, it’ll respond with a SOAP Fault that indicates such. The XML element name is Fault. 另一個SOAP元素值得一提——SOAP故障。你只能有一個SOAP錯誤,它出現在SOAP主體中。SOAP錯誤用來指示錯誤條件。舉例來說,如果你想跟一個web service通信,但是它不理解你使用的SOAP的版本號,就會返回一個提示錯誤信息的SOAP錯誤。XML元素名是Fault。 phrase 5 For more information on SOAP, poke around on Google or check out one of the dozens of books on SOAP web services. 如果你想得到更多的SOAP信息,戳一下谷歌,或者查閱有關SOAP 網絡通信的書籍。 Part5 Speaking SOAP using ksoap2-android 說一說ksoap2-android的使用 phrase 1 If you’ve worked with other web service SDKs (e.g., .NET), you’ve probably used tools that can generate language type definitions from a web service definition file (e.g. WSDL). These kinds of tools abstract away a lot of the “SOAPiness” of web service communications. They keep your application code simple and easy to maintain. I’m sorry to tell you that, at the time of this writing, no such tool for ksoap2-android exists. You’ll have to get a bit more up close and personal with whatever service description your web service provides. 如果你用其他的web service SDK工作(比如.NET),你可能會使用一些,能夠從web service定義文件(如WSDL)中生成語言類型定義。這些各種各樣的工具抽象描述了網絡通信服務的很多“SOAPiness”[指soap(原意是肥皂)生成了很多soapiness(皂滑性),可以理解為增加了原wsdl文件的可操作性]。他們使得你的程序代碼變得簡單而且容易維護。很遺憾,在寫這篇文章的時候,針對ksoap2-android的解釋工具並不存在。你必須近距離接觸你的web service提供的各種服務描述文件。 phrase 2 Of the dozens of classes defined in ksoap2-android, there are only five that you’ll typically use – SoapPrimitive, SoapObject, SoapSerializationEnvelope, HttpTransportSE, and SoapFault. An understanding of these classes is all you need to perform a very simple SOAP request. I’ll discuss each one in turn and show you how you could invoke the “FindBookByISBN” SOAP request in the example above. ksoap2-android中的幾十個類定義,只有5個你經常會用到SoapPrimitive, SoapObject, SoapSerializationEnvelope, HttpTransportSE,和 SoapFault. 為了執行一個簡單的SOAP請求,你需要理解這些類。我會以此討論每一個並向你展示如何調用上述例子中的“FindBookByISBN”SOAP請求。 phrase 3-1 SoapPrimitive     A SoapPrimitive is just what it sounds like. It represents a serialized representation of a primitive data type in SOAP (floats, ints, doubles, strings, etc.) A SoapPrimitive doesn’t actually contain any type information, though. Everything is stored as strings. It’s up to the application developer to interpret and parse the contained data as appropriate. SoapPrimitive顧名思義,它代表一個SOAP中序列化的原始數據類(浮點數、整數、雙精度浮點數、字符串等等)。但是SoapPrimitive並不包含實際意義上任何類型的信息。任何東西都是存儲成字符串類型的。由程序員解釋和解析裡面所包含的數據。 phrase 3-1-1 SoapPrimitive’s constructor looks like this. SoapPrimitive的結構類型如下:
public SoapPrimitive(String namespace, String name, String value);

段落 原文 譯文 phrase 3-1-2 You usually won’t instantiate this yourself. In general, SOAP requests are more complex and require the use of the SoapObject, which I’ll discuss next. But it’s not uncommon to receive this as a response. 你通常不會實例化這個類,一般來說SOAP請求更加復雜,需要使用SoapObject,這個我接下來將會介紹 。而且將常收到這一類的回復。 phrase 3-2 SoapObject   phrase 3-2-1 The SoapObject represents a complex type that’s defined for a given web service. These types are hierarchal data types composed of other data types and live in an application-specific namespace. The FindBookByISBN element used above is an example of a complex type. SoapObject類表示一個給定的web service定義的復雜類型。這些數據類型是由層次結構的數據類型組成的其他數據類型並且活躍在一個由特定應用程序指定的命名空間中。上述例子中使用的FindBookByISBN元素是復雜數據類型的一個例子。

   0-374-15012-5

段落 原文 譯文 phrase 3-2-2 SoapObject’s constructor looks like this. SoapObject類的數據結構如下:
public SoapObject(String namespace, String name);

段落 原文 譯文 phrase 3-2-3 You can instantiate a SoapObject by passing in the object’s namespace and name into the constructor 你可以通過構造函數傳入對象的命名空間和名字來實例化一個SoapObject。
SoapObject findBookRequest = new SoapObject(
    "http://services.example.com", "FindBookByISBN");

段落 原文 譯文 phrase 3-2-4 The FindBookByISBN method has a single parameter, ISBN. ISBN is a simple string. You might expect that you could simply wrap the ISBN inside of a SoapPrimitive and somehow add it into findBookRequest. That would be a perfectly reasonable approach. For some reason, however, SoapObject isn’t able to serve as a container for SoapPrimitives. Even more surprising is that it isn’t able to serve as a container for “AttributeContainer”, which is the parent class for both SoapObject and SoapPrimitive. A SoapObject can only contain other SoapObjects and another type of thing that SoapObject calls a property. FindBookByISBN函數只有一個參數——ISBN。ISBN是一個簡單的字符串。你可能會認為你可以簡單地把ISBN封裝到SoapPrimitive,並且以某種方式添加到findBookRequest。這是一種非常合理的方法。出於某種原因,SoapObject不能作為SoapPrimitives的容器。甚至更讓人驚訝的是,它也不能作為屬性容器“AttributeContainer”(是SoapObject 和 SoapPrimitive的父類)而存在。一個SoapObject只能包含其他的SoapObjects,以及另外一種SoapObject稱之為屬性的東西。 phrase 3-2-5 What’s a property? It’s really just another way to represent a SOAP primitive. This may seem like a strange design decision to you. It seems like a strange design decision to me too. But I imagine the library designers had a good reason for doing it this way. In any case, if you want to add a primitive to a SoapObject, you’ll want to use the addProperty method. 什麼是屬性?這只是另一種用來表示SOAP原語的方式。對於你來說,可能看起來很奇怪。對我來說,看起來也很奇怪。但是我假設這個庫文件的設計者這樣做是為了一個好的目的。無論如何,如果你想要增加一個原語到SoapObject,你就會用到addProperty這個函數。
findBookRequest.addProperty("ISBN", "0-374-15012-5");

段落 原文 譯文 phrase 3-2-6 The addProperty method accepts two arguments. The first argument is the name of the parameter or property to add. The second parameter is the actual value of the property. addProperty函數接受兩個標識符,第一個標識符表示想要添加的參數名。第二個標識符表示前面添加的參數的實際值。 phrase 3-3 SoapSerializationEnvelope   phrase 3-3-1 Once you have the SoapObject that represents your request, you’ll need to wrap it in a SOAP envelope. ksoap2-android has a couple of classes for working with SOAP envelopes, but the one you’ll most often use is the SoapSerializationEnvelope. 一旦你使用SoapObject表示你的請求,你需要把它封裝到SOAP envelope裡面。ksoap2-android有兩三個類是與SOAP envelopes工作的,但是其中你經常會用到的是SoapSerializationEnvelope類。 phrase 3-3-2 If you browse through the library, you might notice a class called SoapEnvelope and wonder why you wouldn’t use that instead. SoapEnvelope is actually the base class for SoapSerializationEnvelope. Both classes serve as containers for the SOAP message header and body. And both provide mechanisms to serialize/deserialize SOAP data. But the problem with SoapEnvelope is that it doesn’t support SoapObjects or SoapPrimitives. It can only deal with kXML Node types. It’s geared more towards the scenario where you manually build up your own SOAP data structure using kXML and then hand it off to SoapEnvelope for serialization. It’s not very convenient to work with and since you’ll almost always be dealing with SoapObjects or SoapPrimitives, you’ll need to use the SoapSerializationEnvelope. 如果你浏覽了類庫文件,你可能會注意到一個名叫SoapEnvelope的類,然後疑惑你為什麼不用這個類來代替。SoapEnvelope是SoapSerializationEnvelope的基類。兩個類都作為SOAP信息的頭部和主體的容器。兩個類同時提供SOAP數據進行序列化/反序列化的機制。但是SoapEnvelope存在的問題是,它不支持SoapObjects也不支持 SoapPrimitives。它只能處理kXML節點類型的數據。它更適合於你利用kXML手動建立自己的SOAP數據結構,然後交給 SoapEnvelope進行序列化。它並不便於使用因為你總是一直在處理SoapObjects或者SoapPrimitives,所以你還是需要使用SoapSerializationEnvelope。 phrase 3-3-3 SoapSerializationEnvelope’s constructor looks like so. SoapSerializationEnvelope類的結構如下:
public SoapSerializationEnvelope(int version);

段落 原文 譯文 phrase 3-3-4 The constructor accepts one argument – the version of SOAP you intend to use. You might use it like so. 構造函數值接受一個參數——你想要使用的SOAP的版本號。你可能會這樣使用。
SoapSerializationEnvelope soapEnvelope = 
    new SoapSerializationEnvelope(SoapEnvelope.VER12);

段落 原文 譯文 phrase 3-3-5 All of the supported versions of SOAP are defined as constants in the SoapEnvelope base class. The versions of SOAP that are currently supported are 1.0, 1.1, and 1.2. The version you choose is dependent on the versions supported by the web service. Consult the web service documentation to see what it supports. 所有支持工作的SOAP的版本號,在SoapEnvelope基類中被定義成常量。目前為止支持的SOAP版本號是1.0,1.1,1.2。你選擇的版本號由你的web service支持的版本決定。查詢web service 文檔看看它支持的版本號。 phrase 3-3-6 Now you can finally add your request object to the envelope by using the setOutputSoapObject() method. 現在你可以通過使用setOutputSoapObject() 函數添加你的請求對象到envelope。
soapEnvelope.setOutputSoapObject(findBookRequest);

段落 原文 譯文 Part6 What about SOAP headers? 什麼是SOAP header phrase 1 SOAP headers seem to have been a bit of an afterthought in the design of ksoap2-android. There is no class in the library that represent a SOAP header. The handling of all the header data is managed by the SoapEnvelope base class. And as you might guess from my earlier comments concerning SoapEnvelope, SOAP headers are specified using a kXML data type – Element. SOAP header看起來像是ksoap2-android裡面馬後炮的東西。在庫文件中並沒有類表示一個SOAP header。對SOAP header數據的處理都受SoapEnvelope基類的管理。正如你可能會像我之前對SoapEnvelope所作出的評論一樣猜測,SOAP header使用特定的kXML數據類型——Element。 phrase 2 SoapEnvelope has a member variable called headerOut, which is declared as an array of Elements. By default this is null, which results in an empty SOAP header element when serialized. To specify your own set of headers, instantiate a new Element array and populate it appropriately for your application. Here’s an example of how you might do this for some fictional authorization header. SoapEnvelope有一個成員變量headerOut,聲明為Element類型的數組。默認為null,導致序列化的時候SOAP header element是空值。指定你自己的header集合,實例化一個Element數組並且為了你的應用程序適當填充。這裡有一個例子,header是虛構的但是被認可的。
final String SERVICE_NAMESPACE = "ServiceNamespace"; 
Element authHeader = new Element();
authHeader.setNamespace(SERVICE_NAMESPACE);
authHeader.setName("authHeader");

Element username = authHeader.createElement(SERVICE_NAMESPACE, "username");
username.addChild(Node.TEXT, "skirk");
authHeader.addChild(Node.ELEMENT, username);

Element password = authHeader.createElement(SERVICE_NAMESPACE, "password");
password.addChild(Node.TEXT, "mypassword");
authHeader.addChild(Node.ELEMENT, password);
soapEnvelope.headerOut = new Element[] { authHeader };

If you know or suspect that your web service was implemented using .NET, set SoapSerializationEnvelope’s dotNet property to true. SoapSerializationEnvelope provides special SOAP encoding support for .NET web services.
如果你懷疑或者知道你的web service使用的是.NET,將SoapSerializationEnvelope設置成true。SoapSerializationEnvelope提供了.NET特殊的SOAP編碼支持。


段落 原文 譯文 Part6 HttpTransportSE   phrase 1 With the SOAP request packaged up in a SoapEnvelope, you’re now ready to send the request to your web service. The SOAP specification allows for any transport mechanism. If you wanted to use SMTP or FTP, for example, you totally could (assuming your web service supported it). But you would have to implement the protocol level support for one of these yourself. Out of the box, ksoap2-android only supports HTTP. And since this is the protocol used by 99% of all web services, you can rest easy knowing that all the hard work has been done for you. 隨著SOAP請求封裝到SoapEnvelope中,你已經准備好發送請求到你的web service了。SOAP機制允許所有的傳輸機制。例如,如果你想使用SMTP或者FTP,你完全可以這樣做(假設你的webservice支持這種傳輸機制)。但是你必須執行協議級別支持其中一個。現在可用的 ksoap2-android只支持HTTP協議。因為這種協議支持99%的webservice,因此你可以高枕無憂,所有的辛勤工作已經完成。 phrase 2 HttpTransportSE is the class you’ll use for communicating with web services over HTTP. In case you’re curious, the “SE” part of the class name means that it’s built around J2SE classes. There’s actually another class, HttpTransport, that’s built around the J2ME connection framework. It’s included as part of the ksoap2-android library, but I’m not entirely sure why you would ever choose this over HttpTransportSE. HttpTransportSE是你會通過HTTP協議與webservice進行通信的類。如果你表示對“SE”感到好奇,它是類名稱的一部分,意味著它是建立在J2SE類基礎上的。事實上有另外一個類,HttpTransport,它是建立在J2ME鏈接框架上的。它包括在ksoap2-android中,但是我不是很確定為什麼選擇這個而不是HttpTransportSE。 phrase 3 To perform a SOAP request, construct an instance of HttpTransportSE and invoke the call method on the instance. 為了執行一個SOAP請求,構建 HttpTransportSE,並且實例化call函數。
HttpTransportSE htse = new HttpTransportSE(
    "http://www.example.com/mywebserviceurl");

htse.call("http://www.example.com/mywebserviceurl/FindBookByISBN", 
    soapEnvelope);

未完待續………………

  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved