0

我正在开发一个必须与 servlet 对话才能访问数据库的 android 应用程序。我想将对象从 servlet 传递到我的 android 应用程序。

这是我发送对象的 servlet 代码。

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    // TODO Auto-generated method stub      
    response.setContentType("text");

    String id = request.getParameter("id");
    try {
        Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
        java.sql.Connection con= DriverManager.getConnection("jdbc:odbc:Database");
        Statement statement = con.createStatement();

        PrintWriter p = response.getWriter();

        ResultSet rs = statement.executeQuery("select * from employee where PS="+id);

        while(rs.next()){
            Employee e = new Employee();
            e.setId(rs.getString("ID"));
            e.setPs(String.valueOf(rs.getDouble("PS")));
            e.setName(rs.getString("Emp_name"));
            e.setDept(rs.getString("Dept"));

            ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
            ObjectOutputStream objectOutputStream = new ObjectOutputStream(outputStream);
            objectOutputStream.writeObject(e);

        }

    } catch (Exception e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}

我应该如何在我的 android 应用程序中读取对象。谢谢。

4

1 回答 1

0

那么你可以做各种各样的事情来解决这个问题

a)在这里使用 DAO 实践阅读它以理解它基本上它将代码的实现分为不同的段和类,以方便您使用。但是,如果您不想经历所有这些麻烦,您可以做一些类似的事情。a.) 创建一个单独的 java 类,在其中编写处理 Db 的所有方法,例如 insert、select 等。例如,如果您在用户中登录……那么方法将是这样的……

public int AuthenticateUser(String un, String pw, String phn) {
        System.out.println("I m in this method");
        Connection conn = (Connection) DBConnection.getInstance().getConnection();
        Statement pstmt = null;
        ResultSet rs = null;

        if (conn == null) {
            System.out.println("Connection error");
            check = -1;

        }


        try {
            String authenticatequery = "SELECT * FROM users WHERE uname = '"+un+"' && password =  '"+pw+"' && phoneno = '"+phn+"'";


            System.out.println(authenticatequery);
            pstmt = conn.createStatement();
            rs = pstmt.executeQuery(authenticatequery);
            System.out.println("I m in above try catch");

            // pstmt = conn.prepareStatement(authenticatequery);

            // rs = pstmt.executeQuery(authenticatequery);


            if (rs.next()) {
                System.out.println("I am in rs method");
                UserInfo  info = new UserInfo();
                rs.getString(1);
                rs.getString(2);
                rs.getString(3);
                //info.setUsername(rs.getString(un));
                System.out.println("i have the username" + un);
                //info.setPassword(rs.getString(un));
                System.out.println("I have got the password " + pw);

                System.out.println("I have got the password " + phn);

                System.out.println("User Exist");
                check = 1;
            } else {

                System.out.println("No user found");
                check = 0;
            }

            // rs.close();
            // pstmt.close();
            // conn.close();
        } catch (SQLException e) {
            // TODO: handle exception
            System.out.println("Exception-internal error");
        } finally {

            if (conn != null) {
                try {
                    conn.close();
                    pstmt.close();
                    conn.close();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }

}
    return check;



}

请注意 int 变量“检查”,如果该方法运行正确,它将返回“1”否则返回“0”。现在您可以使用此检查来获得优势,并通过 servlet 将其发送到您的 android 应用程序,如下所示。

protected void process(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub

        //String username = request.getParameter("username");
    //  String password = request.getParameter("password");
        //UserInfo uinfo = new UserInfo();

    // uinfo.setUsername(username);
    // uinfo.setPassword(password);



        System.out.println("I am in process");

        PrintWriter out = response.getWriter();
          String un, pw,phn;

          un = request.getParameter("Usrnm");
          System.out.println(un);
          pw = request.getParameter("pwd");
          System.out.println(pw);

          phn = request.getParameter("phn");
          System.out.println(phn);

            Authenticate auth = new Authenticate();
            int check = auth.AuthenticateUser(un, pw,phn);
            System.out.println("My result is" + check);
          if (check==1){

              out.print(1);
          }  else{
               out.print(0);
              }
              }
    }

在您的 android 应用程序中,您可以使用下面的代码调用响应对象,为通信添加自定义 http 客户端类。

response = CustomHttpClient.executeHttpPost("http://192.1..11../HelloWorldServlet/LoginDriverServlet", postParameters);

现在您可以使用响应对象并对其进行塑造以满足您的需求

if (res.equals("1")) {

                        Log.e("CHeck4 response","i m here");
                        Log.e("CHeck response", res);
                        //error.setText("Correct Username or Password");
                        Intent intent = new Intent(getApplicationContext(), SetStatus.class);

                        startActivity(intent);
                        setContentView(R.layout.activity_set_status);
                        Toast.makeText(getApplicationContext(), "Loggin In", Toast.LENGTH_LONG).show();
                    } else if(res.equals("0")) {
                        //error.setText("Sorry!! Incorrect Username or Password");
                        Log.e("CHeck3 response", "i m here");
                        Toast.makeText(getApplicationContext(), "Sorry!! Incorrect Username or Password", Toast.LENGTH_LONG).show();
                        Intent intent = new Intent();


                        //Username or Password",Toast.LENGTH_LONG).show();
                    }



Hope you get it..otherwise coments are welcomed :)
于 2015-11-09T10:08:25.537 回答