1 / 38

Java ME Networking

Java ME Networking. This covers HTTP in detail, and mentions other means. Bluetooth will be covered later. Mobile Computing. Some slides from MobEduNet. Getting the application onto the device with OTA.

heath
Download Presentation

Java ME Networking

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Java ME Networking This covers HTTP in detail, and mentions other means. Bluetooth will be covered later. Mobile Computing Bruce Scharlau, University of Aberdeen, 2010 Some slides from MobEduNet

  2. Getting the application onto the device with OTA OTA is Over the Air provisioning and lets users acquire the jad/jar as required via their browser http://developers.sun.com/mobility/midp/articles/ota/ Bruce Scharlau, University of Aberdeen, 2010

  3. OTA using Java Bruce Scharlau, University of Aberdeen, 2010

  4. OTA can be done with Antenna http://antenna.sourceforge.net/index.php • The following steps are necessary to run the OTA servlet: • Install a servlet-capable Web server • Add a new Web application under "<tomcat>/webapps". • Copy the "web.xml" file from the "etc" directory of the Antenna source distribution to "WEB-INF" and adjust. Copy the "antenna-bin.jar" binary distribution file to "WEB-INF/lib". • You should have a working configuration now. If the servlet URL is "http://localhost/antenna", the HTML and WML pages are available as "http://localhost/antenna/index.html" and "http://localhost/antenna/index.wml", respectively. • Test deployment by running the "deploy" sample (modify "build.xml" first). • To create your own layout for the pages, copy the two files from the "res" directory of the Antenna source distribution (or from CVS) to "WEB-INF" and modify them. There's documentation inside the "index.html" file that tells you how to do this. Bruce Scharlau, University of Aberdeen, 2010

  5. Servlet templates are provided OTAServer (servlet) available under C:\Java\antenna-src-1.2.0\src\de\pleumann\antenna\http Bruce Scharlau, University of Aberdeen, 2010

  6. Java uses Internet for direct connections Network operator Bruce Scharlau, University of Aberdeen, 2010 http://developers.sun.com/mobility/midp/articles/network/

  7. Java end to end is possible From pdfs at: http://java.sun.com/blueprints/guidelines/designing_wireless_enterprise_applications/index.html Bruce Scharlau, University of Aberdeen, 2010

  8. CLDC - GCF • Connected Limited Device Configuration offers Generic Connection Framework • Takes in URI as String and returns a connection object • All use pattern of: <protocol>://<address>;<parameters> Bruce Scharlau, University of Aberdeen, 2010

  9. MIDP 2.0 Additions moved beyond required HTTP • Added other interfaces and classes • HTTPSConnection for secure http connections • PushListener for use with PushRegistry and listens for inbound connections • SecureConnection to establish SSL or TLS connections • SecurityInfo interface provides methods to access secure networks • ServerSocketConnection defines this type of connection • UDPDatagramConnection Bruce Scharlau, University of Aberdeen, 2010

  10. ‘extra’ APIs can enhance applications Just remember that they are ‘optional’, so may not be available Bruce Scharlau, University of Aberdeen, 2010 http://java.sun.com/javame/overview/products.jsp

  11. Network URIs • http://java.sun.com for HttpConnection • socket://time-a.nist.gov:13 for StreamConnection • serversocket://:4444 for StreamConnectionNotifier • comm:0;baudrate=2400 for CommConnection • datagram://127.0.0.1 for DatagramConnection • file://address.dat for FileConnection • “bluetooth://psm=1001” for StreamConnection Bruce Scharlau, University of Aberdeen, 2010

  12. Generic Connection Framework Bruce Scharlau, University of Aberdeen, 2010

  13. Variety of Interfaces used for connections Connection • Connection (basic, generic connection) • close method • open method defined in Connector-class • InputConnection (capabilities needed in input stream connection) • openDataInputStream, openInputStream • OutputConnection (capabilities needed in output stream connection) • openDataOutputStream, openOutputStream • StreamConnection (combines input and output) • All methods inherited • ContentConnection (defines streamconnection over which content is passed) • getEncoding, getType • CommConnection (defines a logical serial port connection) • SocketConnection (defines socekt stream connection) StreamConnectionNotifier DatagramConnection InputConnection OutputConnection StreamConnection CommConnection ContentConnection HttpConnection HttpsConnection SocketConnection SecureConnection Bruce Scharlau, University of Aberdeen, 2010

  14. Basic Architecture is simple User requests information from an Application (e.g. MyServlet) Web Server launches MyServlet program and sends it parameters the MIDlet requested MIDP Device Internet Web Server Web server passes output from MyServlet back to the MIDlet Web Server retrieves output from the MyServlet Bruce Scharlau, University of Aberdeen, 2010

  15. What is needed ? • MIDlet & MIDP Device • Servlet & Web Server • Connection between MIDP Device & Web Server • Common Protocol between MIDlet and Servlet Bruce Scharlau, University of Aberdeen, 2010

  16. Opening a Connection is easy Connection throws IOException so can report errors back to application try{ Connectionconnection= Connector.open(http://java.sun.com); }catch(IOExceptionioe){ //report error } Connection also has close() method Bruce Scharlau, University of Aberdeen, 2010

  17. MIDP 3 is MSA(Mobile Service Architecture) • Aims to provide wider functionality for mobiles • Should work with CDC and CLDC devices • Should allow RFID and NFC object communication • Should enable XML parsing Bruce Scharlau, University of Aberdeen, 2010

  18. On Beyond Basics • web services via SOAP and REST • RMI • Location API These require extra libraries Need to check that they are present on device Bruce Scharlau, University of Aberdeen, 2010

  19. Use JSR 179 and JSR 280 to parse RESTful XML Parse the XML to object on handset Check links on course web site Bruce Scharlau, University of Aberdeen, 2010 http://developers.sun.com/mobility/midp/articles/parsingxml/

  20. Go together Go together XML parsing requires several files Midlet plus parser handler class, and object class Bruce Scharlau, University of Aberdeen, 2010

  21. Parsing errors not always obvious midlet parse midlet handler org.xml.sax.SAXParseException: - com.sun.ukit.xml.SAX$Parser.panic(), bci=6 - com.sun.ukit.xml.Parser.setinp(), bci=237 - com.sun.ukit.xml.SAX$Parser.parse(), bci=36 - com.sun.ukit.xml.SAX$ParserImp.parse(), bci=56 - com.auction.j2me.MyParsingMIDlet.loadXML(), bci=33 - com.auction.j2me.MyParsingMIDlet$1.run(), bci=11 midlet alert Need to catch errors, but sometimes only the emulator that causes problems Bruce Scharlau, University of Aberdeen, 2010

  22. Need to use threads for multi-tasking in application Start a connection in a new thread so the application doesn’t hang while waiting for a response Bruce Scharlau, University of Aberdeen, 2010

  23. Wrap thread for connection in try/catch publicvoid commandAction(Command c, Displayable arg1) { if (c == mExitCommand) { notifyDestroyed(); } elseif (c == mLoginCommand) { login(); } elseif (c == mBackCommand) { mainMenu(); } elseif (c == mHelloCommand) { Thread thread = new Thread() { publicvoid run() { try { invokeServlet(); } catch (IOException e) { e.printStackTrace(); } } }; thread.start(); } } Bruce Scharlau, University of Aberdeen, 2010

  24. Duke’s Auction has two versions Use bytes to determine options AuctionServlet AuctionByteServlet Use Strings Bruce Scharlau, University of Aberdeen, 2010

  25. Bytes are easier to organise Pass byte as option Bruce Scharlau, University of Aberdeen, 2010

  26. Methods are similar Get parameter (optional), create message, write reply Bruce Scharlau, University of Aberdeen, 2010

  27. MIDlet works with bytes anyways Same as hello() on next slide Bruce Scharlau, University of Aberdeen, 2010

  28. Binary response to Midlet Same as invokeServlet() Bruce Scharlau, University of Aberdeen, 2010

  29. Round trip with octet-stream from client to servlet From pdfs at: http://java.sun.com/blueprints/guidelines/designing_wireless_enterprise_applications/index.html Bruce Scharlau, University of Aberdeen, 2010

  30. Binary code makes it a pure Java solution Tightly coupled to the service. A change in one will require change in other Need to use web services to interoperate with other systems. Bruce Scharlau, University of Aberdeen, 2010

  31. There are trade-offs between binary and XML From pdfs at: http://java.sun.com/blueprints/guidelines/designing_wireless_enterprise_applications/index.html Bruce Scharlau, University of Aberdeen, 2010

  32. Need to consider state in application As with web apps, need to determine if, and how you will deal with state Bruce Scharlau, University of Aberdeen, 2010

  33. Sessions can maintain state the same as in regular web applications Use either cookies or, preferably, URL rewriting However, network operators can mangle the headers though, so don’t rely upon them Some operators use transcoding, which messes up your application See: http://wurfl.sourceforge.net/manifesto/index.htm for details Bruce Scharlau, University of Aberdeen, 2010

  34. Object reuse and development Same code in servlet and MIDlet Repeated code in MIDlet Need process such as Ant to cope with this, or use other solution Bruce Scharlau, University of Aberdeen, 2010

  35. Use same OOD principles for MIDlets as for other code Refactor out methods classes Apply design patterns as appropriate Remember that it’s mobile, so constrain for reduced memory Bruce Scharlau, University of Aberdeen, 2010

  36. Abstract out repeated code into methods AuctionMIDlet repeats lots of code so that it’s clear what’s happening. These could be removed to a method, or separate class – connection, try/catch, etc There is also the issue of the shared code between the servlet and MIDlet Bruce Scharlau, University of Aberdeen, 2010

  37. Extend Ant to cover building of web and midlet suite Use Ant and Antenna to build whole application Build War Build MIDlet Deploy War Deploy MIDlet Integrate and copy code as required Bruce Scharlau, University of Aberdeen, 2010

  38. Summary • Can use variety of means to connect Java ME devices to a network via GCF • All MIDP 2.0 devices support at least HTTP – need to check for capabilities • Sometimes need to share code across platforms (server and Java ME client) to ease programming Bruce Scharlau, University of Aberdeen, 2010

More Related