• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
  • Campbell Ritchie
  • Liutauras Vilda
  • Bear Bibeault
  • Tim Cooke
  • Junilu Lacar
  • Paul Clapham
  • Devaka Cooray
  • Knute Snortum
Saloon Keepers:
  • Ron McLeod
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Frits Walraven
  • Carey Brown
  • salvin francis
  • Claude Moore

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 =
// 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);
public void startApp() {
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);
} else if (c == backCommand) {
// do it all over again
// 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)
// 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?
Posts: 11962
  • 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
Posts: 11962
  • 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.
Destiny's powerful hand has made the bed of my future. And this tiny ad:
Create Edit Print & Convert PDF Using Free API with Java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!