I want to add authorization header in android volley library , But
unfortunately I am getting errors "BasicNetwork.performRequest:
Unexpected response code 400"
I have tried many suggestions but it didn't work. Please help me to
sort out this issue. Thanks.
private void getLogin() {
final ProgressDialog mProgressDialog = new ProgressDialog(getActivity());
mProgressDialog.setMessage("Please wait...");
mProgressDialog.show();
RequestQueue mRequestQueue = Volley.newRequestQueue(getActivity());
StringRequest sr = new StringRequest(Request.Method.POST, URL, new Response.Listener<String>() {
#Override
public void onResponse(String response) {
Log.print("Login Response", "" + response);
mProgressDialog.dismiss();
}
}, new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError volleyError) {
Toast.makeText(getActivity(), volleyError.getMessage(), Toast.LENGTH_LONG).show();
}
}) {
#Override
protected Map<String, String> getParams() {
Map<String, String> paramsValue = new HashMap<String, String>();
paramsValue.put("username", new String(username));
paramsValue.put("password", new String(password));
paramsValue.put("grant_type", new String(grant_type));
return paramsValue;
}
#Override
public Map<String, String> getHeaders() throws AuthFailureError {
Map<String, String> params = new HashMap<String, String>();
params.put("Authorization", "Basic ZGVtb01vYmlsZUNsaWVudDpwYXNzd29yZDE=");
return params;
}
};
sr.setRetryPolicy(new DefaultRetryPolicy(
999999999,
DefaultRetryPolicy.DEFAULT_MAX_RETRIES,
DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));
mRequestQueue.add(sr);
}
Volley Library:
com.mcxiaoke.volley:library:1.0.19
Related
I have Url for Login. Its working and give response in Postman and Retrofit but not working with volley.
In volley its give error: com.android.volley.RedirectError
Why i geeting RedirectError. url working and give response in retrofit as well in postman.
Here is my Code:
public void Login(){
final String REGISTER_URL = "Here_My_Url";
StringRequest stringRequest = new StringRequest(Request.Method.POST, REGISTER_URL,
new Response.Listener<String>() {
#Override
public void onResponse(String response) {
Log.d("onResponse", response);
}
},
new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
Log.e("eee123",error.toString());
Toast.makeText(getApplicationContext(), error.getMessage(),
Toast.LENGTH_SHORT).show();
}
}) {
#Override
protected Map<String, String> getParams() throws AuthFailureError {
Map<String, String> params = new HashMap<String, String>();
params.put("user_email", st_email);
params.put("password", st_pass);
Log.e("params", " " + params);
}
return params;
}
#Override
public Map<String, String> getHeaders() throws AuthFailureError {
Map<String, String> headers = new HashMap<>();
headers.put("Content-Type", "application/json");
return headers;
}
};
stringRequest.setRetryPolicy(new DefaultRetryPolicy(0, -1,
DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));
stringRequest.setShouldCache(false);
VolleySingleton.getInstance(this).addToRequestQueue(stringRequest);
}
I am using this code
StringRequest stringRequest = new StringRequest(Request.Method.POST,
uploadUrl,
new Response.Listener<String>() {
#Override
public void onResponse(String response) {
Log.d("sfhyoutubeghhj",response);
}
},
new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
//loading.dismiss();
}
}) {
#Override
protected Map<String, String> getParams() throws AuthFailureError {
Map<String, String> prams = new HashMap<>();
prams.put("aaaa", "1111");
prams.put("bbbb", "2222");
return prams;
}
#Override
public Map<String, String> getHeaders() throws AuthFailureError {
HashMap<String, String> headers = new HashMap<String, String>();
headers.put("Content-Type", "application/json; charset=utf-8");
headers.put("User-agent", "My useragent");
return headers;
}
};
RequestQueue requestQueuea = Volley.newRequestQueue(this);
requestQueuea.add(stringRequest);
Request is going to server and response is also coming but its not sending any variable from android either in GET or POST method
and i m just using
print_r($_REQUEST);
at PHP end
I think there was a problem with Request queue only
working fine with this code # Ali Azhar's Answer
StringRequest sr = new StringRequest(Request.Method.POST, url , new Response.Listener<String>() {
#Override
public void onResponse(String response) {
Log.d(TAG, response.toString());
}
}, new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
VolleyLog.d(TAG, "Error: " + error.getMessage());
Log.d(TAG, ""+error.getMessage()+","+error.toString());
}
}){
#Override
protected Map<String,String> getParams(){
Map<String, String> params = new HashMap<String, String>();
params.put("id", "28");
params.put("value", "1");
return params;
}
#Override
public Map<String, String> getHeaders() throws AuthFailureError {
Map<String,String> headers = new HashMap<String, String>();
headers.put("Content-Type","application/x-www-form-urlencoded");
headers.put("abc", "value");
return headers;
}
};
AppController.getInstance().addToRequestQueue(sr);
Volley request showing com.android.volley.ServerError also it is neccessary to implement getHeaders() method? What is exact use of this method?
send.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
JsonObjectRequest jsonObjectRequest = new JsonObjectRequest
(Request.Method.POST, URL, null, new Response.Listener<JSONObject>() {
#Override
public void onResponse(JSONObject response) {
Toast.makeText(getApplicationContext(),response.toString(),Toast.LENGTH_LONG).show();
}
}, new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
error.printStackTrace();
}
}){
#Override
protected Map<String,String> getParams(){
Map<String,String> params = new HashMap<String, String>();
params.put("username","Admin");
params.put("password", "123456789");
return params;
}
#Override
public Map<String, String> getHeaders() throws AuthFailureError {
Map<String, String> params = new HashMap<String, String>();
params.put("Content-Type", "application/x-www-form-urlencoded");
return params;
}
};
RequestQueue requestQueue = Volley.newRequestQueue(getApplicationContext());
requestQueue.add(jsonObjectRequest);
}
});
Generally this type of error occurs when you are using poor internet connection or else your server goes down. There is no need to implement getHeaders() method. Try to just check your connection or server connection.
Use the android volley library,
compile 'com.android.volley:volley:1.0.0'
JsonObjectRequest jsonObjReq = new JsonObjectRequest(Method.POST,
url, null,
new Response.Listener<JSONObject>() {
#Override
public void onResponse(JSONObject response) {
Log.d(TAG, response.toString());
Toast.makeText(getApplicationContext(),response.toString(),Toast.LENGTH_LONG).show();
}
}, new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
Log.d(TAG, "Error: " + error.getMessage());
}
}) {
#Override
protected Map<String, String> getParams() {
Map<String, String> params = new HashMap<String, String>();
params.put("username","Admin");
params.put("password", "123456789");
return params;
}
};
RequestQueue requestQueue = Volley.newRequestQueue(getApplicationContext());
requestQueue.add(jsonObjectRequest);
Hope this will help you (Happy Coding !).
I want to know how to send volley login request if the login request url is
username:password http://login_url
in this format. It would be a great help if a sample code is available.
the purpose is to send login request to a django framework based website.
Thanks in advance
first create a string request ->>
StringRequest stringRequest = new StringRequest(Request.Method.POST, REGISTER_URL,
new Response.Listener<String>() {
#Override
public void onResponse(String response) {
Toast.makeText(MainActivity.this,response,Toast.LENGTH_LONG).show();
}
},
new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
Toast.makeText(MainActivity.this,error.toString(),Toast.LENGTH_LONG).show();
}
});
then, you should override the getParams() method!
#Override
protected Map<String,String> getParams(){
Map<String,String> params = new HashMap<String, String>();
params.put(KEY_USERNAME,username);
params.put(KEY_PASSWORD,password);
params.put(KEY_EMAIL, email);
return params;
add the request to request queue!
RequestQueue requestQueue = Volley.newRequestQueue(this);
requestQueue.add(stringRequest);
three easy steps :)
feel free to ask any doubts
try this
// Tag used to cancel the request
String tag_json_obj = "json_obj_req";
String url = "http:login_url";
ProgressDialog pDialog = new ProgressDialog(this);
pDialog.setMessage("Loading...");
pDialog.show();
JsonObjectRequest jsonObjReq = new JsonObjectRequest(Method.POST,
url, null,
new Response.Listener<JSONObject>() {
#Override
public void onResponse(JSONObject response) {
//your response
Log.d(TAG, response.toString());
pDialog.hide();
}
}, new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
VolleyLog.d(TAG, "Error: " + error.getMessage());
pDialog.hide();
}
}) {
#Override
protected Map<String, String> getParams() {
Map<String, String> params = new HashMap<String, String>();
params.put("name", "username");
params.put("email", "abc#dffg.info");
params.put("password", "password123");
return params;
}
};
// Adding request to request queue
AppController.getInstance().addToRequestQueue(jsonObjReq, tag_json_obj);
i'm trying to update some data with android volley but not working. and with postman it works.
my code:
private void aceptarAlerta()
{
mRequestQueue = VolleySingleton.getInstance().getmRequestQueue();
SharedPreferences preferencia = getSharedPreferences("ComuniUsuario", Context.MODE_PRIVATE);
token = preferencia.getString("ComuniToken", null);
StringRequest stringRequest = new StringRequest(Request.Method.PUT, url,
new Response.Listener<String>() {
#Override
public void onResponse(String response) {
Log.d("Respuesta: ", response);
Toast.makeText(getApplicationContext(), response, Toast.LENGTH_LONG).show();
}
},
new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
Log.d("Error.Response", error.toString());
Toast.makeText(getApplicationContext(), error.toString(), Toast.LENGTH_LONG).show();
}
}) {
#Override
protected Map<String, String> getParams() {
Map<String, String> params = new HashMap<String, String>();
params.put("estado", "true");
params.put("cuidador", Tmensaje[2]);
return params;
}
#Override
public Map<String, String> getHeaders() throws AuthFailureError {
HashMap<String, String> headers = new HashMap<String, String>();
headers.put("Authorization", "Token " + token);
return headers;
}
#Override
public RetryPolicy getRetryPolicy() {
return new DefaultRetryPolicy(
15000,
DefaultRetryPolicy.DEFAULT_MAX_RETRIES,
DefaultRetryPolicy.DEFAULT_BACKOFF_MULT);
}
};
mRequestQueue.add(stringRequest);
}
and if i delete params from code i dont get any error response from server.
i don't know what's the problem, i think have a logical error.