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 .. 61 62 63 64 65 66 < 67 > 68 69 70 71 72 73 .. 166 >> Next

067 modeChoice.setSelectedIndex(1, true);
068 searchScreen.append(modeChoice);
069 searchScreen.addCommand(searchCommand);
070 searchScreen.addCommand(exitCommand);
071 searchScreen.setCommandListener(this);
072
073 // results list
074 resultsScreen = new List("Results", List .EXCLUSIVE);
075 resultsScreen.addCommand(detailsCommand) ;
// - removed adding other commands to resultsScreen
080
081 // details text box
082 detailsScreen = new TextBox("Details", " ", 1024, 2
TextField.ANY);
// - removed adding commands to detailsScreen
086 }
Listing 22.3 (continued)
The constructor for BadMicroAmazonSearch performs three key functions: Gets the Display object via Display.getdisplay() (line 50)
J2ME Performance and Pitfalls 177
Instantiates the Commands (lines 53 to 57) and Screens (lines 61, 74, and 82)
Adds the Commands and subcomponents (for the Form) to the screens
There are many minor differences in a MIDP GUI compared to a J2SE GUI, such as not adding the screens to the Display (as we would to a JFrame); instead, we call Display.setCurrent() to a Display object (as we will in the startApp() method below):
088 public void startApp()
089 {
090 display.setCurrent(searchScreen);
091 }
092
// - removed pauseApp() and destroyApp() as they did nothing
102
103 public void commandAction(Command c, Displayable s)
104 {
105 String cmd = c.getLabel();
106 if (cmd.equals(CMD_EXIT))
107 {
108 destroyApp(false);
109 notifyDestroyed();
110 }
111 else if (cmd.equals(CMD_SEARCH))
112 {
113 // check we have the valid parameters\
114 String targets = targetTF.getString();
115 if (targets.length() == 0)
116 {
117 display.setCurrent(new Alert("Search Error", "'Search 2
For text field cannot be empty.", null, AlertType.ERROR));
118 }
119 else
120 {
121 try
122 {
123 page = 1; // reset
124 doAmazonSearch(page, targets);
125 } catch (Exception e)
126 {
127 e.printStackTrace(); 2
128 display.setCurrent(new Alert("SearchError",
"ERROR: reason: " + e.toString(),null, AlertType.ERROR));
129 }
130 }
131 }
// - removed handling CMD_DETAILS for brevity
136 }
Listing 22.3 (continued)
The commandAction() method is analogous to the actionPerformed() method in J2SE. Note that instead of the getActionCmd(), we call getLabel() to retrieve
178 Item 22
the text label of the Command. Although this is similar to the method used in the Swing application, it is a classic pitfall, since it is much slower than comparing the Command references passed in with the references of our predefined Command objects (like searchCommand). We make this optimization in the next version of the program. The rest of the method is nearly identical to the actionPerformed() method, except the error reporting requires the creation of an Alert screen. Although here in this "bad" version we create temporary objects (lines 117 and 128), hopefully, you noticed this waste and the opportunity for optimization:
138 private void doAmazonSearch(int page, String targets) throws 2
Exception, IOException
139 {
140 ticker.reset("Started Timer in doAmazonSearch()");
141 getter.newBaseURL(); // reset
142
143 // get the operation
144 int idx = opsChoice.getSelectedIndex();
145 String op = (String) opsChoice.getString(idx);
146
// - removed getting the mode as it is similar to getting the op
150
// - removed getter.addXXX methods -- no change.
156
157 // GET it
158 byte [] response = getter.httpGet();
159 System.out.println("Have response. Size is: " +
response.length);
160
161 // parse the XML, extract ProductNames
162 // Kxml required ~ 200k for a full parse.
163 String [] productNames = null;
164 ByteArrayInputStream bais = new 2
ByteArrayInputStream(response);
165 InputStreamReader isr = new InputStreamReader(bais);
166 XmlParser parser = new XmlParser(isr);
167 Document doc = new Document();
168 doc.parse(parser);
169
170 productNodes = new Vector();
171: getProductNames(doc.getRootElement(), productNodes);
172 if (productNodes != null)
173 {
174 int len = productNodes.size();
Listing 22.3 (continued)
J2ME Performance and Pitfalls 179
175 System.out.println("# of products found: " + len);
176 productNames = new String[len];
177 for (int i=0; i < len; i++)
178 {
179 Node n = (Node) productNodes.elementAt(i);
180 productNames[i] = n.getText();
181 }
182 }
183
184 if (productNames != null && productNodes.size() > 0)
185 {
186 // populate the list
187 for (int i=0; i < productNames.length; i++)
188 resultsScreen.append(productNames[i], null);
189
190 // set the display to the results
191 display.setCurrent(resultsScreen);
192 }
// removed the else block for brevity
200 ticker.printStats("Method doAmazonSearch()");
201 }
202
203 public void getProductNames(Node root, Vector v)
204 {
205 int cnt = root.getChildCount();
Previous << 1 .. 61 62 63 64 65 66 < 67 > 68 69 70 71 72 73 .. 166 >> Next