我试图为毕加索连接设置Cookie。我在OkHttp找到了这个:
OkHttpClient client = new OkHttpClient();
CookieManager cookieManager = new CookieManager();
cookieManager.setCookiePolicy(CookiePolicy.ACCEPT_ALL);
client.setCookieHandler(cookieManager);
问题是我不知道在哪里为毕加索设定这个。接受所有想法!谢谢
答案 0 :(得分:7)
您希望使用OkHttpDownloader将两者结合在一起:
OkHttpClient client = new OkHttpClient();
CookieManager cookieManager = new CookieManager();
cookieManager.setCookiePolicy(CookiePolicy.ACCEPT_ALL);
client.setCookieHandler(cookieManager);
// Create the downloader for Picasso to use
OkHttpDownloader downloader = new OkHttpDownloader(client);
Picasso picasso = new Picasso.Builder(context).downloader(downloader).build();
答案 1 :(得分:5)
从UrlConnectionDownloader覆盖openConnection-Method对我有用。
import android.content.Context;
import android.net.Uri;
import com.squareup.picasso.UrlConnectionDownloader;
import java.io.IOException;
import java.net.HttpURLConnection;
public class CookieImageDownloader extends UrlConnectionDownloader{
public CookieImageDownloader(Context context) {
super(context);
}
@Override
protected HttpURLConnection openConnection(Uri path) throws IOException{
HttpURLConnection conn = super.openConnection(path);
String cookieName = /*your cookie-name */;
String cookieValue = /*your cookie-value */;
conn.setRequestProperty("Cookie",cookieName + "=" + cookieValue );
return conn;
}
}
将其应用于毕加索:
Picasso picasso = new Picasso.Builder(context).downloader(new CookieImageDownloader(context)).build();
请注意以后不要使用picasso.with()
,因为它会再次初始化修补程序,删除我们的自定义下载程序CookieImageDownloader
,而是直接使用picasso.load()
。