如何使用httppost方法从android应用程序向servlet发送数据?

时间:2013-10-08 17:04:05

标签: android servlets http-post

我正在尝试从android应用程序向servlet发送字符串数据,但我在servlet中获得了null值。 在这里我提供我的代码

Android代码是:

HttpPost post = new HttpPost("http://localhost:8081/HttpGetServlet/HttpGetServlet");

List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(1);
nameValuePairs.add(new BasicNameValuePair("Email", email));
nameValuePairs.add(new BasicNameValuePair("Passwd", password));

post.setEntity(new UrlEncodedFormEntity(nameValuePairs));
HttpResponse response = client.execute(post);

Sevlet代码是:

public class HelloWorldServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;

    public HelloWorldServlet() {
        super();
    }

    protected void doGet(HttpServletRequest request,HttpServletResponseresponse)throwsServletException, IOException {

        PrintWriter out = response.getWriter();
        String fname = (String) request.getParameter("param1");

        out.println("Hello my 1st name is : "+fname);
        String lname = (String) request.getParameter("lastName");
        out.println("Hello my last name is : "+lname);

    }
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

        PrintWriter out = response.getWriter(); 
            String json_array = request.getParameter("username");
            out.println("hi ::::::::::"+json_array);
    }
}

任何人都有想法请帮助我。提前致谢。

1 个答案:

答案 0 :(得分:1)

您可以设置参数EmailPasswd,但请尝试阅读username。很明显,这将是null