Presentation is loading. Please wait.

Presentation is loading. Please wait.

Programming Handheld and Mobile devices 1 Programming of Handheld and Mobile Devices Lecture 15 IO Using http Rob Pooley

Similar presentations


Presentation on theme: "Programming Handheld and Mobile devices 1 Programming of Handheld and Mobile Devices Lecture 15 IO Using http Rob Pooley"— Presentation transcript:

1 Programming Handheld and Mobile devices 1 Programming of Handheld and Mobile Devices Lecture 15 IO Using http Rob Pooley rjp@macs.hw.ac.uk

2 Programming Handheld and Mobile devices 2 Overview Either a direct TCP/IP connection may be used Or a gateway The application should not be aware of this

3 Programming Handheld and Mobile devices 3

4 4 javax.microedition.io This interface defines the necessary methods and constants for an HTTP connection. HTTP is a request-response protocol in which the parameters of request must be set before the request is sent. The connection exists in one of three states: –Setup, in which the connection has not been made to the server. –Connected, in which the connection has been made, request parameters have been sent and the response is expected. –Closed, in which the connection has been closed and the methods will throw an IOException if called. Connected SetupClosed

5 Programming Handheld and Mobile devices 5 Setup The following methods may be invoked only in the Setup state: –setRequestMethod –setRequestProperty The transition from Setup to Connected is caused by any method that requires data to be sent to or received from the server.

6 Programming Handheld and Mobile devices 6 Methods causing transition to Connected state –openInputStream –openOutputStream –openDataInputStream –openDataOutputStream –getLength –getType –getEncoding –getHeaderField –getResponseCode –getResponseMessage –getHeaderFieldInt –getHeaderFieldDate –getExpiration –getDate –getLastModified –getHeaderField –getHeaderFieldKey

7 Programming Handheld and Mobile devices 7 Invoked while the connection is open. –close –getRequestMethod –getRequestProperty –getURL –getProtocol –getHost –getFile –getRef –getPort –getQuery

8 Programming Handheld and Mobile devices 8 Methods GET and POST in HTML forms In HTML, one can specify two different submission methods for a form.HTMLform The method is specified inside a FORM element, using the METHOD attribute.FORM element The difference between METHOD="GET" (the default) and METHOD="POST" is primarily defined in terms of form data encoding. The official recommendations say that "GET" should be used if and only if the form processing is idempotent, which typically means a pure query form. Generally it is advisable to do so. There are, however, problems related to long URLs and non-ASCII character repertoires which can make it necessary to use "POST" even for idempotent processing.

9 Programming Handheld and Mobile devices 9 Key differences between "GET" and "POST" The HTML specifications technically define the difference –"GET" means that form data is to be encoded (by a browser) into a URLURL –"POST" means that the form data is to appear within a message body. Put another way –"GET" is basically for just getting (retrieving) data –"POST" may involve anything, like storing or updating data, or ordering a product, or sending E-mail.

