Basicnetwork.performrequest - Unexpected Response Code 400 (post)
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:
Check every parameter with spelling.
Make sure if response is obtained in
string
or inobject
.stringRequest
orJSONObjectReuest
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)"