Google

Saturday, December 6, 2008

HttpConnection Class

Read this:

The HttpConnection Class by Yu Feng


Then modified my code.

The code below works for Z610i and on my PC emulator,

but fails on my T610 due to failure to open stream:

is = hc.openInputStream();

The commented out part also works on Z610i and
PC Emulator and also fails on my T610 due
to failure to open stream:

in = conn.openInputStream();


package MyMobilePractice2;

import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
import java.io.*;
import javax.microedition.io.*;
import java.util.*;



public class Fortune extends MIDlet implements CommandListener {
private Command exitCommand, nextCommand;
private Display display;
private Form screen;
private StringItem fortuneItem;
private Vector fortunes;

public Fortune() {
// Get the Display object for the MIDlet
display = Display.getDisplay(this);
// Create the Exit and Next commands
exitCommand = new Command("Exit", Command.ITEM, 2);
nextCommand = new Command("Next", Command.ITEM, 2);
// Create the main screen form
screen = new Form("Fortune of the Day");
fortuneItem = new StringItem("", "Reading fortunes...");
screen.append(fortuneItem);
// Set the Exit and Next commands for the screen
screen.addCommand(exitCommand);
screen.addCommand(nextCommand);
screen.setCommandListener(this);
// Create the fortunes vector
fortunes = new Vector();
}
public void startApp() throws MIDletStateChangeException {
// Set the current display to the location screen
display.setCurrent(screen);
// Initialize the fortunes vector
readFortunes();
// Show the first random fortune
showFortune();
}
public void pauseApp() {
}
public void destroyApp(boolean unconditional) {
}
public void commandAction(Command c, Displayable s) {
if (c == exitCommand) {
destroyApp(false);
notifyDestroyed();
}
else if (c == nextCommand) {
// Show the next random fortune
showFortune();
}
}

private void readFortunes(){
StringBuffer data = new StringBuffer();
HttpConnection hc= null;
InputStream is=null;
try{
hc=(HttpConnection)Connector.open("http://putyourwebsite.here/Fortunes.html");
}
catch(Exception e){
Alert httpAlert=new Alert("HTTP","fail",null,AlertType.ERROR);
httpAlert.setTimeout(Alert.FOREVER);
display.setCurrent(httpAlert);
return;
}
try{
is = hc.openInputStream();
}
catch(Exception ex){
Alert streamAlert=new Alert("STREAM","fail",null,AlertType.ERROR);
streamAlert.setTimeout(Alert.FOREVER);
display.setCurrent(streamAlert);
return;
}

// Read a line at a time from the input stream
int ch;
boolean done = false;
try{
while ((ch = is.read()) != -1) {
if (ch != '\n') {
// Read the line a character at a time
data.append((char)ch);
}
else {
// Add the fortune to the fortunes vector
fortunes.addElement(data.toString());
// Clear the string for the next line
data = new StringBuffer();
}
}
}
catch(Exception e){}

}

// private void readFortunes() {
// StreamConnection conn = null;
// InputStream in = null;
// StringBuffer data = new StringBuffer();
// try {
// // Open the HTTP connection
// conn = (StreamConnection)Connector.open("http://192.168.20.3/Fortunes.txt");
// // Obtain an input stream for the connection
// in = conn.openInputStream();
// // Read a line at a time from the input stream
// int ch;
// boolean done = false;
// while ((ch = in.read()) != -1) {
// if (ch != '\n') {
// // Read the line a character at a time
// data.append((char)ch);
// }
// else {
// // Add the fortune to the fortunes vector
// fortunes.addElement(data.toString());
// // Clear the string for the next line
// data = new StringBuffer();
// }
// }
// }
// catch (IOException e) {
// System.err.println("The connection could not be established.");
// }
// }
//
private void showFortune() {
// Check to make sure the fortunes vector isn’t empty
if (!fortunes.isEmpty()) {
// Create and seed the random number generator
Random rand = new Random(Calendar.getInstance().getTime().getTime());
// Set a random fortune
int fortuneNum = Math.abs(rand.nextInt()) % fortunes.size();
fortuneItem.setText((String)fortunes.elementAt(fortuneNum));
}
else
fortuneItem.setText("No fortune!");
}
}