Fetch page Midlet on a mobile phone
I am testing a simple midlet that fetches a page and prints its contents (1 line) on my mobile phone. My mobile phone is Sony Ericsson T610. The midlet freezes when launched. Anyone of you guys knows what could be the problem. Knowing that the midlet works perfectly when it runs on the Emulator.
My simple midlet code is as follows....
public class Midlet extends MIDlet {
private Display display;
String url = "http://www.javacourses.com/hello.txt";
public Midlet() {
display = Display.getDisplay(this);
}
public void startApp() {
try {
getViaStreamConnection(url);
} catch (IOException e) {
System.out.println("IOException " + e);
e.printStackTrace();
}
}
public void pauseApp() {
}
public void destroyApp(boolean unconditional) {
this.notifyDestroyed();
}
void getViaStreamConnection(String url) throws IOException
{
StreamConnection c = null;
InputStream s = null;
StringBuffer b = new StringBuffer();
TextBox t = null;
try
{
c = (StreamConnection)Connector.open(url);
s = c.openInputStream();
int ch;
while((ch = s.read()) != -1) {
b.append((char) ch);
}
System.out.println(b.toString());
t = new TextBox("Fetch Page", b.toString(), 1024, 0);
} finally
{
if(s != null) {
s.close();
}
if(c != null) {
c.close();
}
}
// display the contents of the file in a text box.
display.setCurrent(t);
}
}
Thank you :)
[1620 byte] By [
MIKE9009a] at [2007-11-27 6:38:40]

# 4
package Midlets;
import java.io.*;
import javax.microedition.io.*;
import javax.microedition.lcdui.*;
import javax.microedition.midlet.*;
import Domain.ConnectingThread;
/**
*
* @author melbadaoui
* @version
*/
public class Midlet extends MIDlet implements CommandListener{
private Display display;
String url = "http://www.javacourses.com/hello.txt";
private Form mainForm;
private Command connectCommand;
private ConnectingThread thread;
public Midlet() {
display = Display.getDisplay(this);
mainForm = new Form("Main Form");
connectCommand = new Command("Connect", Command.OK, 0);
thread = new ConnectingThread(this, url);
thread.start();
}
public void startApp()
{
mainForm.addCommand(connectCommand);
mainForm.setCommandListener(this);
display.setCurrent(mainForm);
}
public void pauseApp() {
}
public void destroyApp(boolean unconditional) {
this.notifyDestroyed();
}
public void commandAction(Command c, Displayable d)
{
if(c == connectCommand)
{
thread.go();
}
}
public void alert(String message)
{
mainForm.append(message);
display.setCurrent(mainForm);
}
}
package Domain;
import Midlets.Midlet;
import java.io.*;
import javax.microedition.io.*;
import javax.microedition.lcdui.Display;
public class ConnectingThread extends Thread
{
private Midlet myMidlet;
private String url;
private HttpConnection httpConn;
private boolean threadIsCanceled;
private String inputString;
/** Creates a new instance of ConnectingThread */
public ConnectingThread(Midlet midlet, String url)
{
myMidlet = midlet;
this.url = url;
threadIsCanceled = false;
}
public synchronized void run() {
while (!threadIsCanceled)
{
try { wait(); }
catch (InterruptedException ie) {}
if (!threadIsCanceled)
connect();
}
}
public synchronized void go() {
notify();
}
private void connect()
{
StreamConnection c = null;
InputStream s = null;
StringBuffer b = new StringBuffer();
try
{
c = (StreamConnection)Connector.open(url);
s = c.openInputStream();
byte[] khara = new byte[7];
s.read(khara);
String kharaString = new String(khara);
System.out.println(kharaString);
myMidlet.alert(kharaString);
}
catch(IOException e) { myMidlet.alert( "Exception1 - " + e.toString() + " - " + e.getMessage()); }
finally
{
try
{
if(s != null) {
s.close();
}
if(c != null) {
c.close();
}
}
catch(IOException e) { myMidlet.alert( "Exception2" ); }
}
// display the contents of the file in a text box.
}
}