Enterprise J2ME Developing Mobile Java Applications [Electronic resources] نسخه متنی

اینجــــا یک کتابخانه دیجیتالی است

با بیش از 100000 منبع الکترونیکی رایگان به زبان فارسی ، عربی و انگلیسی

Enterprise J2ME Developing Mobile Java Applications [Electronic resources] - نسخه متنی

Michael Juntao Yuan

| نمايش فراداده ، افزودن یک نقد و بررسی
افزودن به کتابخانه شخصی
ارسال به دوستان
جستجو در متن کتاب
بیشتر
تنظیمات قلم

فونت

اندازه قلم

+ - پیش فرض

حالت نمایش

روز نیمروز شب
جستجو در لغت نامه
بیشتر
لیست موضوعات
توضیحات
افزودن یادداشت جدید



15.4 Introducing Amazon XML Services


The Amazon Web Services interface (v2.0) is designed to provide Amazon associates programmatic access to Amazon catalog data and search functionalities. Amazon Web Services is available in two flavors.


An Extremely Small and Tolerant Parser


We often construct "simple and dirty" XML documents to pass information quickly. Those documents often have only elements and attributes. They are not even necessarily well formed (i.e., missing tags or incorrectly nested tags). In fact, most HTML files on the Internet are "broken" XML documents. Standard XML parsers might balk at the broken XML structures. Even if they do parse, standard XML parsers have many features that are not needed for simple documents. The XmlReader class in the open source Utils4ME project is an extremely small pull parser that has only a 5 KB memory footprint. It does not handle namespace, comment, or processing instructions, and is tolerant of broken XML structures.

A standard SOAP Web Service. The application architecture here is SOAP RPC. Query and response data are all enclosed in SOAP messages. We will discuss SOAP in more detail in Chapters 16 and 17.

A literal XML service. In this model, the query is encoded as parameters in the request URL. The Amazon server returns an XML document containing the response data. The tags and structure of the returned document are defined in DTD files provided by Amazon.



A RESTful Web Service


The Amazon literal XML service turns out to be much more popular than the SOAP service among Amazon associates. A problem with the SOAP model is that it does not take advantage of existing HTTP infrastructure at all. It merely uses the HTTP POST as a convenient transport channel. In the SOAP model, the URLs are never self-contained. They have to be combined with the POST content to describe a certain RPC exchange (see Chapter 16). Since the SOAP Web Service is out of sync with HTTP, it has been difficult for many Web developers to comprehend and use. To reconcile this problem, Roy Fielding proposed a new XML Web Services model that makes use of HTTP's intrinsic semantic structure. It is called the RESTful (Representational State Transfer) Web Service (see "Resources"). The Amazon literal service is a RESTful Web Service. It makes URLs real pointers to unique resources and makes sensible use of HTTP operations such as GET and POST. As a result, the literal service is a lot more intuitive to Web developers and easier to use.

The Amazon service can operate in lite or heavy mode. A lite mode response is smaller but contains less information. Due to the bandwidth and processing power limits of mobile clients, we use the lite mode in this chapter. To use the Amazon service, you have to first obtain an authentication token from the Amazon Web site. Then, you encode query parameters into a URL string. For example, the following URL (without line breaks)


http://xml.amazon.com/onca/xml?v=1.0
&t=webservices-20&dev-t=ABCD123456
&KeywordSearch=mobile%20java
&mode=books&type=lite&page=1&f=xml

specifies that a user identified by token ABCD123456 asks to search keywords mobile java in books store. Note that the space between keywords is encoded using %20 per URL encoding rules. The first page result is returned as a lite version, literal XML document. Listing 15.3 demonstrates the returned XML document. I have added some white spaces and replaced long strings with ellipses (...) to make it more readable.

Listing 15.3. A sample Amazon search response message



<ProductInfo
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation=
"http://xml.amazon.com/schemas/dev-lite.xsd">
<Details
url="http://www.amazon.com/exec/obidos/...">
<Asin>0471034657</Asin>
<ProductName>
Mobile Information Device Profile for Java 2
Micro Edition (J2ME): Professional
Developer's Guide
</ProductName>
<Catalog>Book</Catalog>
<Authors>
<Author>C. Enrique Ortiz</Author>
<Author>Eric Giguere</Author>
</Authors>
<ReleaseDate>15 January, 2001</ReleaseDate>
<Manufacturer>
John Wiley &amp; Sons
</Manufacturer>
<ImageUrlSmall>
http://images.amazon.com/...THUMBZZZ.jpg
</ImageUrlSmall>
<ImageUrlMedium>
http://images.amazon.com/...MZZZZZZZ.jpg
</ImageUrlMedium>
<ImageUrlLarge>
http://images.amazon.com/...LZZZZZZZ.jpg
</ImageUrlLarge>
<ListPrice>\$49.99</ListPrice>
<OurPrice>\$49.99</OurPrice>
<UsedPrice>\$28.99</UsedPrice>
</Details>
<Details url="...">
... ...
</Details>
... ...
</ProductInfo>

Our sample MIDlet AmazonLite demonstrates how to parse the Amazon lite document. AmazonLite first prompts the user for the keywords to search. Then, the user clicks which XML parsing mode she would like to test. Button Pull is for XmlPull, and button kDOM is for a document model mode. MIDlet then sends out the query, receives data, parses the response document, and then displays extracted data in a new form. Figure 15.4 shows the program in action.


Figure 15.4. Mobile access to Amazon XML search interface.


XML parsing in AmazonLite is done by the kXML parser. kXML is an open source XML parser that is compatible with all J2ME platforms. It is developed under the Endrya ME project and released under the CPL license. kXML v2.0 supports the XmlPull interfaces as well as the kDOM document model.


/ 204