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]
# 1
sure thing man, this is because you have not threaded your application. you must perform all of your network I/O on a separate thread, so that you dont lock your UI thread (exactly what's happening to you)
pandora_fooa at 2007-7-12 18:07:26 > top of Java-index,Java Mobility Forums,Java ME Technologies...
# 2

I tried using a seperate thread to establish the connection but it is still not working. What's actually happening is that when it opens the connection my phone shows that it is accessing the web and then it freezes for 15 seconds then an IOException is thrown. When the exception is thrown the phone shows that it disconnected from the web.

The internet works perfectly on my mobile phone. I can access my gmail account from my phone and check my mail.

Can anyone help please.

MIKE9009a at 2007-7-12 18:07:26 > top of Java-index,Java Mobility Forums,Java ME Technologies...
# 3
please post the new (threaded) code and exception, I'm sure there's a good explanation :)
pandora_fooa at 2007-7-12 18:07:26 > top of Java-index,Java Mobility Forums,Java ME Technologies...
# 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.

}

}

MIKE9009a at 2007-7-12 18:07:26 > top of Java-index,Java Mobility Forums,Java ME Technologies...
# 5
There you go :)
MIKE9009a at 2007-7-12 18:07:26 > top of Java-index,Java Mobility Forums,Java ME Technologies...
# 6

One quick thing in the new code is, you should start the new thread in "startApp()" not in the midlet constructor. The UI may not have been initialized soon enough if you start that thread in the constructor.

so try that, if its still throwing an error, paste the exception stacktrace here

pandora_fooa at 2007-7-12 18:07:26 > top of Java-index,Java Mobility Forums,Java ME Technologies...
# 7

:( still not working. It throws java.io.IOException when it reaches "s = c.openInputStream();". And I don't think I could print the StackTrace because the problem occurs when I run it on my sony ericsson T610. It works perfectly on the Emulator.

I think the problem is with my mobile phone, I read something about changing the WAP profile so that it connects using internet not the service provider WAP. However after I did this, no exception is thrown but the thread freezes on "s = c.openInputStream();"

MIKE9009a at 2007-7-12 18:07:26 > top of Java-index,Java Mobility Forums,Java ME Technologies...
# 8
Could someone help me out, please.
MIKE9009a at 2007-7-12 18:07:26 > top of Java-index,Java Mobility Forums,Java ME Technologies...
# 9
Well well well.... ur program with absolutely no changes is working fine in my Nokia N72. It shows a text 'welcome' in the screen as it is showing in the emulator. And I think this is all it intended to do right now.So what do u say ?
find_suvro@SDNa at 2007-7-12 18:07:26 > top of Java-index,Java Mobility Forums,Java ME Technologies...
# 10
That's exactly what it should do, and that's what I am getting on my emulator. The problem is that it is only working on Nokia phones. I tried a motorola and a sony ericsson and they both didn't work.Do you have any idea what might be the problem.
MIKE9009a at 2007-7-12 18:07:26 > top of Java-index,Java Mobility Forums,Java ME Technologies...
# 11

Ref lnk : http://forum.java.sun.com/thread.jspa?messageID=9728145

Along with the gateway also check that there should be some special settings for Java apps in SE and probably also in Motorola.

For SE check for network access in the application settings under the connections tab or some thing like that.

For Motorola pls chk it with some experienced guy in Moto... I haven't given any try on any Motorola phone so far. :-D

find_suvro@SDNa at 2007-7-12 18:07:26 > top of Java-index,Java Mobility Forums,Java ME Technologies...