Creating an HTTP Connection in MIDlet: Java Example by Markus A. Wolf (Univ. of Greenwich), Study notes of Mobile Computing

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

2010/2011

Uploaded on 09/08/2011

rossi46
rossi46 🇬🇧

4.5

(10)

313 documents

1 / 37

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Lecture 6
Java ME – Communicating using
HTTP
S.Kapetanakis & Markus A. Wolf
Based on material from Gill Windall
1
Application Development
for Mobile Devices
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a
pf1b
pf1c
pf1d
pf1e
pf1f
pf20
pf21
pf22
pf23
pf24
pf25

Partial preview of the text

Download Creating an HTTP Connection in MIDlet: Java Example by Markus A. Wolf (Univ. of Greenwich) and more Study notes Mobile Computing in PDF only on Docsity!

Lecture 6

Java ME – Communicating using

HTTP

S.Kapetanakis & Markus A. Wolf

Based on material from Gill Windall

1 Application Development for Mobile Devices

Markus A. Wolf

Contents

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

Some key classes

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 {

  • (^) The code to run in the separate Thread must go in a method called run() e.g. public void run() { // code to connect to the web server goes here }
  • (^) To start the Thread you create it and call start() e.g. Thread t = new Thread(this); t.start(); 7

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

Reading the response from an

HTTP request

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(""); int endOfTitle = page.toLowerCase().indexOf(""); if ((endOfTitle > 0) && (startOfTitle > 0) && (endOfTitle > startOfTitle)) { return page.substring(startOfTitle + 7, endOfTitle); } return null; }

  • (^) this method "parses" the HTML page looking for and extracts and returns everything between <title> and GetPageTitle.java 5 of 5^19

Markus A. Wolf

Sending data to a web application

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