Android Volley ImageLoader - How to use Basic HTTP Authorization?
Asked Answered
A

2

8

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?

Allowable answered 4/6, 2013 at 13:54 Comment(0)
Z
6

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.

Zinciferous answered 4/6, 2013 at 13:57 Comment(2)
Thanks for your reply. It's pity that no simplier way exists. I'll do it the way you suggested - by modification of ImageLoader to use custom ImageRequest class (by creating and passing to ImageLoader a ImageRequestFactory). If I'm right, the anonymous are just Listeners passed to ImageRequest, not ImageRequest itself, so it shouldn't be a problem.Allowable
there is an anonymous ImageRequest to call onGetImageSuccess(cacheKey, response); in onResponse (though I would have put that directly in ImageRequest, or even in Request.)Zinciferous
C
8

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).

Caves answered 25/7, 2013 at 14:20 Comment(1)
As part of Android M release , HttpResponse class removed from sdkScouting
Z
6

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.

Zinciferous answered 4/6, 2013 at 13:57 Comment(2)
Thanks for your reply. It's pity that no simplier way exists. I'll do it the way you suggested - by modification of ImageLoader to use custom ImageRequest class (by creating and passing to ImageLoader a ImageRequestFactory). If I'm right, the anonymous are just Listeners passed to ImageRequest, not ImageRequest itself, so it shouldn't be a problem.Allowable
there is an anonymous ImageRequest to call onGetImageSuccess(cacheKey, response); in onResponse (though I would have put that directly in ImageRequest, or even in Request.)Zinciferous

© 2022 - 2024 — McMap. All rights reserved.