简体   繁体   English

Android Volley ImageLoader - 如何使用基本HTTP授权?

[英]Android Volley ImageLoader - How to use Basic HTTP Authorization?

I want to use Volley's NetworkImageView to load images from my REST API that needs Basic HTTP Authorization. 我想使用Volley的NetworkImageView从我的REST API加载需要基本HTTP授权的图像。 So i need to add headers to HTTP Request. 所以我需要向HTTP Request添加标头。

I've made up following approaches: 我已经采取了以下方法:

Override Request.getHeaders() - as described in this question . 覆盖Request.getHeaders() - 如本问题中所述 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). 这样会很好,但问题是ImageLoadernew ImageRequest()硬编码所以我不能将我的Request实现传递给ImageLoader ,它不能轻易地继承和调整(我需要重新实现的方法使用私有属性)。

The solution is to modify Volley library itself (what i'd like to avoid). 解决方案是修改Volley库本身(我想避免的)。

Use Custom HttpClientStack - as described here . 使用自定义HttpClientStack -描述这里 Using this approach i'd be able to intercept HTTP comunication and add necessary headers. 使用这种方法,我将能够拦截HTTP通信并添加必要的标头。 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). 但我认为这不是正确的方法 - 我放弃了Volley自动选择HttpClient(Gingerbread vs. HC和IC)。


Is there some simplier way to achive this that I'm missing? 是否有一些简单的方法来实现这一点,我错过了?

I think HTTP stacks are the way to go. 我认为HTTP堆栈是要走的路。 There is no loss of automatic HttpClient selection if you do your overrides based on SDK version, exactly like Volley does. 如果您根据SDK版本进行覆盖,则不会丢失自动HttpClient选择,就像Volley一样。

    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). Volley来源 (第53行)。

I have overriden getHeaders() as well. 我也覆盖了getHeaders() 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. 请参阅此示例https://github.com/njzk2/VolleyTwitter/blob/master/src/com/njzk2/twitterbrowser/TokenRequest.java重写请求以包含Authorization标头。

From Volley code, I don't see any way of adding custom headers if not by overriding the Request object. 从Volley代码中,如果不是通过覆盖Request对象,我看不到添加自定义头的任何方法。

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. 此外,我不知道如何在给定Volley的结构的情况下轻松添加它,对于Images,ImageRequests是由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. 如果我要修改Volley来允许这个,我可以在ImageLoader中使用自定义类扩展ImageRequest。 The anonymous ImageRequest class in ImageLoader makes it a bit complicated, though. 但是ImageLoader中的匿名ImageRequest类使它有点复杂。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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