wood burning stoves 2.0*
The moose likes Java Micro Edition and the fly likes How to invoke the MIDlet ? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Mobile » Java Micro Edition
Bookmark "How to invoke the MIDlet ?" Watch "How to invoke the MIDlet ?" New topic

How to invoke the MIDlet ?

Soumya Mathai

Joined: Jan 01, 2006
Posts: 6
I tried using this code :

import java.io.*;
import javax.microedition.lcdui.*;
import javax.microedition.midlet.*;
import javax.microedition.io.*;

public class HttpLogger extends MIDlet {

private Display display;

private Command exitCommand =
new Command( "Exit", Command.EXIT, 1 );
private Command okCommand =
new Command( "OK", Command.OK, 1 );
private Command cancelCommand =
new Command( "Cancel", Command.CANCEL, 1 );

private URLEntry mainForm;

public HttpLogger(){

protected void destroyApp( boolean unconditional )
throws MIDletStateChangeException {

protected void pauseApp(){

protected void startApp()
throws MIDletStateChangeException {
if( display == null ){

private void initMIDlet(){
display = Display.getDisplay(this);
mainForm=new URLEntry();

public void exitMIDlet(){

void displayError( Throwable e, Displayable next ){
Alert a = new Alert( "Exception" );
a.setString( e.toString() );
a.setTimeout( Alert.FOREVER );
display.setCurrent( a, next );

class URLEntry extends TextBox implements CommandListener {

super("Enter URL:","localhost:8080/jsp-examples/hello.html",100,0 );
addCommand(exitCommand );
addCommand(okCommand );
setCommandListener( this );

public void commandAction( Command c,
Displayable d ){
if( c == exitCommand ){
else if( c == okCommand ) {
try {
HttpConnection conn =(HttpConnection)Connector.open("http://"+getString());
display.setCurrent(new Logger( this, conn ) );
catch( IOException e ){
displayError( e, this );

class Logger extends Form
implements Runnable, CommandListener {
private Displayable next;
private HttpConnection conn;
private StringItem text =
new StringItem( null, "" );

Logger( Displayable next,
HttpConnection conn )
super( "HTTP Log" );

this.next = next;
this.conn = conn;

addCommand( cancelCommand );
setCommandListener( this );

append( text );

Thread t = new Thread( this );

public void commandAction( Command c,
Displayable d ){
display.setCurrent( next );

try {
catch( IOException e ){
displayError( e, next );

public void run(){
update( "Connecting to " + conn.getURL() );

try {
int rc = conn.getResponseCode();
update( "Response code: " + rc );
update( "Response message: " +
conn.getResponseMessage() );

String key;

for( int i = 0;
( key =
conn.getHeaderFieldKey( i ) )
!= null;
++i ){
update( key + ": " +
conn.getHeaderField( i ) );
catch( IOException e ){
update( "Caught exception: " +
e.toString() );

removeCommand( cancelCommand );
addCommand( okCommand );

void update( String line ){
if( display.getCurrent() != this ) return;

String old = text.getText();
StringBuffer buf = new StringBuffer();

buf.append( old );
if( old.length() > 0 ){
buf.append( '\n' );

buf.append( line );
text.setText( buf.toString() );

but i'm getting the warning as:

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

how to solve this?

Please help...,

Mark Spritzler

Joined: Feb 05, 2001
Posts: 17249

Well, I can't read your code because there is no formatting or indentation. I tried to edit your post, expecting to see indentation and just add the [/CODE] tags around it, but there was no indentation.

But you are just receiving a warning and not an error, you can still run your application. It is just saying that you are going to use COmmunication somewhere where the program will hang and wait until it gets a response, in some cases that is fine.

If you wanted to run the Communication in a seperate thread, therefore not block your app, then you won't see the warning again, but you can disregard the warning too.


Perfect World Programming, LLC - Two Laptop Bag - Tube Organizer
How to Ask Questions the Smart Way FAQ
Eduardo Marques
Ranch Hand

Joined: Feb 19, 2005
Posts: 231
[duplicate posting]
[ January 05, 2006: Message edited by: Eduardo Marques ]

Eduardo Marques
Get ready for your SCMAD certification at J2MECertificate.com - See what all the buzz is about ...
Eduardo Marques
Ranch Hand

Joined: Feb 19, 2005
Posts: 231
You are closing the connection (c.close()) in commandAction(), this is the reason for the warning. Note this is just a warning, but preferably you should do c.close() in another thread (not the system thread that invokes commandAction()).

You will not receive other UI events until commandAction() returns ie in your code until c.close() returns. c.close() is a networking operation and may be a time-consuming operation ...
I agree. Here's the link: http://aspose.com/file-tools
subject: How to invoke the MIDlet ?
Similar Threads
Fixed deadlock problem... now new problem
choicegroup not gettin displayed
J2ME, servlet and database help..
How to retrieve the string from textbox using Midlet to invoke the JSP
Zoom In/Zoom Out Image with j2me