Exemplo do livro J2ME Core

1 resposta
R

Pessoal,

Estou tentando rodar o exemplo do livro J2ME Core, mas acontece o seguinte erro:

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

Será que alguém pode me dar alguma dica? Veja o código:

import javax.microedition.midlet.<em>;

import javax.microedition.lcdui.</em>;

import <a href="http://javax.microedition.io">javax.microedition.io</a>.<em>;

import <a href="http://java.io">java.io</a>.</em>;
public class GetNpost extends MIDlet implements CommandListener

{

private Display display;      // Reference to Display object

private Form fmMain;         // The main form

private Alert alError;       // Alert to error message

private Command cmGET;       // Request method GET

private Command cmPOST;      // Request method Post

private Command cmExit;      // Command to exit the MIDlet

private TextField tfAcct;    // Get account number

private TextField tfPwd;     // Get password

private StringItem siBalance;// Show account balance

private String errorMsg = null;

public GetNpost()

{

display = Display.getDisplay(this);
// Create commands
cmGET = new Command("GET", Command.SCREEN, 2);
cmPOST = new Command("POST", Command.SCREEN, 3);    
cmExit = new Command("Exit", Command.EXIT, 1);

// Textfields
tfAcct = new TextField("Account:", "", 5, TextField.NUMERIC);
tfPwd = new TextField("Password:", "", 10, TextField.ANY | TextField.PASSWORD);        

// Balance string item
siBalance = new StringItem("Balance: $", "");

// Create Form, add commands & componenets, listen for events
fmMain = new Form("Account Information");    
fmMain.addCommand(cmExit);
fmMain.addCommand(cmGET);
fmMain.addCommand(cmPOST);
fmMain.append(tfAcct);
fmMain.append(tfPwd);
fmMain.append(siBalance);
fmMain.setCommandListener(this);

}

public void startApp()

{

display.setCurrent(fmMain);

}
public void pauseApp()

{ }

public void destroyApp(boolean unconditional)

{ }
public void commandAction(Command c, Displayable s)

{

if (c == cmGET || c == cmPOST)

{

try

{

if (c == cmGET)

lookupBalance_withGET();

else

lookupBalance_withPOST();

}

catch (Exception e)

{

System.err.println("Msg: " + e.toString());

}

}

else if (c == cmExit)

{

destroyApp(false);

notifyDestroyed();

}

}

/*--------------------------------------------------

  • Access servlet using GET
    
    <em>-------------------------------------------------</em>/
    
    private void lookupBalance_withGET() throws IOException
    
    {
    
    HttpConnection http = null;
    
    InputStream iStrm = null;
    
    boolean ret = false;
    
    // Data is passed at the end of url for GET
    
    String url = <a href="http://127.0.0.1:8080/examples/servlet/GetNpostServlet">http://127.0.0.1:8080/examples/servlet/GetNpostServlet</a> + “?” +
    
    “account=” + tfAcct.getString() + & +
    
    “password=” + tfPwd.getString();
    
    try
    
    {
    
    http = (HttpConnection) Connector.open(url);
    
    //----------------
    
    // Client Request
    
    //----------------
    
    // 1) Send request method
    
    http.setRequestMethod(HttpConnection.GET);
    
    // 2) Send header information - none
    
    // 3) Send body/data -  data is at the end of URL
    
    //----------------
    
    // Server Response
    
    //----------------
    
    iStrm = http.openInputStream();
    
    // Three steps are processed in this method call
    
    ret = processServerResponse(http, iStrm);	
    
    }
    
    finally
    
    {
    
    //Clean up
    
    if (iStrm != null)
    
    iStrm.close();
    
    if (http != null)
    
    http.close();
    
    }
    
    // Process request failed, show alert
    
    if (ret == false)
    
    showAlert(errorMsg);
    
    }
    

/*--------------------------------------------------

  • Access servlet using POST
    
    <em>-------------------------------------------------</em>/
    
    private void lookupBalance_withPOST() throws IOException
    
    {
    
    HttpConnection http = null;
    
    OutputStream oStrm = null;
    
    InputStream iStrm = null;
    
    boolean ret = false;
    
    // Data is passed as a separate stream for POST (below)
    
    String url = <a href="http://127.0.0.1:8080/examples/servlet/GetNpostServlet">http://127.0.0.1:8080/examples/servlet/GetNpostServlet</a>”;
    
    try
    
    {
    
    http = (HttpConnection) Connector.open(url);
    
    oStrm = http.openOutputStream();
    
    //----------------
    
    // Client Request
    
    //----------------
    
    // 1) Send request type
    
    http.setRequestMethod(HttpConnection.POST);
    
    // 2) Send header information. Required for POST to work!
    
    http.setRequestProperty(“Content-Type”, “application/x-www-form-urlencoded”);
    

    // If you experience connection/IO problems, try
    // removing the comment from the following line
    http.setRequestProperty(“Connection”, “close”);

    // 3) Send data/body
    
    // Write account number
    
    byte data[] = (account= + tfAcct.getString()).getBytes();
    
    oStrm.write(data);
    
    // Write password
    
    data = ("&password=" + tfPwd.getString()).getBytes();
    
    oStrm.write(data);
    
    // For 1.0.3 remove flush command
    
    // See the note at the bottom of this file
    
    oStrm.flush();
    
    //----------------
    
    // Server Response
    
    //----------------
    
    iStrm = http.openInputStream();
    
    // Three steps are processed in this method call
    
    ret = processServerResponse(http, iStrm);
    
    }
    
    finally
    
    {
    
    // Clean up
    
    if (iStrm != null)
    
    iStrm.close();
    
    if (oStrm != null)
    
    oStrm.close();
    
    if (http != null)
    
    http.close();
    
    }
    
    // Process request failed, show alert
    
    if (ret == false)
    
    showAlert(errorMsg);
    
    }
    

/*--------------------------------------------------

  • Process a response from a server
    
    <em>-------------------------------------------------</em>/
    
    private boolean processServerResponse(HttpConnection http, InputStream iStrm) throws IOException
    
    {
    
    //Reset error message
    
    errorMsg = null;
    
    // 1) Get status Line
    
    if (http.getResponseCode() == HttpConnection.HTTP_OK)
    
    {
    
    // 2) Get header information - none
    
    // 3) Get body (data)
    
    int length = (int) http.getLength();
    
    String str;
    
    if (length != -1)
    
    {
    
    byte servletData[] = new byte[length];
    
    iStrm.read(servletData);
    
    str = new String(servletData);
    
    }
    
    else  // Length not available…
    
    {
    
    ByteArrayOutputStream bStrm = new ByteArrayOutputStream();
    
    int ch;
    
    while ((ch = iStrm.read()) != -1)
    
    bStrm.write(ch);
    
    str = new String(bStrm.toByteArray());
    bStrm.close();
    
    }
    
    // Update the string item on the display
    
    siBalance.setText(str);
    
    return true;
    
    }
    
    else
    
    // Use message from the servlet
    
    errorMsg = new String( http.getResponseMessage());
    

    return false;
    }

/*--------------------------------------------------

  • Show an Alert
    
    <em>-------------------------------------------------</em>/
    
    private void showAlert(String msg)
    
    {
    
    // Create Alert, use message returned from servlet
    
    alError = new Alert(“Error”, msg, null, AlertType.ERROR);
    

    // Set Alert to type Modal
    alError.setTimeout(Alert.FOREVER);

    // Display the Alert. Once dismissed, display the form
    
    display.setCurrent(alError, fmMain);
    
    }
    
    }
    

1 Resposta

C

Existem MILHARES de posts relacionados a esse problema.
Dá uma procuradinha pela palavra deadlock.

Falow!

Criado 18 de agosto de 2005
Ultima resposta 18 de ago. de 2005
Respostas 1
Participantes 2