Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to send Authorization header in Android using Volley library?

Tags:

How can I send Authorization header using Volley library in Android for GET method?

This is my request code:

JsonObjectRequest req = new JsonObjectRequest(Request.Method.GET, url,             null, new Response.Listener<JSONObject>() {          @Override         public void onResponse(JSONObject response) {             Log.d("Response", response.toString());             pd.dismiss();                          Toast.makeText(MainActivity.this, "" + response.toString(), Toast.LENGTH_SHORT).show();         }     }, new Response.ErrorListener() {         @Override         public void onErrorResponse(VolleyError error) {             VolleyLog.d("Error", "Error: " + error.getMessage());             Toast.makeText(MainActivity.this, "" + error.getMessage(), Toast.LENGTH_SHORT).show();             pd.dismiss();          }     }) {         @Override         public Map<String, String> getHeaders() throws AuthFailureError {             HashMap<String, String> headers = new HashMap<String, String>();             headers.put("Authorization", "2e96e0a4ff05ba86dc8f778ac49a8dc0");             return headers;         }     }; 
like image 716
Velpukonda Ramesh Avatar asked May 16 '17 11:05

Velpukonda Ramesh


People also ask

How do you send a header in volley?

If you need to add custom headers to your volley requests, you can't do this after initialisation, as the headers are saved in a private variable. Instead, you need to override the getHeaders() method of Request.

How do I send an authorization header request?

To send a GET request with a Bearer Token authorization header, you need to make an HTTP GET request and provide your Bearer Token with the Authorization: Bearer {token} HTTP header.

Does browser send authorization header?

Only types like Basic , NTLM of Authorization header is sent automatically by browser in following cases: The Authorization header field allows a user agent to authenticate itself with an origin server – usually, but not necessarily, after receiving a 401 (Unauthorized) response.

What is volley client error?

Indicates that the server responded with an error response indicating that the client has erred. For backwards compatibility, extends ServerError which used to be thrown for all server errors, including 4xx error codes indicating a client error.


2 Answers

StringRequest request = new StringRequest(Request.Method.POST, YourUrl, new Response.Listener<String>() {     @Override     public void onResponse(String response) {         if (!response.equals(null)) {             Log.e("Your Array Response", response);                             } else {             Log.e("Your Array Response", "Data Null");         }     }  }, new Response.ErrorListener() {     @Override     public void onErrorResponse(VolleyError error) {         Log.e("error is ", "" + error);     } }) {       //This is for Headers If You Needed     @Override     public Map<String, String> getHeaders() throws AuthFailureError {         Map<String, String> params = new HashMap<String, String>();         params.put("Content-Type", "application/json; charset=UTF-8");         params.put("token", ACCESS_TOKEN);         return params;     }   //Pass Your Parameters here     @Override     protected Map<String, String> getParams() {         Map<String, String> params = new HashMap<String, String>();         params.put("User", UserName);         params.put("Pass", PassWord);         return params;     } }; RequestQueue queue = Volley.newRequestQueue(getApplicationContext()); queue.add(request); 
like image 153
Ramaraju Avatar answered Sep 19 '22 09:09

Ramaraju


Try following code:

@Override     public Map<String, String> getHeaders() throws AuthFailureError {         String credentials = "username" + ":" + "password";         String base64EncodedCredentials = Base64.encodeToString(credentials.getBytes(), Base64.NO_WRAP);         HashMap<String, String> headers = new HashMap<>();         headers.put("Authorization", "Basic " + base64EncodedCredentials);         return headers;     } 
like image 30
nnn Avatar answered Sep 20 '22 09:09

nnn