{"id":491,"date":"2003-09-05T21:47:40","date_gmt":"2003-09-06T01:47:40","guid":{"rendered":"http:\/\/wordpress.cephas.net\/?p=491"},"modified":"2003-09-05T21:47:40","modified_gmt":"2003-09-06T01:47:40","slug":"j2me-recipe-viewer-update-code-samples","status":"publish","type":"post","link":"https:\/\/cephas.net\/blog\/2003\/09\/05\/j2me-recipe-viewer-update-code-samples\/","title":{"rendered":"J2ME Recipe Viewer update &amp; code samples"},"content":{"rendered":"<p>Hey, it&#8217;s working! \ud83d\ude42  I ironed out the problems I was having with my J2ME Recipe Viewer application.  Couple things I did to make it purr:<\/p>\n<p>First, I mentioned that I was getting an IO error when trying to make a wireless connection using the phone, it became apparent that I needed way better error handling in the application once it moved from the <a href=\"http:\/\/java.sun.com\/products\/j2mewtoolkit\/\">WTK emulator<\/a> phase (where I could System.out.println) to the actual phone. So I created a <a href=\"http:\/\/cephas.net\/docs\/midp1.0\/javax\/microedition\/lcdui\/Form.html\">Form<\/a> that took a <a href=\"http:\/\/cephas.net\/docs\/midp1.0\/java\/lang\/String.html\">String<\/a> message as an argument to the constructor and then appended the String to the Form.  Then in the two <a href=\"http:\/\/cephas.net\/docs\/midp1.0\/java\/lang\/Thread.html\">Thread<\/a> classes that I used, if I got an error, I&#8217;d simply add the error message to the Form and use the DisplayManager (from the <a href=\"http:\/\/www.corej2me.com\/CoreJ2MEBook\/index.shtml\">Core J2ME book<\/a>, you can <a href=\"http:\/\/www.corej2me.com\/CoreJ2MEBook\/index_DL.shtml\">download the code<\/a>) to pop that form to displayable:<\/p>\n<p>midlet.displayManager.pushDisplayable(new FormError(midlet, error.toString()));<\/p>\n<p>Second, I noticed that the getType() method of the <a href=\"http:\/\/cephas.net\/docs\/midp1.0\/javax\/microedition\/io\/HttpConnection.html\">HttpConnection<\/a> object wasn&#8217;t returning &#8220;text\/xml&#8221; like it was on the emulator, instead it was returning <\/p>\n<p>&#8220;text\/xml;Charset=us-ascii&#8221;<\/p>\n<p>which means that the AT&amp;T proxy is messing with the response header that I&#8217;m sending from my webserver.  Because my logic looked like this:<\/p>\n<p>if (code == HttpConnection.HTTP_OK &amp;&amp; type.equals(&#8220;text\/xml&#8221;)) {<\/p>\n<p>after attempting to connect to my web server, I was never able to get to the XML parsing and subsequent handoff to the next screen.  Anyway, I removed the check on the type (why did I have it in the first place? I don&#8217;t know&#8230;) and then everything worked! <\/p>\n<p>After I got it working on the phone, I wanted to fine tune some of the other things I thought were kludgy about the app.  Both the ListRecipes and ListCategory classes (which both extend the MIDP <a href=\"http:\/\/cephas.net\/docs\/midp1.0\/javax\/microedition\/lcdui\/List.html\">List<\/a> class) presented a list of choices to the user and then on select would do two things: a) they would present an Alert to the user and then b) would start a Thread responsible for making a wireless HTTP connection, which itself would then redraw the screen with the result of the HTTP connection.  The thinking behind this is probably common sense to someone who&#8217;s been developing non web-apps for some time, but it was something that I learned about while sitting in the back row of <a href=\"http:\/\/servlet.java.sun.com\/javaone\/sf2003\/conf\/sessions\/display-2037.en-75103.jsp\">TS-2037<\/a>: &#8220;<i>Advanced MIDP Programming: Developing High Performance, Highly Responsive Wireless Applications<\/i>&#8221; [download the <a href=\"http:\/\/servlet.java.sun.com\/javaone\/resources\/content\/sf2003\/conf\/sessions\/pdfs\/2037.pdf\">pdf<\/a>, it&#8217;s got 91 very useful slides on MIDP UI].  The problem with this approach is that the Alert dismisses itself after a set period of time (in this case 3 seconds) which resulted in a weird user interface.  You&#8217;d select &#8220;Breakfast&#8221;, see an alert for 3 seconds, see the recipe categories again, and then a couple seconds later would see the breakfast recipes.  So instead of an Alert, I created another Form that took a String as an argument to its constructor and then displayed that Form&#8230; which would not dismiss itself and then let the Thread take care of redrawing the screen.  <\/p>\n<p>Finally, I updated the search form that it too starts up a Thread, which connects to the webserver, which returns XML and then displays a list of matching recipes.<\/p>\n<p>You can download the updated J2ME app and source:<\/p>\n<p><a href=\"\/projects\/recipeviewer\/RecipeViewer.jar\">RecipeViewer.jar<\/a><br \/>\n<a href=\"\/projects\/recipeviewer\/RecipeViewer.jad\">RecipeViewer.jad<\/a><br \/>\n<a href=\"\/projects\/recipeviewer\/recipeviewer_source.zip\">RecipeViewer source<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Hey, it&#8217;s working! \ud83d\ude42 I ironed out the problems I was having with my J2ME Recipe Viewer application. Couple things I did to make it purr: First, I mentioned that I was getting an IO error when trying to make a wireless connection using the phone, it became apparent that I needed way better error &hellip; <a href=\"https:\/\/cephas.net\/blog\/2003\/09\/05\/j2me-recipe-viewer-update-code-samples\/\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">J2ME Recipe Viewer update &amp; code samples<\/span> <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[20,6,10],"tags":[],"_links":{"self":[{"href":"https:\/\/cephas.net\/blog\/wp-json\/wp\/v2\/posts\/491"}],"collection":[{"href":"https:\/\/cephas.net\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/cephas.net\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/cephas.net\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/cephas.net\/blog\/wp-json\/wp\/v2\/comments?post=491"}],"version-history":[{"count":0,"href":"https:\/\/cephas.net\/blog\/wp-json\/wp\/v2\/posts\/491\/revisions"}],"wp:attachment":[{"href":"https:\/\/cephas.net\/blog\/wp-json\/wp\/v2\/media?parent=491"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/cephas.net\/blog\/wp-json\/wp\/v2\/categories?post=491"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/cephas.net\/blog\/wp-json\/wp\/v2\/tags?post=491"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}