The Java – Post method does not work in Android’s AsyncHttpClient

Post method does not work in Android’s AsyncHttpClient… here is a solution to the problem.

Post method does not work in Android’s AsyncHttpClient

Hello, I

am developing android application using java restfull webservices here, when I use“”) I have a problem calling the web service through the post method, all my values are empty when calling the network service. I don’t know why this is happening. When I use client.get(“), it works fine. I was able to register correctly via client.get. But I want to use please help me and let me know my error.

public void registerUser(View view){

String name = nameET.getText().toString();

String email = emailET.getText().toString();

String password = pwdET.getText().toString();

RequestParams params = new RequestParams();

if(Utility.isNotNull(name) && Utility.isNotNull(email) && Utility.isNotNull(password)){

            params.put("name", name);

params.put("username", email);

params.put("password", password);
             Invoke RESTful Web Service with Http parameters

public void invokeWS(RequestParams params){
    System.out.println("params=====  "+params);;

**AsyncHttpClient client = new AsyncHttpClient();"http://localhost:8080/DemoForAndroid/register/doregister",params ,new AsyncHttpResponseHandler()** {
         When the response returned by REST has Http response code '200'
         public void onSuccess(String response) {
             Hide Progress Dialog
             System.out.println("response=======   "+response);
             try {
                       JSON Object
                     JSONObject obj = new JSONObject(response);
                      When the JSON response has status boolean value assigned with true
                          Set Default Values for Edit View controls
                          Display successfully registered message using Toast
                         Toast.makeText(getApplicationContext(), "You are successfully registered!", Toast.LENGTH_LONG).show();
                      Else display error message
                         Toast.makeText(getApplicationContext(), obj.getString("error_msg"), Toast.LENGTH_LONG).show();
             } catch (JSONException e) {
                  TODO Auto-generated catch block
                 Toast.makeText(getApplicationContext(), "Error Occured [Server's JSON response might be invalid]!", Toast.LENGTH_LONG).show();

          When the response returned by REST has Http response code other than '200'
         public void onFailure(int statusCode, Throwable error,
             String content) {
              Hide Progress Dialog
              When Http response code is '404'
             if(statusCode == 404){
                 Toast.makeText(getApplicationContext(), "Requested resource not found", Toast.LENGTH_LONG).show();
              When Http response code is '500'
             else if(statusCode == 500){
                 Toast.makeText(getApplicationContext(), "Something went wrong at server end", Toast.LENGTH_LONG).show();
              When Http response code other than 404, 500
                 Toast.makeText(getApplicationContext(), "Unexpected Error occcured! [Most common Error: Device might not be connected to Internet or remote server is not up and running]", Toast.LENGTH_LONG).show();

System.out.println("Client=======  "+client);

Log files

01-28 12:10:26.307: W/KeyCharacterMap(106): No keyboard for id 0
01-28 12:10:26.307: W/KeyCharacterMap(106): Using default keymap: /system/usr/keychars/qwerty.kcm.bin
01-28 12:11:33.238: I/System.out(274): params=====  [email protected]&name=Raju &password=123456
01-28 12:11:33.328: D/dalvikvm(274): GC_FOR_MALLOC freed 5941 objects / 275504 bytes in 57ms
01-28 12:11:33.378: I/System.out(274): Client=======
01-28 12:11:33.578: I/System.out(274): response=======   {"tag":"register","status":false,"error_msg":"Error occured"}
01-28 12:11:33.678: I/ARMAssembler(58): generated scanline__00000077:03515104_00000000_00000000 [ 33 ipp] (47 ins) at [0x360af8:0x360bb4] in 405396 ns
01-28 12:11:33.745: W/InputManagerService(58): Window already focused, ignoring focus gain of:$Stub$Proxy@450aa070

Restful web service code

public class Register {
  HTTP Get Method
 User usr = new User();
 Path: http://localhost/<appln-folder-name>/register/doregister
 Produces JSON as response
 Query parameters are parameters: http://localhost/<appln-folder-name>/register/doregister?name=pqrs&username=abc&password=xyz

public String doLogin(@QueryParam("name") String name, @QueryParam("username") String uname, @QueryParam("password") String pwd){
    String response = "";
    System.out.println("Inside doLogin "+uname+"  "+pwd);
    int retCode = registerUser(name, uname, pwd);
    System.out.println("ret code= "+retCode);
    if(retCode == 0){
        response = Utitlity.constructJSON("register",true);
    }else if(retCode == 1){
        response = Utitlity.constructJSON("register",false, "You are already registered");
    }else if(retCode == 2){
        response = Utitlity.constructJSON("register",false, "Special Characters are not allowed in Username and Password");
    }else if(retCode == 3){
        response = Utitlity.constructJSON("register",false, "Error occured");
    return response;


private int registerUser(String name, String uname, String pwd){
    System.out.println("Inside checkCredentials");
    System.out.println("name= "+name);
    System.out.println("uname= "+uname);
    System.out.println("pwd= "+pwd);
    int result = 3;
    if(Utitlity.isNotNull(uname) && Utitlity.isNotNull(pwd)){
        try {
            System.out.println("add user---------------");
           /* if(DBConnection.insertUser(name, uname, pwd)){*/
                new UserDao().addUser(usr);

System.out.println("RegisterUSer if");
                result = 0;
       /*     }*/
        } /*catch(SQLException sqle){
            System.out.println("RegisterUSer catch sqle");
            When Primary key violation occurs that means user is already registered
            if(sqle.getErrorCode() == 1062){
                result = 1;
            When special characters are used in name,username or password
            else if(sqle.getErrorCode() == 1064){
                result = 2;
        catch (Exception e) {
             TODO Auto-generated catch block
            System.out.println("Inside checkCredentials catch e ");
            result = 3;
        System.out.println("Inside checkCredentials else");
        result = 3;

return result;

Network service logs

Inside doLogin null  null
Inside checkCredentials
name= null
uname= null
pwd= null
Inside isNotNull
Inside checkCredentials else
ret code= 3


Replace @QueryParam with @FormParam in doLogin().
I think @QueryParam would be more like the expected GET parameter.

Related Problems and Solutions