SlideShare a Scribd company logo
1 of 33
Java™Platform, Micro Edition Part 7Generic Connection Framework, HTTP and Sockets v3.0 – 06 April 2009 1 Andreas Jakl, 2009
Disclaimer These slides are provided free of charge at http://www.symbianresources.com and are used during Java ME courses at the University of Applied Sciences in Hagenberg, Austria at the Mobile Computing department ( http://www.fh-ooe.at/mc ) Respecting the copyright laws, you are allowed to use them: for your own, personal, non-commercial use in the academic environment In all other cases (e.g. for commercial training), please contact andreas.jakl@fh-hagenberg.at The correctness of the contents of these materials cannot be guaranteed. Andreas Jakl is not liable for incorrect information or damage that may arise from using the materials. This document contains copyright materials which are proprietary to Sun or various mobile device manufacturers, including Nokia, SonyEricsson and Motorola. Sun, Sun Microsystems, the Sun Logo and the Java™ Platform, Micro Edition are trademarks or registered trademarks of Sun Microsystems, Inc. in the United States and other countries.  Andreas Jakl, 2009 2
Contents Generic Connection Framework HTTP Connections Sockets Andreas Jakl, 2009 3
Generic Connection Framework Java SE: 150+ classes and interfaces (java.io, java.net) Bytecode-size: java.net 200kB+ Consistency: Different objects for protocols CLDC: Generic Connection Framework (GCF) Unified API for various protocols Profilesdefine protocols (MIDP, additional packages) Mandatory for MIDP 2.0:Hypertext Transfer Protocol (HTTP)Hypertext Transfer Protocol over TLS/SSL (HTTPS) Today: GCF made its way into Java SE (JSR 197) Andreas Jakl, 2009 4
Generic Connection Framework Hierarchy of interfaces and classes to: create connections (HTTP, datagrams, ...) perform I/O GCF used by optional packages Bluetooth, Files, SmartCards, Messaging, …  … might look difficult at first, but is very easy to use! Andreas Jakl, 2009 5
Connector Single Factory-class, creates any type of connection Examples: Connector.open(“http://www.mopius.com/”); Connector.open(“socket://realreplay.mopius.com:5412”); Connector.open(“file://data.txt”); Andreas Jakl, 2009 6 Connector.open(“protocol:address;parameters”);
Connector – Protocols  Protocol is chosen automatically, based on URL GCF searches for class that implements protocol Success: Returns class that implements Connection-interface Failure: ConnectionNotFoundException HTTP is supported for sure, Sockets might be unavailable on phone / through operator Andreas Jakl, 2009 7 Example: HttpConnection con = (HttpConnection) Connector.open (“http://www.mopius.com/”);
Connect and Transfer Short example for sockets (omits error handling): Andreas Jakl, 2009 8 // Open the connection to a URL SocketConnection con = (SocketConnection) Connector.open(“socket://127.0.0.1:5000”); // Send request DataOutputStream dos = con.openDataOutputStream(); dos.writeShort(12); dos.writeUTF(“Hello World”); dos.close(); // Receive answer DataInputStreamdis = con.openDataInputStream(); short msgId = dis.readShort(); dis.close(); // Cleanup con.close();
GCF – High Level Andreas Jakl, 2009 9 Generic way of handlingall communication andall protocols. Generic factory class, common interface Wait for incoming stream connections Stream-based I/O Packet-based I/O GCF (without additional packages like Bluetooth)supports three types of communication Additional content-specific information (data length, content type, data encoding, …) More comfortablefunctions for protocolslike HTTP are available.
GCF – Basic Class Diagram Andreas Jakl, 2009 10 Connection Connector ConnectionNotFoundException Generic factory class, common interface creates throws DatagramConnection InputConnection OutputConnection StreamConnectionNotifier Other subtypes… Wait for incoming stream connections Stream-based I/O Packet-based I/O creates Datagram StreamConnection ContentConnection Other subtypes… Other subtypes… Additional content-specific information (data length, content type, data encoding, …) Other subtypes… CLDC
GCF – Basic Class Diagram Andreas Jakl, 2009 11 Connection Connector ConnectionNotFoundException creates throws DatagramConnection InputConnection OutputConnection StreamConnectionNotifier Other subtypes… creates Datagram StreamConnection ContentConnection Other subtypes… Other subtypes… Other subtypes… CLDC
GCF – MIDP Class Diagram Andreas Jakl, 2009 12 Connection Connector ConnectionNotFoundException creates throws DatagramConnection InputConnection OutputConnection StreamConnectionNotifier Bluetooth Messaging creates Datagram StreamConnection UDPDatagramConnection CommConnection SocketConnection ContentConnection ServerSocketConnection FileConnection HttpConnection SecureConnection HttpsConnection MIDP 2.0 CLDC Optional JSRs
URL Uniform Resource Locator (URL) Identify connection type and endpoint Andreas Jakl, 2009 13 scheme://user:password@host:port/url-path;parameters
URL – Schemes Some of GCF connection types: Andreas Jakl, 2009 14 ... you can only be sure that HTTP(S) is available on a device!
public void commandAction (Command command, Displayable displayable) {     if (command == iCmdConnect) { con = (HttpConnection) Connector.open("http://www.mopius.com/images/mopius.png");     } } Asynchronous Connection Andreas Jakl, 2009 15 Establishing a connection in the command handling function leads to a deadlock!  The user can no longer select “Yes” or “No”. Notification message provided by WTK (console):
Asynchronous Connection Solution: Start own thread for networking code! Andreas Jakl, 2009 16 public class MyClass extends MIDlet implements CommandListener, Runnable {     // … // Process the command to connect to the network     public void commandAction(Command command, Displayable displayable) {         if (command == cmdConnectToWeb) { // Start connection in extra thread  commandAction-function is not blocked new Thread(this).start();         }     } // Connect to the network public void run() { // … HttpConnection con = (HttpConnection)Connector.open(url); // …     } }
Error Handling Connection should always be closed ( Exception!) Andreas Jakl, 2009 17 // Define those two variables outside the try-block, so that they are known in the finally-block! SocketConnection con = null; DataInputStream is = null; try {     con = (SocketConnection)Connector.open(url);		// Open the connection to the requested URL     // … (optional) define the request package and/or send data …     is = con.openDataInputStream();					// Read from the connection // … read from the input stream to process data … } catch (IOException ex) { } finally {     try {         if (is != null)			// Here we make sure that everything that might still be open is closed! is.close();         if (con != null) con.close();     } catch (IOException ex) {	 // This error can usually be ignored, as something else already went wrong…     } }
HyperText Transfer Protocol Information about the Andreas Jakl, 2009 18
HTTP HTTP = Hypertext Transfer Protocol Request/Response-protocol Andreas Jakl, 2009 19 Client submitsRequestHeader, Body, … Server GPRS, UMTS, WLAN, … CallbackResponseHeader, Body, …
HTTP in JavaME Similar to the logical structure: Open a connection Prepare the request message Send request + body (optional, POST only) Retrieve response Close connection (can’t be reused!) Andreas Jakl, 2009 20
HTTP Messages Andreas Jakl, 2009 21 Server
Request Method 3 request methods supported: Andreas Jakl, 2009 22
HTTP Get Andreas Jakl, 2009 23 JavaME-code: String url = "http://www.mopius.com/test.php?user=me&pwd=xxx"; HttpConnection con = (HttpConnection)Connector.open(url); con.setRequestMethod(HttpConnection.GET);	// Define method as GET conn.setRequestProperty("User-Agent", "Mozilla/4.0"); intrc = con.getResponseCode();				// Request is sent with first read-function 									// No write required! HTTP-Request received by the web-server: GET /test.php?user=me&pwd=xxx HTTP/1.1 Host: www.mopius.com User-Agent: Mozilla/4.0
HTTP Post – Form Andreas Jakl, 2009 24 JavaME-code: String url = "http://www.mopius.com/test.php"; String data = "user=me&pwd=xxx";		// Data to send, without & // Special characters have to be URL-encoded manually (e.g. " " -> "%20") HttpConnection con = (HttpConnection)Connector.open(url); con.setRequestMethod(HttpConnection.POST);		// Define method as POST con.setRequestProperty("Content-Type", "application/x-www-form-urlencoded"); con.setRequestProperty("User-Agent", "Mozilla/4.0"); con.setRequestProperty("Content-Length", Integer.toString(data.length())); OutputStreamos = con.openOutputStream(); os.write(data.getBytes()); os.close();// Request is sent when output-stream is closed intrc = con.getResponseCode();		 HTTP-Request received by the web-server: POST /test.php HTTP/1.1 Host: www.mopius.com User-Agent: Mozilla/4.0 Content-Length: 15 Content-Type: application/x-www-form-urlencoded user=me&pwd=xxx
HTTP Post – Raw Data For raw client-server communication: Set content type Create byte array, e.g. using DataOutputStream Andreas Jakl, 2009 25 con.setRequestProperty("Content-Type", "application/octet-stream"); DataOutputStream dos = new DataOutputStream( con.openOutputStream() ); dos.writeBoolean(true); dos.writeUTF("Hello World"); dos.close();
Server Response Status line contains numeric value + text Query response code: Andreas Jakl, 2009 26 intrc = con.getResponseCode(); Overview: HTTP status categories Most important status codes and the respective constants in HttpConnection
Retrieving a HTTP Response Simple version: (creates an image based on stream) Next slide: More complex version Reads response differently, depending on if the length is known Andreas Jakl, 2009 27 // ... send request ... intrc = con.getResponseCode();			// Retrieve HTTP response code if (rc == HttpConnection.HTTP_OK) {		// Response in most cases only available for OK     is = con.openDataInputStream();			// Get the data input stream     Image img = Image.createImage(is);		// Read image }
Retrieving a HTTP Response Andreas Jakl, 2009 28 // ... send request ... intrc = con.getResponseCode();			// Retrieve HTTP response code if (rc == HttpConnection.HTTP_OK) {		// Response in most cases only available for OK     is = con.openDataInputStream();			// Get the data input stream int length = (int)con.getLength();			// Get length of the response - int is enough     byte data[];     if (length > 0) {						// If the server set the content length...         data = new byte[length];			// Do a direct, more efficient read using a fixed byte array inttotalReadBytes = 0;				// is.read() might not read everything at once         while (totalReadBytes < length) {		//  continue reading until expected length is received intcurReadBytes = is.read(data, totalReadBytes, length - totalReadBytes);             if (curReadBytes == -1) break;		// Reached the end before the expected length? totalReadBytes += curReadBytes;         }     } else {							// If the length is unknown, however, use a dynamic stream ByteArrayOutputStreambos = new ByteArrayOutputStream(); intch;         while ((ch = is.read()) != -1) {			// Read each byte until a the end is reached bos.write(ch);					// Append the new character to the byte array output stream         } bos.flush();         data = bos.toByteArray();			// Create a byte array based on the stream bos.close();     } }
Sockets Low-Level Communication Andreas Jakl, 2009 29
Socket Communication Multiplayer games / applications – HTTP? Request / response might not be useful Large overhead through headers Sockets: Server listens at specific port Client connects to server Bidirectional communication possible Andreas Jakl, 2009 30
Streams / Datagrams Stream socket: Uses TCP (Connection-oriented protocol) Open – send/receive ... – Close  Delivery & order of data are guaranteed Datagram socket: Uses UDP (Record-oriented system) Datagram = chunk of data, no stream Delivery not guaranteed Faster than TCP Mobile phone: Performance issues in any case (network speed)! Andreas Jakl, 2009 31
Sockets and Java ME Similar to HTTP: Writing and Reading: Andreas Jakl, 2009 32 TCP-Connection: SocketConnection con = (SocketConnection) Connector.open(“socket://127.0.0.1:5000”); UDP-Connection: UDPDatagramConnection con = (UDPDatagramConnection)Connector.open(“datagram://127.0.0.1:5000”); InputStream is = con.openInputStream(); OutputStreamos = con.openOutputStream(); // … write and read to/from the streams … is.close(); os.close(); con.close();
Thanks for your attention That’s it! Andreas Jakl, 2009 33

More Related Content

Similar to Java ME - 07 - Generic Connection Framework, HTTP and Sockets

Socket Programming
Socket  ProgrammingSocket  Programming
Socket Programming
leminhvuong
 
15network Programming Clients
15network Programming Clients15network Programming Clients
15network Programming Clients
Adil Jafri
 
Network Programming Clients
Network Programming ClientsNetwork Programming Clients
Network Programming Clients
Adil Jafri
 
Networking and Data Access with Eqela
Networking and Data Access with EqelaNetworking and Data Access with Eqela
Networking and Data Access with Eqela
jobandesther
 
Communication in android
Communication in androidCommunication in android
Communication in android
eleksdev
 
Manual redes - network programming with j2 me wireless devices
Manual   redes - network programming with j2 me wireless devicesManual   redes - network programming with j2 me wireless devices
Manual redes - network programming with j2 me wireless devices
Victor Garcia Vara
 
Tomcat New Evolution
Tomcat New EvolutionTomcat New Evolution
Tomcat New Evolution
Allan Huang
 

Similar to Java ME - 07 - Generic Connection Framework, HTTP and Sockets (20)

Socket Programming
Socket  ProgrammingSocket  Programming
Socket Programming
 
Java bad coding practices
Java bad coding practicesJava bad coding practices
Java bad coding practices
 
Socket Programming - nitish nagar
Socket Programming - nitish nagarSocket Programming - nitish nagar
Socket Programming - nitish nagar
 
Loom Virtual Threads in the JDK 19
Loom Virtual Threads in the JDK 19Loom Virtual Threads in the JDK 19
Loom Virtual Threads in the JDK 19
 
DDD Framework for Java: JdonFramework
DDD Framework for Java: JdonFrameworkDDD Framework for Java: JdonFramework
DDD Framework for Java: JdonFramework
 
Symbian OS - Communication And Messaging
Symbian OS - Communication And MessagingSymbian OS - Communication And Messaging
Symbian OS - Communication And Messaging
 
15network Programming Clients
15network Programming Clients15network Programming Clients
15network Programming Clients
 
Network Programming Clients
Network Programming ClientsNetwork Programming Clients
Network Programming Clients
 
Networking and Data Access with Eqela
Networking and Data Access with EqelaNetworking and Data Access with Eqela
Networking and Data Access with Eqela
 
Review on the Design of Web Based SCADA Systems Based on OPC DA Protocol
Review on the Design of Web Based SCADA Systems Based on OPC DA ProtocolReview on the Design of Web Based SCADA Systems Based on OPC DA Protocol
Review on the Design of Web Based SCADA Systems Based on OPC DA Protocol
 
Java networking
Java networkingJava networking
Java networking
 
Android session-5-sajib
Android session-5-sajibAndroid session-5-sajib
Android session-5-sajib
 
Lecture 10 Networking on Mobile Devices
Lecture 10 Networking on Mobile DevicesLecture 10 Networking on Mobile Devices
Lecture 10 Networking on Mobile Devices
 
Java ME - 04 - Timer, Tasks and Threads
Java ME - 04 - Timer, Tasks and ThreadsJava ME - 04 - Timer, Tasks and Threads
Java ME - 04 - Timer, Tasks and Threads
 
Mobile Device APIs
Mobile Device APIsMobile Device APIs
Mobile Device APIs
 
Android Networking
Android NetworkingAndroid Networking
Android Networking
 
Communication in android
Communication in androidCommunication in android
Communication in android
 
Manual redes - network programming with j2 me wireless devices
Manual   redes - network programming with j2 me wireless devicesManual   redes - network programming with j2 me wireless devices
Manual redes - network programming with j2 me wireless devices
 
Java adv
Java advJava adv
Java adv
 
Tomcat New Evolution
Tomcat New EvolutionTomcat New Evolution
Tomcat New Evolution
 

More from Andreas Jakl

More from Andreas Jakl (20)

Create Engaging Healthcare Experiences with Augmented Reality
Create Engaging Healthcare Experiences with Augmented RealityCreate Engaging Healthcare Experiences with Augmented Reality
Create Engaging Healthcare Experiences with Augmented Reality
 
AR / VR Interaction Development with Unity
AR / VR Interaction Development with UnityAR / VR Interaction Development with Unity
AR / VR Interaction Development with Unity
 
Android Development with Kotlin, Part 3 - Code and App Management
Android Development with Kotlin, Part 3 - Code and App ManagementAndroid Development with Kotlin, Part 3 - Code and App Management
Android Development with Kotlin, Part 3 - Code and App Management
 
Android Development with Kotlin, Part 2 - Internet Services and JSON
Android Development with Kotlin, Part 2 - Internet Services and JSONAndroid Development with Kotlin, Part 2 - Internet Services and JSON
Android Development with Kotlin, Part 2 - Internet Services and JSON
 
Android Development with Kotlin, Part 1 - Introduction
Android Development with Kotlin, Part 1 - IntroductionAndroid Development with Kotlin, Part 1 - Introduction
Android Development with Kotlin, Part 1 - Introduction
 
Android and NFC / NDEF (with Kotlin)
Android and NFC / NDEF (with Kotlin)Android and NFC / NDEF (with Kotlin)
Android and NFC / NDEF (with Kotlin)
 
Basics of Web Technologies
Basics of Web TechnologiesBasics of Web Technologies
Basics of Web Technologies
 
Bluetooth Beacons - Bluetooth 5, iBeacon, Eddystone, Arduino, Windows 10 & More
Bluetooth Beacons - Bluetooth 5, iBeacon, Eddystone, Arduino, Windows 10 & MoreBluetooth Beacons - Bluetooth 5, iBeacon, Eddystone, Arduino, Windows 10 & More
Bluetooth Beacons - Bluetooth 5, iBeacon, Eddystone, Arduino, Windows 10 & More
 
Which new scenarios are enabled by Windows 10 for NFC, Bluetooth LE & Beacons?
Which new scenarios are enabled by Windows 10 for NFC, Bluetooth LE & Beacons?Which new scenarios are enabled by Windows 10 for NFC, Bluetooth LE & Beacons?
Which new scenarios are enabled by Windows 10 for NFC, Bluetooth LE & Beacons?
 
Mobile Test Automation
Mobile Test AutomationMobile Test Automation
Mobile Test Automation
 
Qt App Development - Cross-Platform Development for Android, iOS, Windows Pho...
Qt App Development - Cross-Platform Development for Android, iOS, Windows Pho...Qt App Development - Cross-Platform Development for Android, iOS, Windows Pho...
Qt App Development - Cross-Platform Development for Android, iOS, Windows Pho...
 
WinJS, Apache Cordova & NFC - HTML5 apps for Android and Windows Phone
WinJS, Apache Cordova & NFC - HTML5 apps for Android and Windows PhoneWinJS, Apache Cordova & NFC - HTML5 apps for Android and Windows Phone
WinJS, Apache Cordova & NFC - HTML5 apps for Android and Windows Phone
 
Nokia New Asha Platform Developer Training
Nokia New Asha Platform Developer TrainingNokia New Asha Platform Developer Training
Nokia New Asha Platform Developer Training
 
Windows Phone 8 NFC Quickstart
Windows Phone 8 NFC QuickstartWindows Phone 8 NFC Quickstart
Windows Phone 8 NFC Quickstart
 
Windows (Phone) 8 NFC App Scenarios
Windows (Phone) 8 NFC App ScenariosWindows (Phone) 8 NFC App Scenarios
Windows (Phone) 8 NFC App Scenarios
 
Windows 8 Platform NFC Development
Windows 8 Platform NFC DevelopmentWindows 8 Platform NFC Development
Windows 8 Platform NFC Development
 
NFC Development with Qt - v2.2.0 (5. November 2012)
NFC Development with Qt - v2.2.0 (5. November 2012)NFC Development with Qt - v2.2.0 (5. November 2012)
NFC Development with Qt - v2.2.0 (5. November 2012)
 
06 - Qt Communication
06 - Qt Communication06 - Qt Communication
06 - Qt Communication
 
05 - Qt External Interaction and Graphics
05 - Qt External Interaction and Graphics05 - Qt External Interaction and Graphics
05 - Qt External Interaction and Graphics
 
04 - Qt Data
04 - Qt Data04 - Qt Data
04 - Qt Data
 

Recently uploaded

Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Safe Software
 
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
?#DUbAI#??##{{(☎️+971_581248768%)**%*]'#abortion pills for sale in dubai@
 
Why Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businessWhy Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire business
panagenda
 

Recently uploaded (20)

Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...
Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...
Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
 
presentation ICT roal in 21st century education
presentation ICT roal in 21st century educationpresentation ICT roal in 21st century education
presentation ICT roal in 21st century education
 
CNIC Information System with Pakdata Cf In Pakistan
CNIC Information System with Pakdata Cf In PakistanCNIC Information System with Pakdata Cf In Pakistan
CNIC Information System with Pakdata Cf In Pakistan
 
Introduction to use of FHIR Documents in ABDM
Introduction to use of FHIR Documents in ABDMIntroduction to use of FHIR Documents in ABDM
Introduction to use of FHIR Documents in ABDM
 
AI+A11Y 11MAY2024 HYDERBAD GAAD 2024 - HelloA11Y (11 May 2024)
AI+A11Y 11MAY2024 HYDERBAD GAAD 2024 - HelloA11Y (11 May 2024)AI+A11Y 11MAY2024 HYDERBAD GAAD 2024 - HelloA11Y (11 May 2024)
AI+A11Y 11MAY2024 HYDERBAD GAAD 2024 - HelloA11Y (11 May 2024)
 
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
Why Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businessWhy Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire business
 
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...
 
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
 
WSO2's API Vision: Unifying Control, Empowering Developers
WSO2's API Vision: Unifying Control, Empowering DevelopersWSO2's API Vision: Unifying Control, Empowering Developers
WSO2's API Vision: Unifying Control, Empowering Developers
 
DEV meet-up UiPath Document Understanding May 7 2024 Amsterdam
DEV meet-up UiPath Document Understanding May 7 2024 AmsterdamDEV meet-up UiPath Document Understanding May 7 2024 Amsterdam
DEV meet-up UiPath Document Understanding May 7 2024 Amsterdam
 
[BuildWithAI] Introduction to Gemini.pdf
[BuildWithAI] Introduction to Gemini.pdf[BuildWithAI] Introduction to Gemini.pdf
[BuildWithAI] Introduction to Gemini.pdf
 
MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024
 
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
 
Corporate and higher education May webinar.pptx
Corporate and higher education May webinar.pptxCorporate and higher education May webinar.pptx
Corporate and higher education May webinar.pptx
 
ICT role in 21st century education and its challenges
ICT role in 21st century education and its challengesICT role in 21st century education and its challenges
ICT role in 21st century education and its challenges
 
Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...
 
FWD Group - Insurer Innovation Award 2024
FWD Group - Insurer Innovation Award 2024FWD Group - Insurer Innovation Award 2024
FWD Group - Insurer Innovation Award 2024
 

Java ME - 07 - Generic Connection Framework, HTTP and Sockets

  • 1. Java™Platform, Micro Edition Part 7Generic Connection Framework, HTTP and Sockets v3.0 – 06 April 2009 1 Andreas Jakl, 2009
  • 2. Disclaimer These slides are provided free of charge at http://www.symbianresources.com and are used during Java ME courses at the University of Applied Sciences in Hagenberg, Austria at the Mobile Computing department ( http://www.fh-ooe.at/mc ) Respecting the copyright laws, you are allowed to use them: for your own, personal, non-commercial use in the academic environment In all other cases (e.g. for commercial training), please contact andreas.jakl@fh-hagenberg.at The correctness of the contents of these materials cannot be guaranteed. Andreas Jakl is not liable for incorrect information or damage that may arise from using the materials. This document contains copyright materials which are proprietary to Sun or various mobile device manufacturers, including Nokia, SonyEricsson and Motorola. Sun, Sun Microsystems, the Sun Logo and the Java™ Platform, Micro Edition are trademarks or registered trademarks of Sun Microsystems, Inc. in the United States and other countries. Andreas Jakl, 2009 2
  • 3. Contents Generic Connection Framework HTTP Connections Sockets Andreas Jakl, 2009 3
  • 4. Generic Connection Framework Java SE: 150+ classes and interfaces (java.io, java.net) Bytecode-size: java.net 200kB+ Consistency: Different objects for protocols CLDC: Generic Connection Framework (GCF) Unified API for various protocols Profilesdefine protocols (MIDP, additional packages) Mandatory for MIDP 2.0:Hypertext Transfer Protocol (HTTP)Hypertext Transfer Protocol over TLS/SSL (HTTPS) Today: GCF made its way into Java SE (JSR 197) Andreas Jakl, 2009 4
  • 5. Generic Connection Framework Hierarchy of interfaces and classes to: create connections (HTTP, datagrams, ...) perform I/O GCF used by optional packages Bluetooth, Files, SmartCards, Messaging, … … might look difficult at first, but is very easy to use! Andreas Jakl, 2009 5
  • 6. Connector Single Factory-class, creates any type of connection Examples: Connector.open(“http://www.mopius.com/”); Connector.open(“socket://realreplay.mopius.com:5412”); Connector.open(“file://data.txt”); Andreas Jakl, 2009 6 Connector.open(“protocol:address;parameters”);
  • 7. Connector – Protocols Protocol is chosen automatically, based on URL GCF searches for class that implements protocol Success: Returns class that implements Connection-interface Failure: ConnectionNotFoundException HTTP is supported for sure, Sockets might be unavailable on phone / through operator Andreas Jakl, 2009 7 Example: HttpConnection con = (HttpConnection) Connector.open (“http://www.mopius.com/”);
  • 8. Connect and Transfer Short example for sockets (omits error handling): Andreas Jakl, 2009 8 // Open the connection to a URL SocketConnection con = (SocketConnection) Connector.open(“socket://127.0.0.1:5000”); // Send request DataOutputStream dos = con.openDataOutputStream(); dos.writeShort(12); dos.writeUTF(“Hello World”); dos.close(); // Receive answer DataInputStreamdis = con.openDataInputStream(); short msgId = dis.readShort(); dis.close(); // Cleanup con.close();
  • 9. GCF – High Level Andreas Jakl, 2009 9 Generic way of handlingall communication andall protocols. Generic factory class, common interface Wait for incoming stream connections Stream-based I/O Packet-based I/O GCF (without additional packages like Bluetooth)supports three types of communication Additional content-specific information (data length, content type, data encoding, …) More comfortablefunctions for protocolslike HTTP are available.
  • 10. GCF – Basic Class Diagram Andreas Jakl, 2009 10 Connection Connector ConnectionNotFoundException Generic factory class, common interface creates throws DatagramConnection InputConnection OutputConnection StreamConnectionNotifier Other subtypes… Wait for incoming stream connections Stream-based I/O Packet-based I/O creates Datagram StreamConnection ContentConnection Other subtypes… Other subtypes… Additional content-specific information (data length, content type, data encoding, …) Other subtypes… CLDC
  • 11. GCF – Basic Class Diagram Andreas Jakl, 2009 11 Connection Connector ConnectionNotFoundException creates throws DatagramConnection InputConnection OutputConnection StreamConnectionNotifier Other subtypes… creates Datagram StreamConnection ContentConnection Other subtypes… Other subtypes… Other subtypes… CLDC
  • 12. GCF – MIDP Class Diagram Andreas Jakl, 2009 12 Connection Connector ConnectionNotFoundException creates throws DatagramConnection InputConnection OutputConnection StreamConnectionNotifier Bluetooth Messaging creates Datagram StreamConnection UDPDatagramConnection CommConnection SocketConnection ContentConnection ServerSocketConnection FileConnection HttpConnection SecureConnection HttpsConnection MIDP 2.0 CLDC Optional JSRs
  • 13. URL Uniform Resource Locator (URL) Identify connection type and endpoint Andreas Jakl, 2009 13 scheme://user:password@host:port/url-path;parameters
  • 14. URL – Schemes Some of GCF connection types: Andreas Jakl, 2009 14 ... you can only be sure that HTTP(S) is available on a device!
  • 15. public void commandAction (Command command, Displayable displayable) { if (command == iCmdConnect) { con = (HttpConnection) Connector.open("http://www.mopius.com/images/mopius.png"); } } Asynchronous Connection Andreas Jakl, 2009 15 Establishing a connection in the command handling function leads to a deadlock!  The user can no longer select “Yes” or “No”. Notification message provided by WTK (console):
  • 16. Asynchronous Connection Solution: Start own thread for networking code! Andreas Jakl, 2009 16 public class MyClass extends MIDlet implements CommandListener, Runnable { // … // Process the command to connect to the network public void commandAction(Command command, Displayable displayable) { if (command == cmdConnectToWeb) { // Start connection in extra thread  commandAction-function is not blocked new Thread(this).start(); } } // Connect to the network public void run() { // … HttpConnection con = (HttpConnection)Connector.open(url); // … } }
  • 17. Error Handling Connection should always be closed ( Exception!) Andreas Jakl, 2009 17 // Define those two variables outside the try-block, so that they are known in the finally-block! SocketConnection con = null; DataInputStream is = null; try { con = (SocketConnection)Connector.open(url); // Open the connection to the requested URL // … (optional) define the request package and/or send data … is = con.openDataInputStream(); // Read from the connection // … read from the input stream to process data … } catch (IOException ex) { } finally { try { if (is != null) // Here we make sure that everything that might still be open is closed! is.close(); if (con != null) con.close(); } catch (IOException ex) { // This error can usually be ignored, as something else already went wrong… } }
  • 18. HyperText Transfer Protocol Information about the Andreas Jakl, 2009 18
  • 19. HTTP HTTP = Hypertext Transfer Protocol Request/Response-protocol Andreas Jakl, 2009 19 Client submitsRequestHeader, Body, … Server GPRS, UMTS, WLAN, … CallbackResponseHeader, Body, …
  • 20. HTTP in JavaME Similar to the logical structure: Open a connection Prepare the request message Send request + body (optional, POST only) Retrieve response Close connection (can’t be reused!) Andreas Jakl, 2009 20
  • 21. HTTP Messages Andreas Jakl, 2009 21 Server
  • 22. Request Method 3 request methods supported: Andreas Jakl, 2009 22
  • 23. HTTP Get Andreas Jakl, 2009 23 JavaME-code: String url = "http://www.mopius.com/test.php?user=me&pwd=xxx"; HttpConnection con = (HttpConnection)Connector.open(url); con.setRequestMethod(HttpConnection.GET); // Define method as GET conn.setRequestProperty("User-Agent", "Mozilla/4.0"); intrc = con.getResponseCode(); // Request is sent with first read-function // No write required! HTTP-Request received by the web-server: GET /test.php?user=me&pwd=xxx HTTP/1.1 Host: www.mopius.com User-Agent: Mozilla/4.0
  • 24. HTTP Post – Form Andreas Jakl, 2009 24 JavaME-code: String url = "http://www.mopius.com/test.php"; String data = "user=me&pwd=xxx"; // Data to send, without & // Special characters have to be URL-encoded manually (e.g. " " -> "%20") HttpConnection con = (HttpConnection)Connector.open(url); con.setRequestMethod(HttpConnection.POST); // Define method as POST con.setRequestProperty("Content-Type", "application/x-www-form-urlencoded"); con.setRequestProperty("User-Agent", "Mozilla/4.0"); con.setRequestProperty("Content-Length", Integer.toString(data.length())); OutputStreamos = con.openOutputStream(); os.write(data.getBytes()); os.close();// Request is sent when output-stream is closed intrc = con.getResponseCode(); HTTP-Request received by the web-server: POST /test.php HTTP/1.1 Host: www.mopius.com User-Agent: Mozilla/4.0 Content-Length: 15 Content-Type: application/x-www-form-urlencoded user=me&pwd=xxx
  • 25. HTTP Post – Raw Data For raw client-server communication: Set content type Create byte array, e.g. using DataOutputStream Andreas Jakl, 2009 25 con.setRequestProperty("Content-Type", "application/octet-stream"); DataOutputStream dos = new DataOutputStream( con.openOutputStream() ); dos.writeBoolean(true); dos.writeUTF("Hello World"); dos.close();
  • 26. Server Response Status line contains numeric value + text Query response code: Andreas Jakl, 2009 26 intrc = con.getResponseCode(); Overview: HTTP status categories Most important status codes and the respective constants in HttpConnection
  • 27. Retrieving a HTTP Response Simple version: (creates an image based on stream) Next slide: More complex version Reads response differently, depending on if the length is known Andreas Jakl, 2009 27 // ... send request ... intrc = con.getResponseCode(); // Retrieve HTTP response code if (rc == HttpConnection.HTTP_OK) { // Response in most cases only available for OK is = con.openDataInputStream(); // Get the data input stream Image img = Image.createImage(is); // Read image }
  • 28. Retrieving a HTTP Response Andreas Jakl, 2009 28 // ... send request ... intrc = con.getResponseCode(); // Retrieve HTTP response code if (rc == HttpConnection.HTTP_OK) { // Response in most cases only available for OK is = con.openDataInputStream(); // Get the data input stream int length = (int)con.getLength(); // Get length of the response - int is enough byte data[]; if (length > 0) { // If the server set the content length... data = new byte[length]; // Do a direct, more efficient read using a fixed byte array inttotalReadBytes = 0; // is.read() might not read everything at once while (totalReadBytes < length) { //  continue reading until expected length is received intcurReadBytes = is.read(data, totalReadBytes, length - totalReadBytes); if (curReadBytes == -1) break; // Reached the end before the expected length? totalReadBytes += curReadBytes; } } else { // If the length is unknown, however, use a dynamic stream ByteArrayOutputStreambos = new ByteArrayOutputStream(); intch; while ((ch = is.read()) != -1) { // Read each byte until a the end is reached bos.write(ch); // Append the new character to the byte array output stream } bos.flush(); data = bos.toByteArray(); // Create a byte array based on the stream bos.close(); } }
  • 29. Sockets Low-Level Communication Andreas Jakl, 2009 29
  • 30. Socket Communication Multiplayer games / applications – HTTP? Request / response might not be useful Large overhead through headers Sockets: Server listens at specific port Client connects to server Bidirectional communication possible Andreas Jakl, 2009 30
  • 31. Streams / Datagrams Stream socket: Uses TCP (Connection-oriented protocol) Open – send/receive ... – Close Delivery & order of data are guaranteed Datagram socket: Uses UDP (Record-oriented system) Datagram = chunk of data, no stream Delivery not guaranteed Faster than TCP Mobile phone: Performance issues in any case (network speed)! Andreas Jakl, 2009 31
  • 32. Sockets and Java ME Similar to HTTP: Writing and Reading: Andreas Jakl, 2009 32 TCP-Connection: SocketConnection con = (SocketConnection) Connector.open(“socket://127.0.0.1:5000”); UDP-Connection: UDPDatagramConnection con = (UDPDatagramConnection)Connector.open(“datagram://127.0.0.1:5000”); InputStream is = con.openInputStream(); OutputStreamos = con.openOutputStream(); // … write and read to/from the streams … is.close(); os.close(); con.close();
  • 33. Thanks for your attention That’s it! Andreas Jakl, 2009 33