发送 HTTP 请求以在 cardslib 中获取照片
Sending HTTP Request to get photo in 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);
但是cardslib中的解决方案是什么?
在这种情况下,内置函数不起作用。
但是你可以使用 Picasso 来获得 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 库,可以按类似方式集成该库,如下所述:
在UIL中给header添加参数,可以参考SO上的这个问题:
Is there a way to specify extra headers while fetching images using Universal Image Loader?
我正在尝试从安全域获取照片。 为了能够获取照片,我必须发送 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);
但是cardslib中的解决方案是什么?
在这种情况下,内置函数不起作用。
但是你可以使用 Picasso 来获得 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 库,可以按类似方式集成该库,如下所述:
在UIL中给header添加参数,可以参考SO上的这个问题:
Is there a way to specify extra headers while fetching images using Universal Image Loader?