Question

J'ai fait l'application de connexion J2ME simple, le code est ci-dessous.

Cela fonctionne parfaitement sur l'émulateur, maintenant je veux l'exécuter sur mon application mobile via Bluetooth Connection. Dans un autre mot, la communication téléphonique (application J2ME) -pc (servlet) via Bluetooth. Besoin d'aide.

mettre à jour Prise en charge de l'appareil JSR 82 API Java pour Bluetooth 1.1.

package hello;

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

public class MidletServlet extends MIDlet implements CommandListener {
    Display display = null;
    Form form = null;
    TextField tb = null,tb1= null;
    String str = null;
    String url = "http://localhost:8084/j2metest/getText";
    Command backCommand = new Command("Back", Command.BACK, 0);
    Command submitCommand = new Command("Submit", Command.OK, 2);
    Command exitCommand = new Command("Exit", Command.STOP, 3);
    private Test test;

    public MidletServlet() {}

    public void startApp() throws MIDletStateChangeException {
        display = Display.getDisplay(this);
        form = new Form("Request Servlet");
        tb = new TextField("Username: ","",30,TextField.ANY );
        tb1 = new TextField("Password: ","",30,TextField.ANY);
        form.append(tb);
        form.append(tb1);
        form.addCommand(submitCommand);
        form.addCommand(exitCommand);
        form.setCommandListener(this);
        display.setCurrent(form);
    }

    public void pauseApp() {}

    public void destroyApp(boolean unconditional) {}

    public void commandAction(Command c, Displayable d) {
        if (c == exitCommand) {
            destroyApp(true);
            notifyDestroyed();
        } else if (c == backCommand) {
            display.setCurrent(form);
        } else if (c == submitCommand) {
            test  = new Test(this);
            test.start();
            test.getServlet(tb.getString(),tb1.getString());
        }
    }
    class Test implements Runnable {
        MidletServlet midlet;
        private Display display;
        String pass;
        String user;

        public Test(MidletServlet midlet) {
            this.midlet = midlet;
            display = Display.getDisplay(midlet);
        }

        public void start() {
            Thread t = new Thread(this);
            t.start();
        }

        public void run() {
            StringBuffer sb = new StringBuffer();
            try {
                HttpConnection c = (HttpConnection) Connector.open(url);
                c.setRequestProperty(
                  "User-Agent","Profile/MIDP-2.1, Configuration/CLDC-1.1");

                c.setRequestProperty("Content-Language","en-US");
                c.setRequestMethod(HttpConnection.POST);

                DataOutputStream os =
                        (DataOutputStream)c.openDataOutputStream();

                os.writeUTF(user.trim());
                os.writeUTF(pass.trim());
                os.flush();
                os.close();

                // Get the response from the servlet page.
                DataInputStream is =(DataInputStream)c.openDataInputStream();
                //is = c.openInputStream();
                int ch;
                sb = new StringBuffer();
                while ((ch = is.read()) != -1) {
                    sb.append((char)ch);
                }
                showAlert(sb.toString());
                is.close();
                c.close();
            } catch (Exception e) {
                showAlert(e.getMessage());
            }
        }
                /* This method takes input from user like text and pass
                to servlet */
        public void getServlet(String user,String pass) {
            this.user = user;
            this.pass=pass;
        }

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

Et c'est mon code de servlet

package my;
import java.sql.*;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 *
 * @author vicky
 */
import java.io.*;
import java.text.*;
import javax.servlet.*;
import javax.servlet.http.*;

public class getText extends HttpServlet {

    public void init() {
    }

    public void doPost(HttpServletRequest request,
            HttpServletResponse response) throws ServletException,
            IOException {

        DataInputStream in =
                new DataInputStream((InputStream)request.getInputStream());

        String user = in.readUTF();
        String pass=in.readUTF();
        String text="";
        try {

            Class.forName("oracle.jdbc.driver.OracleDriver");
                Connection conn= DriverManager.getConnection(
                "jdbc:oracle:thin:@localhost:1521:XE","vicky","1");
                Statement stmt=conn.createStatement();
                ResultSet rs;int f=0;
            rs=stmt.executeQuery("select * from ADMIN");
            //s1=request.getParameter("un");
            //s2=request.getParameter("pw");

            if(user==""||pass=="")
                {
                    text="Username and Password are required for login;";
                }
            else
                {
                //session.setAttribute("uname",s1);
                while(rs.next())
                    {
                    String s3=rs.getString(1);
                    String s4=rs.getString(2);
                    if (user.equals(s3)&&pass.equals(s4))
                        {
                            f=1;
                        }
                }
                if(f==1)
                    {
                    //response.sendRedirect("success.jsp");
                    //out.print(s1);
                text="Login Successful";
                                }
                else
                    {
                     //out.print("ERROR");
                    text="Login Failure";
                                }
                }
          }
        catch(Exception e)
        {
       //out.print("ERROR"+e);
        text="error"+e;
            }
        response.setContentType("text/plain");
        response.setContentLength(text.length());
        PrintWriter out = response.getWriter();

        out.print(text);


        in.close();
        out.close();
        out.flush();
    }

    public void doGet(HttpServletRequest request,
            HttpServletResponse response) throws ServletException,
            IOException {

        doPost(request,response);
    }
}
Était-ce utile?

La solution

Je pense que c'est là que se trouve le problème réel, changez simplement votre String url = "http://localhost:8084/j2metest/getText"; à l'IP de son ordinateur comme 192.168.1.1, puis réessayer.

La raison derrière cela est lorsque vous exécutez Java ME Code sur l'émulateur, il détecte LocalHost. Mais maintenant, lorsque vous avez déployé cette application sur mobile et la connecte avec Bluetooth, vous accédez à l'ordinateur de l'extérieur, donc dans ce cas, localhost ne fonctionnera pas. Vous devez passer son IP.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top