我试图为毕加索连接设置Cookie.我在OkHttp找到了这个:
OkHttpClient client = new OkHttpClient(); CookieManager cookieManager = new CookieManager(); cookieManager.setCookiePolicy(CookiePolicy.ACCEPT_ALL); client.setCookieHandler(cookieManager);
问题是我不知道在哪里为毕加索设置这个.接受所有想法!谢谢
你会想要使用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();
覆盖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()
直接使用.