Hi,

In the Retrieve email code, im getting the warning as -To avoid potential deadlock, operations that may block, such as
networking, should be performed in a different thread than the commandAction() handler.

Got to know that from discussion forum's that thread concept should be used and networking operations should be written in run() method, but im not getting clearly of wat exactly should come under the run method.

Any help would be appreciated.

Here is the code for Retrieving email -

class CheckMail extends MIDlet implements CommandListener {
private String MAIL_SERVER_URL =
"http://localhost:8080/examples/servlet/CheckMailServlet?";
private int MSG_LIST_SIZE = 5;
Display display = null;
List menu = null;
List dmenu = null;
TextBox input = null;
String[] msgs = new String[MSG_LIST_SIZE];
String user = null;
static int MAX_LIST_SIZE;

int status = 0;
Command backCommand = new Command("Back", Command.BACK, 0);
Command submitCommand = new Command("Submit", Command.OK, 2);
Command exitCommand = new Command("Exit", Command.STOP, 3);

public CheckMail() {System.out.println("Inside the constructor");}

public void startApp() throws MIDletStateChangeException {
System.out.println("Inside the startApp");
display = Display.getDisplay(this);
displayMenu();
}

public void pauseApp() {System.out.println("Inside the pauseApp");}

public void destroyApp(boolean unconditional) {
System.out.println("Inside the destroyApp");
notifyDestroyed();
}

public void commandAction(Command c, Displayable d) {
if(c == exitCommand ) {
destroyApp(true);
} else if (c == backCommand) {
handleBack();
} else if (c == submitCommand) {
user = input.getString();
retreiveMail(0, null);
} else if (d == dmenu) {
handleMainMenu();
} else if (d == menu) {
handleMsgMenu();
} else
loginUser();
}




/* Display Main Screen */
private void displayMenu() {
dmenu = new List("Check Email", Choice.IMPLICIT);
if (user == null)
dmenu.append("Login", null);
else
dmenu.append("Logout", null);
dmenu.append("Check Mail", null);
dmenu.addCommand(exitCommand);
dmenu.setCommandListener(this);
display.setCurrent(dmenu);
status = 0;
}

/* This method takes users Mail ID and password*/
private void loginUser() {
input = new TextBox(
"Enter Login Name/Password (Seperate by /) :", "test/test", 25,
TextField.ANY);

input.addCommand(submitCommand);
input.addCommand(backCommand);
input.setCommandListener(this);
display.setCurrent(input);
}

/* This method retrieves Message with msgNum and
if no MsgNum, displays the list of all msgs*/
private void retreiveMail(int s, String msgNum) {

String popURL = MAIL_SERVER_URL + "u=" + user;
if (s == 0)
popURL = popURL; // Login
if (s == 1)
// List messages
popURL = popURL + "&s=" + msgNum + "&a=l";
else if (s == 2)
// Retreive message with i = msgNum
popURL = popURL + "&i=" + msgNum + "&a=r";

StreamConnection c = null;
InputStream is=null;
StringBuffer sb = null;
String err = null;

try {
c = (StreamConnection)Connector.open(popURL,
Connector.READ_WRITE);

is = c.openInputStream();
int ch;
sb = new StringBuffer();
while ((ch = is.read()) != -1) {
sb.append((char)ch);
}
} catch(Exception ex){ err = ex.getMessage(); } finally {
try {
if(is!= null) {is.close(); }
if(c != null) {c.close(); }
} catch(Exception exp) { err = exp.getMessage(); }
}

if (err == null) {
if (s == 0) {
user = sb.toString();

if (user.indexOf("INVALIDUSR") >= 0) {
user = null;
showAlert("Invalid User Name and Password");
} else {
input = null;
dmenu = null;
displayMenu();
}
} else if (s == 1) // For retreiving the list
showMList(sb.toString());
else // s ==2, show Message
showMsg(sb.toString());
} else
showAlert(err); // Need to Show Error Page
}

/* Display the selected Message*/
private void showMsg(String msg) {
StringBuffer buffer = new StringBuffer(200);
int delim = 'ß';
int j = msg.indexOf(delim);
int i =0;

// Get's FROM
buffer.append(msg.substring(0,j ));
for (int k=0;k<4;k++) {
i = j+1;
j = msg.indexOf(delim, i);
// Get's TO, CC, Subject, Date
buffer.append(msg.substring(i,j ));
}
// Rest of the Message
buffer.append("\n" + msg.substring(j+1 ));
showAlert(buffer.toString());
}

/* Display Error On screen*/
private void showAlert(String err) {
Alert a = new Alert("");
a.setString(err);
a.setTimeout(Alert.FOREVER);
display.setCurrent(a);
}

/* Display MessageList of User*/
private void showMList(String msgStr) {
for (int i=0;i<MAX_LIST_SIZE;i++) {
msgs[i] = "";
}
menu = new List("Messages", Choice.IMPLICIT);
int delim = 'ß';
String sub = null;
int i =0;
int k = 0;
int l =0;
int j =msgStr.indexOf(delim);
while( j >= 0) {
sub = msgStr.substring(i,j);
if ((k % 2) == 0) {
msgs[l] = sub; // First would be Message Number
l++;
} else
menu.append(sub, null);
k++;
i = j + 1;
j = msgStr.indexOf(delim, i); // Rest of substrings
}
sub = msgStr.substring(i); // Last substring
menu.append(sub, null);
menu.addCommand(backCommand);
menu.addCommand(exitCommand);
menu.setCommandListener(this);
status = 1;
display.setCurrent(menu);
}

private void handleBack() {
if (status == 1) {
dmenu = null;
displayMenu();
} else
display.setCurrent(menu);
}

private void handleMainMenu() {
int index = dmenu.getSelectedIndex();
switch(index) {
case 0 :
if (user != null) {
retreiveMail(1, "0");
break;
}
case 1 :
status = 1;
loginUser();
break;
case 2 :
break;
}
}

private void handleMsgMenu() {
int index = menu.getSelectedIndex();
if (!msgs[index].equals("")) {
retreiveMail(2, msgs[index]); // Retreive Mail
}
}
}


Regards
Nagesh