Post on 28-Mar-2015
transcript
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.
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/
Bruce Scharlau, University of Aberdeen, 2010
OTA using Java
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 "<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.
http://antenna.sourceforge.net/index.php
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
Bruce Scharlau, University of Aberdeen, 2010
Java uses Internet for direct connections
http://developers.sun.com/mobility/midp/articles/network/
Network operator
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
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:
<protocol>://<address>;<parameters>
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
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
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
Bruce Scharlau, University of Aberdeen, 2010
Generic Connection Framework
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
Bruce Scharlau, University of Aberdeen, 2010
Basic Architecture is simple
Internet MIDPDevice
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 fromthe MyServlet
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
Bruce Scharlau, University of Aberdeen, 2010
Opening a Connection is easyConnection 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
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
Bruce Scharlau, University of Aberdeen, 2010
On Beyond Basics• web services via SOAP and REST
• RMI
• Location API
These require extra librariesThese require extra libraries
Need to check that they are present on deviceNeed to check that they are present on device
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 handsetParse the XML to object on handset
Check links on course web siteCheck links on course web site
Bruce Scharlau, University of Aberdeen, 2010
XML parsing requires several files
Go togetherGo together
Midlet plus parser handler class, and object class
Parsing errors not always obvious
Bruce Scharlau, University of Aberdeen, 2010
midlet parsemidlet handlerorg.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=11midlet alert
Need to catch errors, but sometimes only the emulator that causes problems
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 doesn’t hang while waiting for a response
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();} }
Bruce Scharlau, University of Aberdeen, 2010
Duke’s Auction has two versions
AuctionServlet AuctionByteServlet
Use StringsUse Strings
Use bytes to determine optionsUse bytes to determine options
Bruce Scharlau, University of Aberdeen, 2010
Bytes are easier to organise
Pass byte as option
Bruce Scharlau, University of Aberdeen, 2010
Methods are similar
Get parameter (optional), create message, write reply
Bruce Scharlau, University of Aberdeen, 2010
MIDlet works with bytes anyways
Same as hello() on next slideSame as hello() on next slide
Bruce Scharlau, University of Aberdeen, 2010
Binary response to Midlet
Same as invokeServlet()Same as invokeServlet()
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
Bruce Scharlau, University of Aberdeen, 2010
Binary code makes it a pure Java solution
Tightly coupled to the service.Tightly coupled to the service.
Need to use web services to interoperate with other systems.
Need to use web services to interoperate with other systems.
A change in one will require change in otherA change in one will require change in other
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
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
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 don’t rely upon them
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
Object reuse and development
Same code in servlet and MIDletSame code in servlet and MIDlet
Repeated code in MIDletRepeated code in MIDlet
Need process such as Ant to cope with this, or use other solution
Need process such as Ant to cope with this, or use other solution
Bruce Scharlau, University of Aberdeen, 2010
Use same OOD principles for MIDlets as for other code
Refactor out methods classesRefactor out methods classes
Apply design patterns as appropriate Apply design patterns as appropriate
Remember that it’s mobile, so constrain for reduced memory
Remember that it’s mobile, so constrain for reduced memory
Bruce Scharlau, University of Aberdeen, 2010
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
Extend Ant to cover building of web and midlet suite
Use Ant and Antenna to build whole application
Build MIDletBuild War
Deploy MIDlet Deploy War
Integrate and copy code as required
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