//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
公共类FypServletTest扩展了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)
            {

            }

}
}
//我可以成功地在数据库中添加新行,但是参数(读取)变为空。我试图通过在servelet中直接println(request.getparmater(“o2”))来更改程序,但没有保存到db,结果也是一样的(在servlet中读取变为空空)。因此,我怀疑我无法将参数传递给servlet,或者方法request.getparmater有问题,那么出了什么问题?
//我遇到的问题是发送post变量,它似乎一直是空的
//过去一周我一直在看论坛,所有的httppost示例几乎都是这样的。如果你愿意帮忙,我将不胜感激。谢谢

最佳答案

通过使用hackst.com之类的东西发布到您的url,然后报告回来,检查这是否是一个android问题。

07-24 21:19