Monday, 19 June 2017

volley simple request with Authenticate Header (Authorisation)

public void makeStringReqToken(String url, final HashMap<String, String> params, VolleyCallback volleyCallback) {

        showDialog();

        this.mparams = params;
        Log.d("TAG", "PARAM " + mparams);
        this.mcallback = volleyCallback;


        JsonObjectRequest jsonObjReq = new JsonObjectRequest(Request.Method.POST,
                url, new JSONObject(mparams),
                new Response.Listener<JSONObject>() {

                    @Override
                    public void onResponse(JSONObject response) {

                        dismissDialog();
                        s = response.toString();
                        mcallback.onSuccess(s);
                        Log.d("TAG", "Response " + s);


                    }
                }, new Response.ErrorListener() {

            @Override
            public void onErrorResponse(VolleyError error) {
                dismissDialog();
                mcallback.onFailure(error.toString());

            }
        }) {

            /**
             * Passing some request headers
             * */
            @Override
            public Map<String, String> getHeaders() throws AuthFailureError {
                HashMap<String, String> headers = new HashMap<String, String>();
                headers.put("Authorization", "Basic ZGhhdmFsLnNAbXNwLWdyb3VwLmNvLnVrOjd0emhYWEN4Rit3PQ==");
                return headers;
            }

        };


        jsonObjReq.setRetryPolicy(new DefaultRetryPolicy(10000, 1, DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));

        // Adding request to request queue
        AppController.getInstance().addToRequestQueue(jsonObjReq,
                "string_req");

    }

No comments:

Post a Comment

Run/install/debug Android applications over Wi-Fi ?

Open Teminal/cmd --------------- Below steps is for Android 10 or lower Step 1 - Connect the device via USB and make sure debugging is work...