How to send post request with x-www-form-urlencoded body with loopj-async-http library

beginner

i m trying to post data in body with x-www-form-urlencoded but i failed

    private void sendData(final String toekn) {

    RequestParams params = new RequestParams();
    params.put("_token", toekn);
    StringEntity entity=null;
    try {
        entity = new StringEntity(params.toString());
        entity.setContentType(new BasicHeader(HTTP.CONTENT_TYPE, "application/x-www-form-urlencoded; charset=UTF-8"));
    } catch (UnsupportedEncodingException e) {
        e.printStackTrace();
    }
    PropClient.post(getBaseContext(), "", entity, new JsonHttpResponseHandler() {

        @Override
        public void onSuccess(int statusCode, Header[] headers, JSONObject response) {

            Log.e("see", response.toString());
            Toast.makeText(SplashActivity.this, response.toString()+"", Toast.LENGTH_SHORT).show();

        }

        @Override
        public void onSuccess(int statusCode, Header[] headers, JSONArray timeline) {
            // Pull out the first event on the public timeline
            // Do something with the response
        }

        @Override
        public void onFailure(int statusCode, Header[] headers, String response, Throwable e) {

        }

        @Override
        public void onRetry(int retryNo) {
            // called when request is retried
        }
    });
}

above is code i have tried but always get failure .. api works perfect in postman i have attached pic for understanding params ..

image1

image2

static class

public class PropClient { private static final String BASE_URL = "";

private static AsyncHttpClient client = new AsyncHttpClient();

public static void get(String url, RequestParams params, AsyncHttpResponseHandler responseHandler) {
    client.get(getAbsoluteUrl(url), params, responseHandler);
}

public static void post(Context context, String url, StringEntity entity, AsyncHttpResponseHandler responseHandler) {
  //  client.addHeader("Accept", "application/json");
    client.addHeader("Content-Type", "application/x-www-form-urlencoded");
    entity.setContentType("application/json");
    client.setUserAgent("android");
    client.post(context, getAbsoluteUrl(url), entity, "application/json", responseHandler);
}

private static String getAbsoluteUrl(String relativeUrl) {
    String url = BASE_URL + relativeUrl;
    return url;
}

}

altu

if u want to use volley u can check my answer

compile 'com.android.volley:volley:1.0.0'

  private void getVolley(final String token, String url) {
    final RequestQueue requestQueue = Volley.newRequestQueue(getApplicationContext());
    StringRequest jsonObjRequest = new StringRequest(Request.Method.POST, url, new Response.Listener<String>() {
        @Override
        public void onResponse(String response) {
            Toast.makeText(SplashActivity.this, response + "  success", Toast.LENGTH_SHORT).show();
            Log.e("volley", response);

        }
    }, new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error) {
            Toast.makeText(SplashActivity.this, error.toString() + "", Toast.LENGTH_SHORT).show();
        }
    }) {
        @Override
        public String getBodyContentType() {
            return "application/x-www-form-urlencoded; charset=UTF-8";
        }

        @Override
        public Map<String, String> getHeaders() throws AuthFailureError {
            Map<String, String> params = new HashMap<String, String>();
            params.put("User-Agent", "android");
            params.put("Content-Type", "application/x-www-form-urlencoded");

            return params;
        }

        @Override
        protected Map<String, String> getParams() throws AuthFailureError {
            Map<String, String> params = new HashMap<String, String>();
            params.put("_token", token);
            return params;
        }
    };
    jsonObjRequest.setRetryPolicy(new DefaultRetryPolicy(30 * 1000,
            DefaultRetryPolicy.DEFAULT_MAX_RETRIES,
            DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));


    requestQueue.add(jsonObjRequest);
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Spring - wrong encoding POST request with x-www-form-urlencoded

How to send post request with x-www-form-urlencoded body

Post a x-www-form-urlencoded request from React Native

Why does Axios send my POST request with Content-Type application/x-www-form-urlencoded when using a string as the request body?

RestSharp post request - Body with x-www-form-urlencoded values

Jersey client Post Request with x-www-form-urlencoded Fails

How do I post data using okhttp library with content type x-www-form-urlencoded?

Swift - How to send POST request with "x-www-form-urlencoded" content-type

Swift 4 send POST request as x-www-form-urlencoded

How is Laravel decoding HTTP request body Content-Type: application/x-www-form-urlencoded when using api call

How do I get raw request body using servicestack with content-type set to application/x-www-form-urlencoded?

How to send bearer token and x-www-form-urlencoded data using Node Request

How to POST content as application/x-www-form-urlencoded

How to POST x-www-form-urlencoded in retrofit

How to send a POST request with Content-Type "application/x-www-form-urlencoded"

How to correctly send application/x-www-form-urlencoded in Angular post call

Karate DSL: How to add specific key and value (XML) as x-www-form-urlencoded to body of request?

How to post request with spring boot web-client for Form data for content type application/x-www-form-urlencoded

How to send post request with content-type x-www-form-urlencoded android retrofit

PYTHON: requests.post() how to send request_body encoded as application/x-www-form-urlencoded

Akka HTTP how to POST singleRequest with Content-Type application/x-www-form-urlencoded

How test Post request with custom object in content type application/x-www-form-urlencoded?

why can't Volley String request send Body parameters with POST method in x-www-form-urlencoded?

How to send x-www-form-urlencoded in a post request in webclient?

how to use the simple-oauth2 library to make request application/x-www-form-urlencoded?

how to post body x-www-form-urlencoded using webclient?

How to do a post request using FORM-DATA or x-www-form-urlencoded with Android?

Making a MarkLogic xdmp:http-post() request with x-www-form-urlencoded request body

How to sent Http POST request with application/x-www-form-urlencoded