user3794585
user3794585

Reputation: 203

LoopJ AndroidAsyncHttp Server Response after POST Android

I am using LoopJ AndroidAsyncHttp to get/post data from/to my server. I know that when I call the .get method, the JSON response is stored in the s string as shown below:

    client = new AsyncHttpClient();
    client.get("https://example.com/generateToken.php", new TextHttpResponseHandler() {
        @Override
        public void onFailure(int i, Header[] headers, String s, Throwable throwable) {

        }

        @Override
        public void onSuccess(int i, Header[] headers, String s) {}

If, however, I am posting to the server, how do I get my server's response? I used the template here for posting: http://loopj.com/android-async-http/doc/com/loopj/android/http/RequestParams.html

Specifically my code looks something like this:

    params = new RequestParams();

    params.put("first_name", firstName);
    params.put("last_name", lastName);
   client = new AsyncHttpClient();
    client.post("xxx.com/createCustomer.php", params, responseHandler);

My server takes these inputs and returns a token. How do I retrieve this token? Do I have to call the .get method as before immediately after the .post code above? Or is it automatically echoed somehow? Thanks

Upvotes: 0

Views: 1175

Answers (1)

Michele
Michele

Reputation: 6131

Its just the same as your Get request.

The Last Paramter in the post Method needs an suitable ResponseHandler.

  params = new RequestParams();

    params.put("first_name", firstName);
    params.put("last_name", lastName);
   client = new AsyncHttpClient();
    client.post("xxx.com/createCustomer.php", params, new TextHttpResponseHandler() {
        @Override
        public void onFailure(int i, Header[] headers, String s, Throwable throwable) {

        }

        @Override
        public void onSuccess(int i, Header[] headers, String s) {}
);

Upvotes: 1

Related Questions