Please tell me how to get the body of the sent POST request in AsyncHttpClient in the onFailure method

47 Views Asked by At

here is an example of my code. (Dependencies implementation 'com.loopj.android:android-async-http:1.4.11')

AsyncHttpClient asyncHttpClient = new AsyncHttpClient();

asyncHttpClient.addHeader("secret", "123");

JSONObject jsonParams = new JSONObject();

onParams.put("name", "Jon");

asyncHttpClient.post(menu.this, "http://192.168.1.104:8000/", jsonEntity, "application/json", new JsonHttpResponseHandler() {
                @Override
                public void onSuccess(int statusCode, Header[] headers, JSONObject response) {
                    super.onSuccess(statusCode, headers, response);

                }

                @Override
                public void onFailure(int statusCode, Header[] headers, Throwable throwable, JSONObject errorResponse ) {
                    super.onFailure(statusCode, headers, throwable, errorResponse);
                     //////////////??????????????????????????????????/////////////////////////
                }
            });

If the server does not respond, I get into the onFailure method with a TimeOut error, how can I get the body of my request that I sent ( onParams.put("name", "Jon");) ?

i want to get the body of the request i sent

1

There are 1 best solutions below

1
MESP On

You could just use the reference from above. Also you made a little error in the second use of jsonParams, you typed onParams here.

AsyncHttpClient asyncHttpClient = new AsyncHttpClient();

asyncHttpClient.addHeader("secret", "123");

JSONObject jsonParams = new JSONObject();

jsonParams.put("name", "Jon");

asyncHttpClient.post(menu.this, "http://192.168.1.104:8000/", jsonEntity, "application/json", new JsonHttpResponseHandler() {
                @Override
                public void onSuccess(int statusCode, Header[] headers, JSONObject response) {
                    super.onSuccess(statusCode, headers, response);

                }

                @Override
                public void onFailure(int statusCode, Header[] headers, Throwable throwable, JSONObject errorResponse ) {
                    super.onFailure(statusCode, headers, throwable, errorResponse);
                    log.d("TAG", jsonParams);
                }
            });