I decided it myself ... This code will give you authentication with a username and password, and then this token will be placed in the header to receive data from the server ...
public void vollyRequestGetAuthToken() { RequestQueue queue = Volley.newRequestQueue(this); StringRequest request = new StringRequest(Request.Method.POST , "https://example.com/get-auth-token/", new Response.Listener<String>() { @Override public void onResponse(String response) { Toast.makeText(MainActivity.this, "RESPONSE: " + response, Toast.LENGTH_SHORT ).show(); System.out.println("RESPONSE: >>> " + response + "<<<"); } }, new Response.ErrorListener() { @Override public void onErrorResponse(VolleyError error) { Toast.makeText(MainActivity.this, "ERROR: " + error, Toast.LENGTH_SHORT ).show(); } }) { @Override protected Map<String,String> getParams(){ Map<String,String> params = new HashMap<String, String>(); params.put("username","*****"); params.put("password","*****"); return params; } @Override public Map<String, String> getHeaders() throws AuthFailureError { Map<String,String> params = new HashMap<String, String>(); params.put("Authorization", String.format("Basic %s", Base64.encodeToString( String.format("%s:%s", "<username>", "<password>").getBytes(), Base64.DEFAULT))); params.put("username" , "*****" ); params.put("password" , "*****" ); return params; } }; queue.add(request); }
, and now that you have an authentication token, use the following code to send an authentication token to receive data
@Override public Map<String, String> getHeaders() throws AuthFailureError { HashMap<String, String> headers = new HashMap<String, String>(); headers.put("Authorization", "Token <token>"); return headers; }
source share