





























Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
A java example of creating an http connection in a midlet by markus a. Wolf from the university of greenwich. It covers creating a separate thread, making an http connection, handling exceptions, and extracting page titles. The example demonstrates how to send data via get and post methods and discusses the use of cookies for stateful dialogues.
Typology: Study notes
1 / 37
This page cannot be seen from the preview
Don't miss anything!






























1 Application Development for Mobile Devices
Markus A. Wolf
Background connection types need for threads key classes Basic techniques Making an HTTP connection Reading the response to an HTTP request Sending data to a Web Application via GET Sending data via POST Stateful sessions 2
Markus A. Wolf A common architecture MIDlet application rms RecordStore web server Serverside program e.g. Servlet, JSP, ASP, PHP, Perl Enterprise database e.g. Oracle, SQL Server Intermittent HTTP connection 4
Markus A. Wolf javax.microedition.io Connection HttpConnection DatagramConnection HttpsConnection CommConnection StreamConnection SocketConnection ContentConnection Connector
java.io InputStream OutputStream 5
Markus A. Wolf Creating a separate Thread The MIDlet must implement the interface Runnable e.g. public class GetWebPageSize extends MIDlet implements CommandListener, Runnable {
Markus A. Wolf Making an HTTP Connection HttpConnection hc = null; try { hc = (HttpConnection)Connector.open("http://www.gre.ac.uk"); // process the response here hc.close(); } catch(Exception e) { System.err.println("Problem connecting to server"); } call Connector.open() to open the connection the connection string - in this case a URL "cast" the object returned to HttpConnection remember to close the connection to avoid wasting resources 8
Markus A. Wolf import javax.microedition.io.; import javax.microedition.midlet.; import javax.microedition.lcdui.*; public class GetWebPageSize extends MIDlet implements CommandListener, Runnable { private Form mForm; private TextField txtUrl; private StringItem strResult; public GetWebPageSize() { mForm = new Form("Get information via HTTP"); txtUrl = new TextField("Enter URL ", getAppProperty("default-URL"), 60, TextField.URL); strResult = new StringItem("", ""); mForm.append(txtUrl); mForm.append(strResult); mForm.addCommand(new Command("Exit", Command.EXIT, 0)); mForm.addCommand(new Command("Get page size", Command.SCREEN, 0)); mForm.setCommandListener(this); } GetWebPageSize.java page 1 of 3^10
Markus A. Wolf protected void startApp() { Display mDisplay = Display.getDisplay(this); mDisplay.setCurrent(mForm); } protected void destroyApp(boolean param) {} protected void pauseApp() {} public void commandAction(Command c, Displayable s) { if (c.getCommandType() == Command.EXIT) { notifyDestroyed(); } else if (c.getLabel().equals("Get page size")) { Thread t = new Thread(this); t.start(); } } GetWebPageSize.java page 2 of 3^11
Markus A. Wolf
We can ask the HttpConnection object for an InputStream which allows us to read the body of the Http response as a stream of bytes hc = (HttpConnection)Connector.open(url); ..... InputStream inStream = hc.openInputStream(); byte [] page = new byte[length]; inStream.read(page); String strPage = new String(page); ask the HttpConnection for an InputStream set up a buffer to read the web page into read the page from the stream convert to a String 13
Markus A. Wolf Reading data from a web page GetPageTitle.java 14
Markus A. Wolf protected void startApp() { Display mDisplay = Display.getDisplay(this); mDisplay.setCurrent(mForm); } public void commandAction(Command c, Displayable s) { if (c.getCommandType() == Command.EXIT) { notifyDestroyed(); } else if (c.getLabel().equals("Get title")) { Thread t = new Thread(this); t.start(); } } protected void destroyApp(boolean param) {} protected void pauseApp() {} GetPageTitle.java 2 of 5^16
Markus A. Wolf public void run() { strResult.setText("trying to connect ...."); String url = txtUrl.getString(); HttpConnection hc = null; int length = 0; int responseCode = 0; String pageTitle = "unknown"; try { hc = (HttpConnection)Connector.open(url); responseCode = hc.getResponseCode(); if (responseCode == HttpConnection.HTTP_OK) { length = (int) hc.getLength(); if (length > 0) { InputStream inStream = hc.openInputStream(); byte [] page = new byte[length]; inStream.read(page); String strPage = new String(page); pageTitle = extractTitle(strPage); if (pageTitle != null) { strResult.setText(pageTitle); } GetPageTitle.java 3 of 5^17
Markus A. Wolf public String extractTitle(String page) { int startOfTitle = page.toLowerCase().indexOf("
Markus A. Wolf
As you know HTTP can send data to a server-side application in a GET or POST message GET data passed in the HTTP header appended to the URL e.g. http://www.xxx.yyy.uk/webapp?name=gill&age= limit on the amount of data sent POST data sent in the body of the HTTP message no limit on the amount of data sent GET is considerably easier to use for MIDlets 20