Books
in black and white
Main menu
Share a book About us Home
Books
Biology Business Chemistry Computers Culture Economics Fiction Games Guide History Management Mathematical Medicine Mental Fitnes Physics Psychology Scince Sport Technics
Ads

More Java Pitfalls Share Reactor - Daconta M,C.

Daconta M,C. More Java Pitfalls Share Reactor - Wiley publishing, 2003. - 476 p.
ISBN: 0-471-23751-5
Download (direct link): morejavapitfallssharereactor2003.pdf
Previous << 1 .. 62 63 64 65 66 67 < 68 > 69 70 71 72 73 74 .. 166 >> Next

206 for (int i=0; i < cnt; i++)
207 {
208 Object o = root.getChild(i);
209 if (o instanceof Node)
210 {
211 Node n = (Node) o;
212 String name = n.getName();
213 if (name.equals(ELEMENT_PRODUCT_NAME))
214 {
215 v.addElement(n);
216 }
217
218 // element?
219 if (n.getChildCount() > 0)
220 getProductNames(n, v);
221 }
222 }
223 }
224 }
Listing 22.3 (continued)
180 Item 22
The doAmazonSearch() method is similar to its Swing counterpart with a few exceptions. For example, you cannot directly get a selected item (like with the getSe-lectedItem() method) from a ChoiceGroup or List; instead, you must get the index (line 144) and then call getString() (line 144). Such minor API changes can be frustrating, though in this case the purpose is to eliminate the need for casting (in this direct port it was accidentally left in but is corrected in the next version). On line 158 notice that the httpGet() method returns a byte array (which is required by the kxml parser). Lines 164 to 167 represent the parsing of the XML document using the kxml package. At line 171, we call the utility method, getProductNames(), to recursively traverse the document tree and extract the product Nodes. Unfortunately, this was necessary because the kdom package does not have a getElementsByTagName() method.
Like the minor API changes in the javax.microedition packages, the kdom package has a slightly different API than the w3c DOM package. Such API changes only cause a serious pitfall when such a change eliminates any implicit guarantee of the former abstraction. For the DOM, a tree of Nodes represents the "flattened view" where every object is of type Node. This uniformity makes traversal easy and consistent. Unfortunately, kxml breaks the metaphor by mixing Nodes and other objects (like Strings). This nonuniformity led to runtime ClassCastExceptions (due to the assumption of uniformityóa classic pitfall) and required explicit testing (line 209). Additionally, the kxml changed the method names from getNodeName() to getName() and from getNodeValue() to getText().
Figure 22.3 displays the Network Monitor application, which is part of Sun Microsystems J2ME Wireless Toolkit. This toolkit allows you to emulate J2ME on a personal computer or workstation and enabled the writing and testing of this pitfall. Sun Microsystems did a real service to developers in delivering such a high-quality emulation environment for Java programmers. You can download the kit from http://java.sun.com/products/j2mewtoolkit/.
The Network Monitor application captures all communication between your MIDlet and the Web server. The left pane shows a tree with all HTTP requests and responses. Clicking on a request or response displays the details of the communication in the right pane in both hexadecimal and ASCII. Now we can examine the port of AmazonHttpGet to the J2ME platform in Listing 22.4.
Network Monitor-RIMJavaHandheld - Wireless Toolkit
I Filter 7 –ň‚„ Settings | Clear Sort By: |Time Show By: jstatus Line ^ |
¬ IHTTP.' GET /oncę/xml?v-l .08i-v\i Body
t CHttpI HTTP/1.1 200 OK
URL: //xml.amazon.cofn/onca/xnnJ?v=l.08it=webservices-208udev4=03AG4L7PI53LPH8iKeywordSearch=daconta8inxtde... 5ize: 8085
<1 I
4a0: 4b0: 4c0: 4d0: 4e0: 4f 0: 500: 510: 520:
3c 4f 75 72 50 72 69 63 65 3e 24 33 31 2e 34 39 k0urPrice>$31.49
3c 2f 4f 75 72 50 72 69 63 65 3e Oa 20 20 20 20 </0urPrice>.
20 20 3c 55 73 65 64 50 72 69 63 65 3e 24 32 34 <UsedPrice>$24
2e 39 35 3c 2f 55 73 65 64 50 72 69 63 65 3e 0a .95</UsedPrice>.
20 20 20 3c 2f 44 65 74 61 69 6c 73 3e Oa 20 20 </Details>.
20 3c 44 65 74 61 69 6c 73 20 75 72 6c 3d 22 68 <Details utl-'ŕ
74 74 70 3a 2f 2 f 77 77 77 2e 61 6d 61 7a 6f 6e ttp://mro.anazon
2e 63 6f 6d 2f 65 78 65 63 2f 6f 62 69 64 6f 73 .cou/exec/obidos
2f 72 65 64 69 72 65 63 74 3f 74 61 67 3d 77 65 /rediEect?tag>we
Number of shown messages: 2 out of 2
Figure 22.3 The J2ME Wireless Toolkit Network Monitor.
J2ME Performance and Pitfalls 181
001: /* BadMicroAmazonHttpGet.java */
002: package org.javapitfalls.item22;
003:
004: import javax.microedition.io.*;
005: import java.io.*;
006: import java.util.*;
007:
008: public class BadMicroAmazonHttpGet
009: {
// - deleted static constants -- No change 056:
057: static Timer ticker = new Timer();
058:
// - deleted stringExists() method -- No change 076:
077: private StringBuffer urlBuf;
078:
079: public StringBuffer getUrlBuf()
080: { return urlBuf; }
081:
082: public BadMicroAmazonHttpGet()
083: {
084: newBaseURL();
085: }
086:
// - deleted method newBaseURL() -- No change // - deleted all validation methods -- No change // - deleted all addXXX methods -- No change // - deleted replaceString() -- No change 216:
217: public byte [] httpGet() throws IOException
218: {
219: ticker.reset("Started Timer in httpGet()");
220: // get the connection object
221: String surl = urlBuf.toString();
222: surl.trim();
223: System.out.println("url: " + surl);
224:
Previous << 1 .. 62 63 64 65 66 67 < 68 > 69 70 71 72 73 74 .. 166 >> Next