f3oioq
Last Updated: February 25, 2016
·
4.826K
· aferiat
Johnnyb012

Simple GET request using Volley android, basic authentifications

a simple way to do a get request using google's library Volley and basic authentification:

JsonObjectRequest request = new JsonObjectRequest("GET Url", null,
new Response.Listener < JSONObject > () {@Override
    public void onResponse(JSONObject response) {

        try {
            //read your json here
        }
    } catch (JSONException jsone) {

    } catch (Exception e) {
        // log and show error message with error code;

    }


}
},
new Response.ErrorListener() {

    @Override
    public void onErrorResponse(VolleyError error) {

        // log and show error message with error code;

    }
}) {@Override
    public Map < String, String > getHeaders() throws AuthFailureError {
        HashMap < String, String > headers = new HashMap < String, String > ();
        String encodedCredentials = Base64.encodeToString("passwordandlogin".getBytes(), Base64.NO_WRAP);
        headers.put("Authorization", "Basic " + encodedCredentials);

        return headers;
    }
};
RetryPolicy policy = new DefaultRetryPolicy("2000", DefaultRetryPolicy.DEFAULT_MAX_RETRIES, DefaultRetryPolicy.DEFAULT_BACKOFF_MULT);
request.setRetryPolicy(policy);

// invoke the server to retrieve data
VolleyApplication.getInstance().getRequestQueue().add(request);
Say Thanks
Respond