Folks...
I used a code I got from a Sun's tutorial to make my mobile phone fetch an internet page reading a file. The code is down under.
Okay. I developed the code using JBuilder 9 and it create two files: a JAR and a JAD.
What is JAD for?? Must I put it in my mobile phone, too???
So... the program runned perfectly in the JBuilder's emulator, however when I downloaded it to the mobile phone Nokia 7650 the program crashed saying that a error has ocurred and that's why the program was going to exit. =(
Did anyone see this happened??? It's weird because it works perfectly in the emulator!!! If anyone knows the answer I will be very thankful.
Follows the source code:
package monitoracao;
import java.io.*;
import javax.microedition.midlet.*;
import javax.microedition.io.*;
import javax.microedition.lcdui.*;
/**
* Um MIDlet que abre uma pagina usando uma HttpConnection.
*/
public class MIDlet1 extends MIDlet {
private Display display;
private String url = "http://www.javacourses.com/hello.txt";
public MIDlet1() {
display = Display.getDisplay(this);
try {
jbInit();
}
catch(Exception e) {
e.printStackTrace();
}
}
/**
* Isso sera chamado quando This will be invoked when we activate the MIDlet.
*/
public void startApp() {
// Use the specified URL is overriden in the descriptor
try {
downloadPage(url);
} catch(IOException e) {
// handle the exception
}
}
private void downloadPage(String url) throws IOException {
StringBuffer b = new StringBuffer();
InputStream is = null;
HttpConnection c = null;
TextBox t = null;
try {
long len = 0 ;
int ch = 0;
c = (HttpConnection)Connector.open(url);
is = c.openInputStream();
len =c.getLength() ;
if ( len != -1) {
// Read exactly Content-Length bytes
for (int i =0 ; i < len ; i++ )
if ((ch = is.read()) != -1)
b.append((char) ch);
} else {
// Read till the connection is closed.
while ((ch = is.read()) != -1) {
len = is.available() ;
b.append((char)ch);
}
}
t = new TextBox("Monit. Fatura", b.toString(), 1024, 0);
} finally {
is.close();
c.close();
}
display.setCurrent(t);
}
/**
* Pause, discontinue....
*/
public void pauseApp() {
}
/**
* Destroy must cleanup everything.
*/
public void destroyApp(boolean unconditional) {
}
private void jbInit() throws Exception {
}
}
Yours Truly,
Carlos
carlos_online@ig.com.br

Reply With Quote


