简体   繁体   中英

How to send authorization header in android using volley library GET method

how to send Authorization header using volley library in android for GET method please help me thank you

  public void token(){
    SharedPreferences usuario = getActivity().getSharedPreferences(DataManager.SharedPreferences, Context.MODE_PRIVATE);
    String id = usuario.getString(DataManager.json_Id, "");
    final String token = usuario.getString(DataManager.json_Token,"");
    StringRequest request = new StringRequest(Request.Method.GET, urlReadUser, 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 volleyError) {
            try {
                String responseBody = new String(volleyError.networkResponse.data, "utf-8");
                JSONObject jsonObject = new JSONObject(responseBody);
                //loadingData.dismiss();
                if (jsonObject.getInt(DataManager.json_Code) == 400) {
                   // onDialogErrorResponse();
                }

            } catch (JSONException e) {
                //Handle a malformed json response
                Log.d("Response", String.valueOf(e));
            } catch (UnsupportedEncodingException error) {

                Log.d("Response", String.valueOf(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("TOKEN", token);
            return params;
        }
    };
    request.setRetryPolicy(new DefaultRetryPolicy(
            10000,
            DefaultRetryPolicy.DEFAULT_MAX_RETRIES,
            DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));
    requestQueue = Volley.newRequestQueue(getActivity());
    requestQueue.add(request);
    DiskBasedCache cache = new 
    DiskBasedCache(getActivity().getCacheDir(), 500 * 1024 * 1024);
    requestQueue = new RequestQueue(cache, new BasicNetwork(new 
    HurlStack()));
    requestQueue.start();
}

I am trying to make the request to the server to bring data by the GET method, I have to send the Token so that I input but it returns an error where it tells me that the way to insert the header token is wrong, I hope they can support me.

Authorization header generally sent as

params.put("Authorization", "bearer " +token);

Here bearer is auth type, it can be Basic as well as per your api's requirement.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM