podczas uruchomionego midletu jezeli chce polaczyc sie z internetem w celu pobrania danych z pliku, po zadanym pytaniu jakie widac na screenie:
http://kolos.math.uni.lodz.pl/~bart/screen2.jpg
klikajac YES, midlet nie przechodzi dalej tylko konsola wyrzuca w tle warning:
Warning: To avoid potential deadlock, operations that may block, such as
networking, should be performed in a different thread than the
commandAction() handler.
kod programu zwiazanego z HttpConnection
/*
* live.java
*
* Created on 12 styczeń 2008, 02:18
*/
package futbol24;
import java.io.IOException;
import javax.microedition.lcdui.*;
import javax.microedition.io.*; // do laczenia sie z serwerem
import javax.microedition.midlet.*;
import java.io.*;
/**
*
* @author bart
* @version
*/
public class DataTransfer {
private Form tForm;
public String httpAddress = ""; // url where the file is set
String st = new String();
String[] getLine(InputStream is) {
String[] line = new String[5];
line[0] = line[1] = line[2] = line[3] = line[4] = "";
int i = 0;
try {
byte b[] = new byte[1];
int end;
end = is.read(b);
if( end != -1 ) {
while ((new String(b).equals("^") == false) && end != -1) {
if (new String(b).equals("@")) {
i = 1;
end = is.read(b);
continue;
} else if (new String(b).equals("#")) {
i = 2;
end = is.read(b);
continue;
} else if (new String(b).equals("$")) {
i = 3;
end = is.read(b);
continue;
} else if (new String(b).equals("%")) {
i = 4;
end = is.read(b);
continue;
}
line[i] += new String(b);
end = is.read(b);
}
}
} catch (IOException ex) {
ex.printStackTrace();
}
return line;
}
public String getText() throws IOException {
if (tForm == null) {
HttpConnection sc = (HttpConnection)Connector.open("http://kolos.math.uni.lodz.pl/~bart/tekstowy.txt");
InputStream is = sc.openInputStream();
boolean flag = true;
String[] line = new String[5];
int a = 0;
while(flag) {
line = this.getLine(is);
if(line[1].equals("end")) {
flag = false;
break;
} else {
this.st += line[0];
}
}
is.close();
sc.close();
}
return st;
}
}
oraz wywolanie w innej klasie, jedna z metod:
public TextField getTextField1() {
if (textField1 == null) {
// write pre-init user code here
textField1 = new TextField("textField1", null, 32, TextField.ANY);
this.dt.httpAddress = "http://kolos.math.uni.lodz.pl/~bart/tekstowy.txt";
String tex = new String();
try {
tex = this.dt.getText();
} catch (IOException ex) {
ex.printStackTrace();
}
textField1.setString(tex);
// write post-init user code here
}
return textField1;
}