2015-01-05 25 views
0

我試圖從安全域獲取照片。 爲了能夠得到照片,我必須發送HTTP請求 - 或發送Cookie - 以獲取照片。發送HTTP請求以獲取cardslib中的照片

此代碼不起作用,因爲它發送正常的請求。

CardThumbnail thumb = new CardThumbnail(getActivity()); 
String photoUrl = "https://secure.website/image.png"; 
thumb.setUrlResource(photoUrl); 

我需要的是通過發送所需的cookie來獲取此安全照片。

在改造,我用:

request.addHeader("Cookie", "SESSION=123; CSRF_TOKEN=" + token); 

但什麼是cardslib解決?

回答

0

在這種情況下,內置函數不起作用。

但是你可以使用畢加索獲得it.For例子是這樣的:

MyThumbnail thumbnail = new MyThumbnail(mContext); 
//You need to set true to use an external library 
thumbnail.setExternalUsage(true); 
addCardThumbnail(thumbnail); 


public class MyThumbnail extends CardThumbnail { 

    @Override 
    public void setupInnerViewElements(ViewGroup parent, View viewImage) { 

     //Here you have to set your image with an external library 
     Picasso.Builder builder = new Picasso.Builder(getContext()); 
     builder.downloader(new OkHttpDownloader(ctx) { 
      @Override 
       protected HttpURLConnection openConnection(Uri uri) throws IOException { 
        HttpURLConnection connection = super.openConnection(uri); 

        //Put your header values here 
        connection.setRequestProperty("X-HEADER", "VAL"); 

        return connection; 
      } 
     });   
     Picasso sPicasso = builder.build(); 
     sPicasso.load("https://secure.website/image.png") 
       .into((ImageView)viewImage); 

    } 

} 

如果你想使用UniversalImageLoader庫,你可以在圖書館以類似的方式整合,如所描述在這裏:

https://github.com/gabrielemariotti/cardslib/blob/master/doc/OTHERLIBRARIES.md#using-card-with-android-universal-image-loader

將參數添加到UIL頭,你可以參考上所以這個問題:

Is there a way to specify extra headers while fetching images using Universal Image Loader?

+0

謝謝。但我需要使用UniversalImageLoader而不是畢加索。你有解決UIL的問題嗎? –

+0

@ArabAgile你可以用同樣的方法整合UIL。我剛剛編輯了答案。 Hovewer我不太清楚這個庫是否可以爲http請求添加標題。我在SO中報告了該功能的鏈接。 –