Skip to content Skip to sidebar Skip to footer

Basicnetwork.performrequest - Unexpected Response Code 400 (post)

When I am trying to fetch data through rest api using Volley StringRequest or JsonObjectRequest. I always get 400 error. Its working fine with the postman. Http request method is P

Solution 1:

Below is the working example of POST request using Volley library. Add this to your build.gradle (Module:app) - compile 'com.mcxiaoke.volley:library:1.0.19'

StringRequest stringRequest = newStringRequest(Request.Method.POST, AppConfig.URL_LOGIN, newResponse.Listener<String>() {
                @OverridepublicvoidonResponse(String response) {
                    Log.d(TAG, response.toString());
                    progressDialog.dismiss();

                    JSONObject jsonObject = null;

                    try {
                        jsonObject = newJSONObject(response);

                        if(jsonObject.has("success")){
                            if(jsonObject.getBoolean("success") == true){
                                JSONObject userObject = newJSONObject(jsonObject.getString("user"));

                                Map<String, String> loginDetails = newHashMap<String, String>();
                                loginDetails.put(KEY_IS_LOGGED_IN, "true");
                                loginDetails.put(ACCESS_TOKEN, jsonObject.getString(ACCESS_TOKEN));
                                loginDetails.put(USERID, userObject.getString(USERID));
                                loginDetails.put(FIRSTNAME, userObject.getString(FIRSTNAME));
                                loginDetails.put(LASTNAME, userObject.getString(LASTNAME));
                                loginDetails.put(EMAIL, userObject.getString(EMAIL));

                                session = newSessionManager(_myActivity);
                                session.setLogin(loginDetails);

                                Intent intent = newIntent(LoginActivity.this, SearchActivity.class);
                                intent.putExtra("FROM_ACTIVITY", "LoginActivity");
                                startActivity(intent);
                                LoginActivity.this.finish();
                            }else{
                                btnLogin.setClickable(true);
                                btnLogin.setEnabled(true);
                                Toast.makeText(getApplicationContext(), "Username or Password does not matched!", Toast.LENGTH_LONG).show();
                            }
                        }

                    } catch (JSONException e) {
                        btnLogin.setClickable(true);
                        btnLogin.setEnabled(true);
                        e.printStackTrace();
                        Toast.makeText(getApplicationContext(), "Something went wrong, Please try agian", Toast.LENGTH_LONG).show();
                    }
                }
            }, newResponse.ErrorListener() {
                @OverridepublicvoidonErrorResponse(VolleyError error) {
                    Log.d(TAG, error.toString());
                    progressDialog.dismiss();

                    btnLogin.setClickable(true);
                    btnLogin.setEnabled(true);
                }
            }){
                @OverrideprotectedMap<String, String> getParams() {
                    // Posting parameters to login urlMap<String, String> params = newHashMap<String, String>();
                    params.put("grant_type", "password");
                    params.put("username", email);
                    params.put("password", password);
                    return params;
                }

                @OverridepublicMap<String, String> getHeaders() throws AuthFailureError {
                    Map<String,String> params = newHashMap<String, String>();
                    params.put("Content-Type", "application/x-www-form-urlencoded");
                    return params;
                }

                @OverridepublicStringgetBodyContentType(){
                    return"application/x-www-form-urlencoded";
                }
            };

            stringRequest.setRetryPolicy(newDefaultRetryPolicy(5000, DefaultRetryPolicy.DEFAULT_MAX_RETRIES, DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));

            // Adding request to request queueRequestQueue mRequestQueue = Volley.newRequestQueue(getApplicationContext());

            mRequestQueue.add(stringRequest);

Please comment if anyone faced issue with above code. Thanks to everyone who tried to help me.

Solution 2:

A 400 means bad request the data sent by the you to the server didn't follow the rules.So check your parameter or spelling of these parameter which is required by server. So check those try again hope it work for you :)

Solution 3:

Unexpected response code 400 means Bad Reuest.

This Response is caused by sending wrong parameters with the URL.

To solve:

  1. Check every parameter with spelling.

  2. Make sure if response is obtained in string or in object. stringRequest or JSONObjectReuest

  3. Also check Content-type

use this as Content-type

@OverridepublicMap<String, String> getHeaders() throws AuthFailureError {
    HashMap<String, String> headers = newHashMap<String, String>();
    headers.put("Content-Type", "application/json; charset=utf-8");
    return headers;
}

For JsonObjectReuest use:

HashMap<String, String> params = newHashMap<String, String>();
params.put("name", "BLah");

JsonObjectRequest req = newJsonObjectRequest(URL, newJSONObject(params),
newResponse.Listener<JSONObject>() {
   @OverridepublicvoidonResponse(JSONObject response) {
       try {
           Log.d("Response:", response.toString());
       } catch (JSONException e) {
           e.printStackTrace();
       }
   }
}, newResponse.ErrorListener() {
   @OverridepublicvoidonErrorResponse(VolleyError error) {
       Log.d("Error: ", error.getMessage());
   }
});

Post a Comment for "Basicnetwork.performrequest - Unexpected Response Code 400 (post)"