Download presentation
Presentation is loading. Please wait.
Published byAshton Evans Modified over 10 years ago
1
Bruce Scharlau, University of Aberdeen, 2010 Java ME Networking Mobile Computing Some slides from MobEduNet This covers HTTP in detail, and mentions other means. Bluetooth will be covered later.
2
Bruce Scharlau, University of Aberdeen, 2010 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/
3
Bruce Scharlau, University of Aberdeen, 2010 OTA using Java
4
Bruce Scharlau, University of Aberdeen, 2010 OTA can be done with Antenna The following steps are necessary to run the OTA servlet: Install a servlet-capable Web server Add a new Web application under " /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. http://antenna.sourceforge.net/index.php
5
Bruce Scharlau, University of Aberdeen, 2010 Servlet templates are provided OTAServer (servlet) available under C:\Java\antenna-src-1.2.0\src\de\pleumann\antenna\http OTAServer (servlet) available under C:\Java\antenna-src-1.2.0\src\de\pleumann\antenna\http
6
Bruce Scharlau, University of Aberdeen, 2010 Java uses Internet for direct connections http://developers.sun.com/mobility/midp/articles/network/ Network operator
7
Bruce Scharlau, University of Aberdeen, 2010 Java end to end is possible From pdfs at: http://java.sun.com/blueprints/guidelines/designing_wireless_enterprise_applications/index.html
8
Bruce Scharlau, University of Aberdeen, 2010 CLDC - GCF Connected Limited Device Configuration offers Generic Connection Framework –Takes in URI as String and returns a connection object –All use pattern of: :// ;
9
Bruce Scharlau, University of Aberdeen, 2010 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
10
Bruce Scharlau, University of Aberdeen, 2010 extra APIs can enhance applications Just remember that they are optional, so may not be available http://java.sun.com/javame/overview/products.jsp
11
Bruce Scharlau, University of Aberdeen, 2010 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
12
Bruce Scharlau, University of Aberdeen, 2010 Generic Connection Framework
13
Bruce Scharlau, University of Aberdeen, 2010 Variety of Interfaces used for connections 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) Connection InputConnection OutputConnection DatagramConnection StreamConnectionNotifier StreamConnection ContentConnection HttpConnection HttpsConnection CommConnection SocketConnection SecureConnection
14
Bruce Scharlau, University of Aberdeen, 2010 Basic Architecture is simple Internet MIDP Device Web Server User requests information from an Application (e.g. MyServlet) Web server passes output from MyServlet back to the MIDlet Web Server launches MyServlet program and sends it parameters the MIDlet requested Web Server retrieves output from the MyServlet
15
Bruce Scharlau, University of Aberdeen, 2010 What is needed ? MIDlet & MIDP Device Servlet & Web Server Connection between MIDP Device & Web Server Common Protocol between MIDlet and Servlet
16
Bruce Scharlau, University of Aberdeen, 2010 Opening a Connection is easy Connection throws IOException so can report errors back to application try { Connection connection = Connector.open(http://java.sun.com); } catch (IOException ioe) { //report error } Connection also has close() method
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
19
Bruce Scharlau, University of Aberdeen, 2010 Use JSR 179 and JSR 280 to parse RESTful XML http://developers.sun.com/mobility/midp/articles/parsingxml/ Parse the XML to object on handset Check links on course web site
20
Bruce Scharlau, University of Aberdeen, 2010 XML parsing requires several files Go together Midlet plus parser handler class, and object class
21
Parsing errors not always obvious Bruce Scharlau, University of Aberdeen, 2010 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
22
Bruce Scharlau, University of Aberdeen, 2010 Need to use threads for multi-tasking in application Start a connection in a new thread so the application doesnt hang while waiting for a response
23
Bruce Scharlau, University of Aberdeen, 2010 Wrap thread for connection in try/catch public void commandAction(Command c, Displayable arg1) { if (c == mExitCommand) { notifyDestroyed(); } else if (c == mLoginCommand) { login(); } else if (c == mBackCommand) { mainMenu(); } else if (c == mHelloCommand) { Thread thread = new Thread() { public void run() { try { invokeServlet(); } catch (IOException e) { e.printStackTrace(); } } }; thread.start(); }
24
Bruce Scharlau, University of Aberdeen, 2010 Dukes Auction has two versions AuctionServletAuctionByteServlet Use Strings Use bytes to determine options
25
Bruce Scharlau, University of Aberdeen, 2010 Bytes are easier to organise Pass byte as option
26
Bruce Scharlau, University of Aberdeen, 2010 Methods are similar Get parameter (optional), create message, write reply
27
Bruce Scharlau, University of Aberdeen, 2010 MIDlet works with bytes anyways Same as hello() on next slide
28
Bruce Scharlau, University of Aberdeen, 2010 Binary response to Midlet Same as invokeServlet()
29
Bruce Scharlau, University of Aberdeen, 2010 Round trip with octet-stream from client to servlet From pdfs at: http://java.sun.com/blueprints/guidelines/designing_wireless_enterprise_applications/index.html
30
Bruce Scharlau, University of Aberdeen, 2010 Binary code makes it a pure Java solution Tightly coupled to the service. Need to use web services to interoperate with other systems. A change in one will require change in other
31
Bruce Scharlau, University of Aberdeen, 2010 There are trade-offs between binary and XML From pdfs at: http://java.sun.com/blueprints/guidelines/designing_wireless_enterprise_applications/index.html
32
Bruce Scharlau, University of Aberdeen, 2010 Need to consider state in application As with web apps, need to determine if, and how you will deal with state
33
Bruce Scharlau, University of Aberdeen, 2010 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 dont rely upon them Some operators use transcoding, which messes up your application See: http://wurfl.sourceforge.net/manifesto/index.htm for detailshttp://wurfl.sourceforge.net/manifesto/index.htm
34
Bruce Scharlau, University of Aberdeen, 2010 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
35
Bruce Scharlau, University of Aberdeen, 2010 Use same OOD principles for MIDlets as for other code Refactor out methods classes Apply design patterns as appropriate Remember that its mobile, so constrain for reduced memory
36
Bruce Scharlau, University of Aberdeen, 2010 Abstract out repeated code into methods AuctionMIDlet repeats lots of code so that its clear whats 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
37
Bruce Scharlau, University of Aberdeen, 2010 Extend Ant to cover building of web and midlet suite Use Ant and Antenna to build whole application Build MIDlet Build War Deploy MIDlet Deploy War Integrate and copy code as required
38
Bruce Scharlau, University of Aberdeen, 2010 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
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.