发送HTTP请求以在cardslib中获取照片

时间:2015-01-05 11:10:22

标签: android cardslib

我正在尝试从安全域获取照片。 为了能够获取照片,我将发送HTTP请求 - 或发送Cookie - 以获取照片。

此代码无效,因为它发送正常请求。

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

我需要的是通过发送所需的cookie来获取此安全照片。

在改造中,我曾经使用过:

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

但是cardlib中的解决方案是什么?

1 个答案:

答案 0 :(得分:0)

在这种情况下,内置功能无法正常工作。

但是你可以使用 Picasso 来获取它。例如:

 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中向标题添加参数,可以在SO:

上引用此问题

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