user2117089
user2117089

Reputation: 21

Android - LoopJ AndroidAsyncHttp - No POST params

I have a problem with POST requests on LoopJ AndroidAsyncHttp. The paramters I define are just not sent by the client.

Like in the examples I have built an own class "RestClient" for the AsyncHTTPClient

public class RestClient {
  private static final String BASE_URL = "http://www.myserver.de/picbox/";

  private static AsyncHttpClient client = new AsyncHttpClient();

  /**
   * GET-Request
 * @param relativeURL
 * @param params
 * @param responseHandler
 */
public static void get(String relativeURL, RequestParams params, AsyncHttpResponseHandler responseHandler) {
      //System.out.println(getAbsoluteUrl(relativeURL));
      client.get(getAbsoluteUrl(relativeURL), params, responseHandler);
      client.getHttpClient().getParams().setParameter(ClientPNames.ALLOW_CIRCULAR_REDIRECTS, true);
  }


  /**
   * POST-Request
 * @param relativeURL
 * @param params
 * @param responseHandler
 */
public static void post(String relativeURL, RequestParams params, AsyncHttpResponseHandler responseHandler) {
      client.post(getAbsoluteUrl(relativeURL), params, responseHandler);
      client.getHttpClient().getParams().setParameter(ClientPNames.ALLOW_CIRCULAR_REDIRECTS, true);
  }


  /**
   * Construct final URL for request
 * @param relativeUrl
 * @return
 */
private static String getAbsoluteUrl(String relativeUrl) {
      return BASE_URL + relativeUrl;
  }
}

Within my activity I use the Client this way:

private boolean uploadImage() {

    String img64 = encodeTobase64(bitmap);
    Log.d(TAG, img64);

    RequestParams params = new RequestParams();
    params.put("userid", userID);
    params.put("username", username);
    params.put("img64", img64);

    RestClient.post("pictures/upload", params, new AsyncHttpResponseHandler() {

        // If request is successful
        @Override
        public void onSuccess(String response) {
            Log.d(TAG, response);
            //
        }

        // If request was not succesful, show error
        @Override
        public void onFailure(Throwable error, String content) {
            Log.d(TAG, error.toString());
        }
    });
    return true;

}

On the server side I have a PHP script, answering the requests. At the moment the script just returns the $_POST and $_REQUEST variables:

var_dump($_POST);
var_dump($_REQUEST);

The request works and the onSuccess() method is reached, but as a response i just get

array(0){

}
array(0){

}

It seems that the params are just not sent by AsyncHTTPClient :( The variables userid, username and img64 are properly initialized as calss variables and not null. I have no clue, what could be the problem.

Hoping for some help. Max

Upvotes: 1

Views: 3035

Answers (1)

Syed Farabi
Syed Farabi

Reputation: 175

I had the same issue.

Just try to add as string not as int as below

params.put("userid", String.valueOf(userID));

This is working for me.

I think the param is always expecting as string not any other type.

Upvotes: 1

Related Questions