• Post Reply Bookmark Topic Watch Topic
  • New Topic

Re: Problem about HTTP connection  RSS feed

 
Ranch Hand
Posts: 104
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dear all,
I am using J2ME Wireless Toolkit 2.0 to develop a application using HTTP connection to communicate with a server. I encountered a problem that cause my application did not work and the Console displayed the following message:
Warning: To avoid potential deadlock, operations that may block, such as
networking, should be performed in a different thread than the
commandAction() handler.
The source code of the application is shown below:
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
import javax.microedition.io.*;
import java.io.*;
public class HTTPMIDlet extends MIDlet implements CommandListener {
private static String defaultURL =
"http://www.comp.polyu.edu.hk/~csstlee/hello.txt";
// GUI component for user to enter web url
private Display myDisplay = null;
private Form mainScreen;
private TextField requestField;
// GUI component for displaying web page content
private Form resultScreen;
private StringItem resultField;
// the "send" button used on mainScreen
Command sendCommand = new Command("SEND", Command.OK, 1);
// the "back" button used on resultScreen
Command backCommand = new Command("BACK", Command.OK, 1);
public HTTPMIDlet(){
// initializing the GUI components for entering web urls
myDisplay = Display.getDisplay(this);
mainScreen = new Form("Type in a URL:");
requestField =
new TextField(null, defaultURL,
100, TextField.URL);
mainScreen.append(requestField);
mainScreen.addCommand(sendCommand);
mainScreen.setCommandListener(this);
}
public void startApp() {
myDisplay.setCurrent(mainScreen);
}
public void pauseApp() {
}
public void destroyApp(boolean unconditional) {
}
public void commandAction(Command c, Displayable s) {
// when user clicks on "send" button on mainScreen
if (c == sendCommand) {
// retrieving the web url that user entered
String urlstring = requestField.getString();
// sending a GET request to web server
String resultstring = sendGetRequest(urlstring);
// displaying the page content retrieved from web server
resultScreen = new Form("GET Result:");
resultField =
new StringItem(null, resultstring);
resultScreen.append(resultField);
resultScreen.addCommand(backCommand);
resultScreen.setCommandListener(this);
myDisplay.setCurrent(resultScreen);
} else if (c == backCommand) {
// do it all over again
requestField.setString(defaultURL);
myDisplay.setCurrent(mainScreen);
}
}
// send a GET request to web server
public String sendGetRequest(String urlstring) {
HttpConnection hc = null;
DataInputStream dis = null;
String message = "";
try {
/* openning up http connection with the web server
* when the connection is opened, the default request
* method is GET
*/
hc = (HttpConnection)
Connector.open(urlstring);
// establishing input stream from the connection
dis = new DataInputStream(hc.openInputStream());
// reading the response from web server character by character
int ch;
while ((ch = dis.read()) != -1) {
message = message + (char) ch;
}
} catch (IOException ioe) {
message = "ERROR";
} finally {
try { if (hc != null) hc.close();} catch (IOException ignored) {}
try { if (dis != null) dis.close();} catch (IOException ignored) {}
}
return message;
}
}
I want to know what is the problem?
Regards,
Joe
 
author
Sheriff
Posts: 11962
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
you should do exactly what the error message suggests -- start a new thread for the networking operation so you won't block the UI event thread.
 
Lasse Koskela
author
Sheriff
Posts: 11962
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You should login to www.forum.nokia.com and locate an article titled Brief Introduction to Networked MIDlets. It gives an example of how to launch a thread for making a HTTP request and waiting for the answer without blocking the UI event thread.
 
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Lasse,
Even I didn't ask anything, I would like to thank you about the link to Brief Introduction to Networked MIDlets. The article is obviously written by person how has a plenty of knowledge of this issue and at leats I learnt some new things from it.
The lenght of the article is only 22 pages and worth of reading.
br,
Jorma
 
Don't count your weasels before they've popped. And now for a mulberry bush related tiny ad:
Why should you try IntelliJ IDEA ?
https://coderanch.com/wiki/696337/IntelliJ-IDEA
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!