2

On a listView, in its adapters' getView method, I need to request an image per ListView item in order to load a picture by item thanks to NetworkImageView. The problem is that I need to add authentication header to the request to allow the user the get pictures from server. I've read some solutions which are not possible to put in practice.

Thanks in advance...

Community
  • 1
  • 1
Hubert Solecki
  • 2,611
  • 5
  • 31
  • 63
  • 1
    Sorry, I don't understand `which are not possible to put in practice.`. Moreover, post your code and your logcat for more info – BNK Aug 26 '15 at 14:27

1 Answers1

2

I've find out how to set a Basic Authentication header into an ImageLoader. I was misunderstanding the answer on that link same topic. So credit goes to the real answerer. Anyway the trick was to add an HurlStack into the getRequestQueue method as follows:

public RequestQueue getRequestQueue()
{
    if (mRequestQueue == null) {

        HurlStack stack = new HurlStack() {
            @Override
            public HttpResponse performRequest(Request<?> request, Map<String, String> headers)
                throws IOException, AuthFailureError {

                String auth = "Basic " + Base64.encodeToString((GlobalVariables.getInstance().getWS_KEY()+":").getBytes(),
                        Base64.NO_WRAP);
                headers.put("Authorization", auth);

                return super.performRequest(request, headers);
            }
        };
        mRequestQueue = Volley.newRequestQueue(getApplicationContext(),stack);
    }
    return mRequestQueue;
}

The RequestQueue object is placed into a Global class and a singleton pattern is applied, so it means that whenever you request the RequestQueue, the authorization header will be in it. Hope it helps !

Community
  • 1
  • 1
Hubert Solecki
  • 2,611
  • 5
  • 31
  • 63