如何从android中的servlet读取对象

时间:2012-11-07 12:00:35

标签: java android servlets

我正在开发一个Android应用程序,它必须与servlet通信才能访问数据库。我想将对象从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应用程序中的对象。 感谢。

1 个答案:

答案 0 :(得分:0)

你可以做各种事情来解决这个问题

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

$http.get('first_url').then(function(data) {
    var second_url = data.url;
    $http.get(second_url).then(function(data) {
        console.log(data); //final data
    })
})

注意int变量"检查"如果方法运行正常,它将返回' 1'否则' 0' ..现在你可以使用这个检查优势,并通过像这样的servlet发送到你的Android应用程序..

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;



}

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

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);
              }
              }
    }

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

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