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

Slides:



Advertisements
Similar presentations
Networking with Java 1. Introduction to Networking 2.
Advertisements

Socket Programming ENTERPRISE JAVA. 2 Content  Sockets  Streams  Threads  Readings.
J2ME Form Nesneleri Melih Sakarya. Connection HttpConnectionInputConnectionOutputConnectionDatagramConnectionStreamConnection Socket Connection lari.
 2002 Prentice Hall. All rights reserved. Chapter 9: Servlets Outline 9.1 Introduction 9.2 Servlet Overview and Architecture Interface Servlet and.
Lecture 6/2/12. Forms and PHP The PHP $_GET and $_POST variables are used to retrieve information from forms, like user input When dealing with HTML forms.
Common Gateway Interface (CGI). CGI is a protocol: CGI is not a programming language CGI is a protocol for the exchange of information between between.
Prepared By E. Musa Alyaman1 URLs, InetAddresses, and URLConnections Chapter 9.
16-Jun-15 HTTP Hypertext Transfer Protocol. 2 HTTP messages HTTP is the language that web clients and web servers use to talk to each other HTTP is largely.
HTML Form Processing Learning Web Design – Chapter 9, pp Squirrel Book – Chapter 11, pp
Cosc 5/4730 Blackberry: More of the GCF. Common Connections URL SchemeProtocolGCF TypeDefined by Btl2capBluetoothL2CAPConnectionJSR 82 datagramDatagramDatagramConnectionCLDC,
1 Java Networking CS , Spring 2008/9. 2 Today’s Menu Networking Basics  TCP, UDP, Ports, DNS, Client-Server Model TCP/IP in Java Sockets URL 
Liang, Introduction to Java Programming, Sixth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Chapter 34 Servlets.
Internet Programming In Java. References Java.sun.com Java552 Many of the programs shown.
Networking with Java 1: The Client Side. Introduction to Networking.
Generic Connection Framework Connection FileConnectionSocketConnectionHTTPConnection InputConnection OutputConnection StreamConnection.
Socket Communication Sockets provide an abstraction of two-point communication The two sides only read/write without concern for how data is translated.
Persistent Storage  Record Stores  mini databases – represent data as byte records  Record Management System (RMS) API  Files and Directories  FileConnection.
13-Jul-15 Sockets and URLs. 2 Sockets A socket is a low-level software device for connecting two computers together Sockets can also be used to connect.
HTTP Overview Vijayan Sugumaran School of Business Administration Oakland University.
CEG3185 Tutorial 4 Prepared by Zhenxia Zhang Revised by Jiying Zhao (2015w)
Java Programming: I/O1 Java I/O Reference: java.sun.com/docs/books/tutorial/essential/io/
1 Networking with Java. 2 Introduction to Networking.
Babak Esfandiari (based on slides by Qusay Mahmoud)
An program As a simple example of socket programming we can implement a program that sends to a remote site As a simple example of socket.
TCP/IP protocols Communication over Internet is mostly TCP/IP (Transmission Control Protocol over Internet Protocol) TCP/IP "stack" is software which allows.
Simple Web Services. Internet Basics The Internet is based on a communication protocol named TCP (Transmission Control Protocol) TCP allows programs running.
NET0183 Networks and Communications Lecture 31 The Socket API 8/25/20091 NET0183 Networks and Communications by Dr Andy Brooks Lecture powerpoints from.
Appendix F: Network Programming in Java ©SoftMoore ConsultingSlide 1.
Simple Web Services. Internet Basics The Internet is based on a communication protocol named TCP (Transmission Control Protocol) TCP allows programs running.
TCP/IP Protocol Suite 1 Chapter 22 Upon completion you will be able to: World Wide Web: HTTP Understand the components of a browser and a server Understand.
Application Protocols: HTTP CSNB534 Semester 2, 2007/2008 Asma Shakil.
Maryam Elahi University of Calgary – CPSC 441.  HTTP stands for Hypertext Transfer Protocol.  Used to deliver virtually all files and other data (collectively.
An program As a simple example of socket programming we can implement a program that sends to a remote site As a simple example of socket.
COMP 321 Week 7. Overview HTML and HTTP Basics Dynamic Web Content ServletsMVC Tomcat in Eclipse Demonstration Lab 7-1 Introduction.
CSCI 7010 UGA March 23 rd,  BES/MDS  TCP/IP  BIS  WiFi  WAP 2.0  WAP 1.0.
J2EE training: 1 Course Material Usage Rules PowerPoint slides for use only in full-semester, for-credit courses at degree-granting.
MIDP Programming Networking Khanh Le. / 2 of Chapter Objectives The CLDC Streams Model Generic Connection Framework (GCF) Supported Protocols Creating.
1 Week 12 l Overview of Streams and File I/O l Text File I/O Streams and File I/O.
Li Tak Sing COMPS311F. Case study: consumers and producers A fixed size buffer which can hold at most certain integers. A number of producers which generate.
Lab 2 Primer Assignment 3 Structure File I/O More parsing and HTTP Formatting.
Operating Systems Lesson 12. HTTP vs HTML HTML: hypertext markup language ◦ Definitions of tags that are added to Web documents to control their appearance.
PROG Mobile Java Application Development PROG Mobile Java Application Development Data Storage, Continued.
MIDP Programming Networking. Chapter Objectives The CLDC Streams Model Generic Connection Framework (GCF) Supported Protocols Creating a Connection Review.
Project Scenario for OpX. High-level Overview In the Test GUI Controller, opXController, for the opX command there is conceptually a call to an appropriate.
Prepared by Dr. Jiying Zhao University of Ottawa Canada.
UNIT-6. Basics of Networking TCP/IP Sockets Simple Client Server program Multiple clients Sending file from Server to Client Parallel search server.
Java Programming II Java Network (I) Java Programming II.
MT311 Java Application Development and Programming Languages Li Tak Sing ( 李德成 )
Generic Connection Framework Connection FileConnectionSocketConnectionHTTPConnection InputConnection OutputConnection StreamConnection.
Server-side http General form of http response/request GET request method POST request method Responses Servlet support.
Simple Web Services. Internet Basics The Internet is based on a communication protocol named TCP (Transmission Control Protocol) TCP allows programs running.
Access to Remote Data ( ) Frank Ducrest.
Servlets.
Object Writing in files
Network Programming Introduction
Sockets and URLs 17-Sep-18.
Sockets and URLs 13-Nov-18.
Generating the Server Response: HTTP Status Codes
Dr. John P. Abraham Professor UTRGV eCommerce CSCI 6314
Clients and Servers 19-Nov-18.
URL in Java C343 Lab (Week 13).
Clients and Servers 1-Dec-18.
Networking.
Sockets and URLs 3-Dec-18.
Servlet APIs Every servlet must implement javax.servlet.Servlet interface Most servlets implement the interface by extending one of these classes javax.servlet.GenericServlet.
Lecture 5: Functions and Parameters
Web APIs API = Application Programmer Interface
Uniform Resource Locator: URL
Presentation transcript:

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

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

Programming Handheld and Mobile devices 3

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

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.

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

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

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.

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 .

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(); }

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(); }

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(); }

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 :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(); }

Programming Handheld and Mobile devices 14 HttpConnection Example 1 public class HttpTest extends MIDlet { private Display display; String url = " 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…`

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 :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 ); }

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 ); }

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)

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