Pessoal estou fazendo tudo "certo" mas não estou conseguindo connectar no emulador eu estou tendo conectar em um Servlet Java, mas consigo…
abaixo segue o código por favor me ajudem !!!
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
import javax.microedition.io.*;
import java.io.*;
public class Conecao extends MIDlet implements CommandListener
{
private Display display; // Reference to display object
private Form fmMain; // The main form
private TextField tfAcct; // Get account number
private TextField tfPwd; // Get password
private Command cmCall; // Command to call the servlet
private Command cmExit; // Command to exit
public Conecao()
{
display = Display.getDisplay(this);
// Textfields
tfAcct = new TextField("Conta:", "", 5, TextField.NUMERIC);
tfPwd = new TextField("Password:", "", 10, TextField.ANY | TextField.PASSWORD);
// Define commands
cmCall = new Command("Call", Command.SCREEN, 2);
cmExit = new Command("Exit", Command.EXIT, 1);
// Create the form, add components and commands
fmMain = new Form("Data from servlet");
fmMain.append(tfAcct);
fmMain.append(tfPwd);
fmMain.addCommand(cmExit);
fmMain.addCommand(cmCall);
// Capture events
fmMain.setCommandListener(this);
}
// Called by application manager to start the MIDlet.
public void startApp()
{
display.setCurrent(fmMain);
}
public void pauseApp()
{ }
public void destroyApp(boolean unconditional)
{ }
private void callServlet() throws IOException
{
HttpConnection http = null;
InputStream iStrm = null;
// Data is passed at the end of url for GET
String url = "http://localhost:8080/Locadora/GetNPostServltet" + "?" +
"conta=" + 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
//----------------
if (http.getResponseCode() == HttpConnection.HTTP_OK)
{
iStrm = http.openInputStream();
// 2) Get header information - none
// 3) Get body (data)
int length = (int) http.getLength();
if (length > 0)
{
byte servletData[] = new byte[length];
iStrm.read(servletData);
// Update the string item on the display
fmMain.append("You passed to the servlet: \n" + new String(servletData));
}
else
fmMain.append("Unable to read data");
}
}
catch (Exception e)
{
fmMain.append("Network error");
System.out.println(e.toString());
}
finally
{
// Clean up
if (iStrm != null)
iStrm.close();
if (http != null)
http.close();
}
}
public void commandAction(Command c, Displayable s)
{
if (c == cmCall)
{
try
{
callServlet();
}
catch (Exception e)
{
System.out.println(e.toString());
}
}
else if (c == cmExit)
{
destroyApp(false);
notifyDestroyed();
}
}
}
e o código do servlet…
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
import java.io.*;
import java.net.*;
import javax.servlet.*;
import javax.servlet.http.*;
/**
*
* @author Rafael
*/
public class GetNPostServltet extends HttpServlet {
/**
* Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
* @param request servlet request
* @param response servlet response
*/
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();
try {
/* TODO output your page here
out.println("<html>");
out.println("<head>");
out.println("<title>Servlet GetNPostServltet</title>");
out.println("</head>");
out.println("<body>");
out.println("<h1>Servlet GetNPostServltet at " + request.getContextPath () + "</h1>");
out.println("</body>");
out.println("</html>");
*/
} finally {
out.close();
}
}
// <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
/**
* Handles the HTTP <code>GET</code> method.
* @param request servlet request
* @param response servlet response
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String conta = request.getParameter("conta");
String pwd = request.getParameter("password");
String balance = conta+pwd;
response.setContentType("text/plain");
PrintWriter out = response.getWriter();
out.println(balance);
out.close();
}
/**
* Handles the HTTP <code>POST</code> method.
* @param request servlet request
* @param response servlet response
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String conta = request.getParameter("conta");
String pwd = request.getParameter("password");
String balance = conta+pwd;
response.setContentType("text/plain");
PrintWriter out = response.getWriter();
out.println(balance);
out.close();
}
/**
* Returns a short description of the servlet.
*/
public String getServletInfo() {
return "Short description";
}
// </editor-fold>
}
vai saber !!!