• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Devaka Cooray
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Tim Moores
  • Carey Brown
  • Mikalai Zaikin
Bartenders:
  • Lou Hamers
  • Piet Souris
  • Frits Walraven

j2me httpconnection

 
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi i am try to run this program but i have got one warning plz help me .

import javax.microedition.midlet.*;

import javax.microedition.lcdui.*;

import javax.microedition.io.*;

import java.io.*;

public class HttpGET extends MIDlet implements CommandListener {
/* * the default value for the url string is * http://64.28.105.110/servlets/webyu/Chapter7Servlet?request=gettimestamp * * user can change it to some other urls within the application * */
private static String defaultURL = "http://www.smsindya.com/app/forlogin.asp?userid=919892790096&password=26201439&isdcode=91";

// 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 HttpGET(){
// 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 have got warning

Warning: To avoid potential deadlock, operations that may block, such as
networking, should be performed in a different thread than the
commandAction() handler.

plz help
my email :- ilumania@yahoo.co.in

thanks!
 
ranger
Posts: 17347
11
Mac IntelliJ IDE Spring
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It is just a warning, you don't have to do anything about it if you don't want to.

All that it says is that when you connect to HTTP to get stuff, the whole main thread will be blocked and waiting till it gets everything back. But if you spawn off a thread to load the stuff, then the program won't appear frozen, as the main thread can keep going and show something like a progress bar.

Mark
 
He does not suffer fools gladly. But this tiny ad does:
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic