Android - 使用 Httppost 将一些数据从 android 应用程序传递到 servlet,但在 servlet 中变为 null

发布于 2025-01-08 07:39:13 字数 2883 浏览 0 评论 0原文

//Here is my Eclipse, suppose i already get the heartvalue and oxygenvalue in other method call
private OnClickListener upload = new OnClickListener()
{
    public void onClick(View v)
    {
        ArrayList<NameValuePair> namevaluepairs = new ArrayList<NameValuePair>(2);
        namevaluepairs.add(new BasicNameValuePair("o2",oxygenvalue));
        namevaluepairs.add(new BasicNameValuePair("heart",heartvalue));
        try {
            HttpClient httpclient = new DefaultHttpClient();
            HttpPost httppost = new HttpPost("http://www2.comp.polyu.edu.hk:8080/08065312D/servlet/ServletTest"); 
            httppost.setEntity(new UrlEncodedFormEntity(namevaluepairs));
            HttpResponse response = httpclient.execute(httppost);
            HttpEntity entity = response.getEntity();
            Log.d("d", response.toString());
            InputStream is = entity.getContent();
            BufferedReader reader = new BufferedReader(new InputStreamReader(is, "iso-8859-1"), 8);
            StringBuilder sb = new StringBuilder();
            String line = null;
            while ((line = reader.readLine()) != null) {
                sb.append(line + "\n");
            }
            is.close();
            String reading = sb.toString();
            TextView lbl = (TextView) findViewById(R.id.test4);
            lbl.setText(reading);
        } catch (Exception e) {
        }

//这是我的servlet

public class FypServletTest extends HttpServlet {

public void doPost(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
        Connection con = null;
        Statement stmt = null;
        response.setContentType("text/html");
        PrintWriter out = response.getWriter();

        try {
            Class.forName("com.mysql.jdbc.Driver");
            con = DriverManager.getConnection(
                    "jdbc:mysql://mysql.comp.polyu.edu.hk:3306/", "08065312d", "xxxx");
            stmt = con.createStatement();
            stmt.execute("USE 08065312d");
            String id = "2";
            String oxygenvalue = request.getParameter("o2");
            String heartvalue = request.getParameter("heart");
            stmt = con.createStatement();
            String query="insert into Test values('"+id+"','"+oxygenvalue+"','"+heartvalue+"');";
            int rs = stmt.executeUpdate(query);
            out.flush();
            out.close();

        }
          catch(Exception ex)
            {

            }

} }

//我可以成功地将新行添加到数据库中,但参数(读数)变为空。我尝试通过在servlet中直接 println(request.getParmater("o2")) 来更改程序,但不保存到数据库,结果也是相同的(在servlet中读取变为null null)。所以我怀疑我无法获取传递给servlet的参数或者request.getParmater方法有问题,那么到底出了什么问题呢?

//我遇到的问题是发送 POST 变量,该变量似乎一直为空 //过去一周我一直在查看论坛,所有 httppost 示例几乎完全遵循这一点。如果您愿意提供任何帮助,我将不胜感激。多谢

//Here is my Eclipse, suppose i already get the heartvalue and oxygenvalue in other method call
private OnClickListener upload = new OnClickListener()
{
    public void onClick(View v)
    {
        ArrayList<NameValuePair> namevaluepairs = new ArrayList<NameValuePair>(2);
        namevaluepairs.add(new BasicNameValuePair("o2",oxygenvalue));
        namevaluepairs.add(new BasicNameValuePair("heart",heartvalue));
        try {
            HttpClient httpclient = new DefaultHttpClient();
            HttpPost httppost = new HttpPost("http://www2.comp.polyu.edu.hk:8080/08065312D/servlet/ServletTest"); 
            httppost.setEntity(new UrlEncodedFormEntity(namevaluepairs));
            HttpResponse response = httpclient.execute(httppost);
            HttpEntity entity = response.getEntity();
            Log.d("d", response.toString());
            InputStream is = entity.getContent();
            BufferedReader reader = new BufferedReader(new InputStreamReader(is, "iso-8859-1"), 8);
            StringBuilder sb = new StringBuilder();
            String line = null;
            while ((line = reader.readLine()) != null) {
                sb.append(line + "\n");
            }
            is.close();
            String reading = sb.toString();
            TextView lbl = (TextView) findViewById(R.id.test4);
            lbl.setText(reading);
        } catch (Exception e) {
        }

//Here is my servlet

public class FypServletTest extends HttpServlet {

public void doPost(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
        Connection con = null;
        Statement stmt = null;
        response.setContentType("text/html");
        PrintWriter out = response.getWriter();

        try {
            Class.forName("com.mysql.jdbc.Driver");
            con = DriverManager.getConnection(
                    "jdbc:mysql://mysql.comp.polyu.edu.hk:3306/", "08065312d", "xxxx");
            stmt = con.createStatement();
            stmt.execute("USE 08065312d");
            String id = "2";
            String oxygenvalue = request.getParameter("o2");
            String heartvalue = request.getParameter("heart");
            stmt = con.createStatement();
            String query="insert into Test values('"+id+"','"+oxygenvalue+"','"+heartvalue+"');";
            int rs = stmt.executeUpdate(query);
            out.flush();
            out.close();

        }
          catch(Exception ex)
            {

            }

}
}

//I can successfully add a new row into the DB, but the parameters(the reading) becomes null. I tried to change the program by directly println(request.getParmater("o2")) in servelet but not saving to db, the result are the same too(the reading becomes null null in the servlet). So I doubt that i can't get the parameter passed to the servlet or there is problem with the method request.getParmater, so what's going wrong?

//The problem I am having is sending the POST variable, which continually seems to be null
//I have been looking at forums for the past week, and all the httppost examples follow this almost exactly. I would appreciate any help that you are willing to give. Thanks a lot

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

西瑶 2025-01-15 07:39:13

首先检查这是否是 Android 问题,方法是使用 hackst.com 之类的内容发布到您的网址,然后报告后退。

Check if it is a Android issue to begin with at all by using something like hackst.com to post to your url and then report back.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文