0

I am using the LoopJ AndroidAsyncHttp Library in an Android Project but when I try to get the parameters at the server side, I get null. I tried using PHP & Java same result. I am 100% sure my server side is working since I use postman chrome plugin and it works:

Client Code:

public void sendRequest(View v) {

    try {

        AsyncHttpClient client = new AsyncHttpClient();

        // HashMap<String, String> paramsMap = new HashMap<String,
        // String>();
        // paramsMap.put("action", "Action Value");
        // RequestParams params = new RequestParams(paramsMap);

        RequestParams params = new RequestParams();
        params.add("action", "insert");

        AsyncHttpResponseHandler handler = new AsyncHttpResponseHandler() {

            @Override
            public void onSuccess(int statusCode, Header[] headers,
                    byte[] body) {
                System.out.println("On Success --> Status Code: "
                        + statusCode);
                String response = new String(body);
                System.out.println("On Success --> Response: " + response);
            }

            @Override
            public void onFailure(int statusCode, Header[] headers,
                    byte[] body, Throwable error) {
                System.out.println("On Failure --> Status Code: "
                        + statusCode);
            }
        };

        String url1 = "http://192.168.1.9:8080/Tester";
        String url2 = "http://192.168.1.6/test";
        System.out.println("--->>> Params: " + params.toString());
         client.post(url1, params, handler);


    } catch (Exception e) {
        System.out.println("--> Exception: " + e.getMessage());
    }
}

Server Code (Java):

protected void processRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    System.out.println("------->>> " + request.getParameter("action"));
    response.setContentType("text/html;charset=UTF-8");
    try (PrintWriter out = response.getWriter()) {
        /* TODO output your page here. You may use following sample code. */
        out.println("<!DOCTYPE html>");
        out.println("<html>");
        out.println("<head>");
        out.println("<title>Servlet FrontEndController</title>");            
        out.println("</head>");
        out.println("<body>");
        out.println("<h1>Servlet FrontEndController at " + request.getContextPath() + "</h1>");
        out.println("</body>");
        out.println("</html>");
    }
}
Stephane Landelle
  • 6,990
  • 2
  • 23
  • 29

0 Answers0