10 Programming Handheld and Mobile devices 10 Example using StreamConnection void getViaStreamConnection(String url) throws IOException { StreamConnection c = null; InputStream s = null; try { c = (StreamConnection)Connector.open(url); s = c.openInputStream(); int ch; while ((ch = s.read()) != -1) {... } } finally { if (s != null) s.close(); if (c != null) c.close(); }

11 Programming Handheld and Mobile devices 11 Example using ContentConnection void getViaContentConnection(String url) throws IOException { ContentConnection c = null; InputStream is = null; try { c = (ContentConnection)Connector.open(url); int len = (int)c.getLength(); if (len > 0) { is = c.openInputStream(); byte[] data = new byte[len]; int actual = is.read(data);... } else { int ch; while ((ch = is.read()) != -1) {... } } finally { if (is != null) is.close(); if (c != null) c.close(); }

12 Programming Handheld and Mobile devices 12 Example using HttpConnection void getViaHttpConnection(String url) throws IOException { HttpConnection c = null; InputStream is = null; try { c = (HttpConnection)Connector.open(url); // Getting the InputStream will open the // connection and read the HTTP headers. // They are stored until requested. is = c.openInputStream(); // Get the ContentType String type = c.getType(); // Get the length and process the data int len = (int)c.getLength(); if (len > 0) { byte[] data = new byte[len]; int actual = is.read(data);... } else { int ch; while ((ch = is.read()) != -1) {... } } finally { if (is != null) is.close(); if (c != null) c.close(); }

13 Programming Handheld and Mobile devices 13 Example using POST with HttpConnection void postViaHttpConnection(String url) throws IOException { HttpConnection c = null; InputStream is = null; OutputStream os = null; try { c = (HttpConnection)Connector.open(url); // Set the request method and headers c.setRequestMethod(HttpConnection.POST); c.setRequestProperty("If-Modified-Since", "29 Oct 1999 19:43:31 GMT"); c.setRequestProperty("User-Agent", "Profile/MIDP-1.0 Configuration/CLDC-1.0"); c.setRequestProperty("Content-Language", "en-US"); // Getting the output stream may flush the headers os = c.openOutputStream(); os.write("LIST games\n".getBytes()); os.flush(); // Optional, openInputStream will flush // Opening the InputStream will open the connection // and read the HTTP headers. They are stored until // requested. is = c.openInputStream(); // Get the ContentType String type = c.getType(); processType(type); // Get the length and process the data int len = (int)c.getLength(); if (len > 0) { byte[] data = new byte[len]; int actual = is.read(data); process(data); } else { int ch; while ((ch = is.read()) != -1) { process((byte)ch); } } finally { if (is != null) is.close(); if (os != null) os.close(); if (c != null) c.close(); }

14 Programming Handheld and Mobile devices 14 HttpConnection Example 1 public class HttpTest extends MIDlet { private Display display; String url = "http://developer.java.sun.com/cgi-bin/getgrade.cgi?name=1820"; public HttpTest() { display = Display.getDisplay( this ); } public void startApp() { try { getGrade( url ); // use HTTP.GET getGradeWithPost( url ); // use HTTP.POST } catch( IOException e ) { e.printStackTrace(); } // continued…`

15 Programming Handheld and Mobile devices 15 HttpConnection Example 2 void getGrade( String url ) throws IOException { HttpConnection c = null; StringBuffer b = new StringBuffer(); try { c = ( HttpConnection )Connector.open( url ); c.setRequestMethod( HttpConnection.GET ); c.setRequestProperty( "IF-Modified-Since", "10 Nov 2000 17:29:12 GMT" ); c.setRequestProperty( "User-Agent", "Profile/MIDP-1.0 Configuration/CLDC-1.0" ); c.setRequestProperty( "Content-Language", "en-CA" ); InputStream is = c.openDataInputStream(); while( ( int ch = is.read() ) != -1 ) { b.append( (char) ch ); TextBox t = new TextBox("Grades",b.toString(),1024,0); } } finally { if ( is != null )is.close(); if ( c != null ) c.close(); } display.setCurrent( t ); }

16 Programming Handheld and Mobile devices 16 HttpConnection Example 3 void getGradeWithPost( String url ) { try { c = ( HttpConnection )Connector.open( url ); c.setRequestMethod( HttpConnection.POST ); // setRequestProperty as in the previous slide OutputStream os = c.openOutputStream(); String str = "?idnum=182016"; byte postmsg[] = str.getBytes(); for int( i = 0; i < postmsg.length; i++ ) os.writeByte( postmsg[i] ); os.flush(); InputStream is = c.openDataInputStream(); while( ( int ch = is.read() ) != -1 ) { b.append( (char) ch ); TextBox t = new TextBox("Grades",b.toString(),1024,0); } } finally { /* as per previous slide */ } display.setCurrent( t ); }

17 Programming Handheld and Mobile devices 17 Simplified Stream Methods on Connector Please note the following: The Connector class defines the following convenience methods for retrieving an input or output stream directly for a specified URL: –InputStream openDataInputStream(String url) –DataInputStream openDataInputStream(String url) –OutputStream openOutputStream(String url) –DataOutputStream openDataOutputStream(String url)

18 Programming Handheld and Mobile devices 18 Methods which then cannot be used getRequestMethod() setRequestMethod() getRequestProperty() setRequestProperty() getLength() getType() getEncoding() getHeaderField() getResponseCode() getResponseMessage() getHeaderFieldInt getHeaderFieldDate getExpiration getDate getLastModified getHeaderField getHeaderFieldKey


Download ppt "Programming Handheld and Mobile devices 1 Programming of Handheld and Mobile Devices Lecture 15 IO Using http Rob Pooley"

Similar presentations


Ads by Google