Home » Android » Android Volley ImageLoader – How to use Basic HTTP Authorization?

Android Volley ImageLoader – How to use Basic HTTP Authorization?

Posted by: admin June 15, 2020 Leave a comment

Questions:

I want to use Volley’s NetworkImageView to load images from my REST API that needs Basic HTTP Authorization. So i need to add headers to HTTP Request.

I’ve made up following approaches:

Override Request.getHeaders() – as described in this question.
This would be fine, but the problem is that ImageLoader has new ImageRequest() hardcoded so I can’t pass my Request implementation into ImageLoader and it cannot be easily inherited and tweaked (the method i’d need to reimplement uses private properties).

The solution is to modify Volley library itself (what i’d like to avoid).

Use Custom HttpClientStack – as described here.
Using this approach i’d be able to intercept HTTP comunication and add necessary headers. But I think this is not the right way to do – I’d loose automatic selection of HttpClient by Volley (Gingerbread vs. HC and IC).


Is there some simplier way to achive this that I’m missing?

How to&Answers:

I have overriden getHeaders() as well. So far I haven’t found a way to do that more easily.

See this example https://github.com/njzk2/VolleyTwitter/blob/master/src/com/njzk2/twitterbrowser/TokenRequest.java of an overriden Request to include the Authorization header.

From Volley code, I don’t see any way of adding custom headers if not by overriding the Request object.

Moreover, I don’t see how it can be easily added given the structure of Volley, as for the Images, ImageRequests are created by the ImageLoader.

If I were to modify Volley to allow this, I would make it possible to use a custom class extends ImageRequest in the ImageLoader. The anonymous ImageRequest class in ImageLoader makes it a bit complicated, though.

Answer:

I think HTTP stacks are the way to go. There is no loss of automatic HttpClient selection if you do your overrides based on SDK version, exactly like Volley does.

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.GINGERBREAD) {
        HurlStack stack = new HurlStack() {
            @Override
            public HttpResponse performRequest(Request<?> request, Map<String, String> headers)
                throws IOException, AuthFailureError {

                headers.putAll(MyApp.getAuthParams());

                return super.performRequest(request, headers);
            }
        };

        requestQueue = Volley.newRequestQueue(getApplicationContext(), stack);

    } else {
        HttpClientStack stack = new HttpClientStack(AndroidHttpClient.newInstance("volley/0")) {
            @Override
            public HttpResponse performRequest(Request<?> request, Map<String, String> headers)
                throws IOException, AuthFailureError {

                headers.putAll(MyApp.getAuthParams());

                return super.performRequest(request, headers);
            }
        };

        requestQueue = Volley.newRequestQueue(getApplicationContext(), stack);
    }

See Volley source (line 53).