J2me network connection
I've read in many places that the network connection in a j2me application should be done on a separate thread. Is it a necessity or a good one?
I'm asking this because I couldn't find anywhere that this should be done in a separate thread. Also, when I wrote a simple application to fetch an image over the network and display it on the screen (without using a stream) it didn't work. When I changed the same to use a separate thread, it worked. I'm not sure if this only works because I changed it to a separate thread as I made many more changes in the code.
Can anyone confirm?
Edit: If running in a separate thread is not necessary, can someone please tell me why below simple piece of code doesn't work?
He comes to the stage when the emulator asks: "Can I connect to the network." Whether I press yes or no, the screen does not change.
public class Moo extends MIDlet {
protected void destroyApp(boolean arg0) throws MIDletStateChangeException {
// TODO Auto-generated method stub
}
protected void pauseApp() {
}
protected void startApp() throws MIDletStateChangeException {
Display display = Display.getDisplay(this);
MyCanvas myCanvas = new MyCanvas();
display.setCurrent(myCanvas);
myCanvas.repaint();
}
class MyCanvas extends Canvas {
protected void paint(Graphics graphics) {
try {
Image bgImage = Image.createImage(getWidth(), getHeight());
HttpConnection httpConnection = (HttpConnection) Connector
.open("https://stackoverflow.com/content/img/so/logo.png");
Image image = Image.createImage(httpConnection
.openInputStream());
bgImage.getGraphics().drawImage(image, 0, 0, 0);
httpConnection.close();
graphics.drawImage(bgImage, 0, 0, 0);
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
Edit . I got an answer for the code here .
Edit: I've spawned a separate question about this here .
a source to share
The problem is that you are trying to do the work on the thread that is responsible for starting the UI. If you are not using a separate thread, then that UI thread is waiting while you are doing your work and cannot handle any other updates to your UI! so yeah, you really shouldn't be doing any significant work in the event handlers as you need to manage there quickly.
a source to share
I agree with Sean, but not necessarily for your network connection to be on a separate thread, but just in best practice. I think it was probably by accident that the connection was working correctly by moving it to a separate thread. Anyway, if you want to provide visual feedback to the user during the connection (which you are probably using given the lag mismatch that users may experience on the mobile network), you must have network processing on a separate thread.
a source to share
You don't need to make network connections in a new thread, but in practice you will find that this is almost always a good idea, as network activity can block and leave your application irresponsible.
This is an old article but it talks about some issues related to the network interface and user experience.
a source to share