-1

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

安卓代码是:

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

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

4

1 回答 1

1

您设置了参数EmailPasswd但尝试读取username. 很明显,这将是null

于 2013-10-08T17:21:51.060 回答