2

對於listView,在其適配器的getView方法中,我需要爲每個ListView項目請求一個圖像,以便通過NetworkImageView逐條加載圖片。問題是我需要將驗證頭添加到請求以允許用戶從服務器獲取圖片。我已經閱讀了一些不可能付諸實踐的解決方案。將身份驗證標頭添加到ImageLoader for NetworkImageView圖片加載

在此先感謝...

+1

對不起,我不明白'哪些不可能付諸實踐。此外,發佈你的代碼和你的logcat的更多信息 – BNK

回答

2

我已經找到了如何設置一個基本認證頭到ImageLoader的。我誤解了該鏈接的答案same topic。因此,信用轉到真正的回答者。 反正的訣竅是一個HurlStack添加到getRequestQueue方法如下:

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; 
} 

請求隊列對象被放置到全局類,並施加一個單模式,因此這意味着無論何時請求RequestQueue,授權標題將在其中。希望能幫助到你 !

相關問題