问题描述
我已经使用推荐的的HttpURLConnection
开始,远离 DefaultHTTPClient
。其中一个,我一直没能粘到一起的地方是使用持久性cookie存储的。我想简单地附加一个自定义的cookie处理程序/管理我的连接到存储的cookie。因为它包装了关于两行饼干的主题Android的文件一直没有很大的帮助。
I've begun using the recommended HTTPUrlConnection
and moved away from the DefaultHTTPClient
. One of the things that I haven't been able to glue back together is the use of a persistent cookie store. I'd like to simply attach a custom cookie handler/manager to my connection to store the cookies. The Android documentation hasn't been very helpful as it wraps up the subject about cookies in two lines.
我一直在使用循环J的 PersistentCookieStore
早期和工作漂亮。
I've been using LoopJ's PersistentCookieStore
earlier and that worked beautifully.
我如何可以建立一个持久cookie存储在Android中,我可以附加到任何想法,我的HttpURLConnection
的保存和自动检索饼干?
Any idea on how I could set up a persistent cookie store in Android that I can attach to my HTTPUrlConnection
that saves and retrieves cookies automatically?
感谢
推荐答案
其我花了几个小时,但我设法建立一个自定义的cookie存储自己。
Its' taken me a few hours but I managed to build a custom cookie storage myself.
你必须这样做是为了连接这样的:
You have to attach this by doing this:
public class application extends Application {
@Override
public void onCreate() {
super.onCreate();
CookieManager cmrCookieMan = new CookieManager(new MyCookieStore(this.objContext), CookiePolicy.ACCEPT_ALL);
CookieHandler.setDefault(cmrCookieMan);
}
}
下面是实际的存储:
/*
* This is a custom cookie storage for the application. This
* will store all the cookies to the shared preferences so that it persists
* across application restarts.
*/
class MyCookieStore implements CookieStore {
/*
* The memory storage of the cookies
*/
private Map<URI, List<HttpCookie>> mapCookies = new HashMap<URI, List<HttpCookie>>();
/*
* The instance of the shared preferences
*/
private final SharedPreferences spePreferences;
/*
* @see java.net.CookieStore#add(java.net.URI, java.net.HttpCookie)
*/
public void add(URI uri, HttpCookie cookie) {
System.out.println("add");
System.out.println(cookie.toString());
List<HttpCookie> cookies = mapCookies.get(uri);
if (cookies == null) {
cookies = new ArrayList<HttpCookie>();
mapCookies.put(uri, cookies);
}
cookies.add(cookie);
Editor ediWriter = spePreferences.edit();
HashSet<String> setCookies = new HashSet<String>();
setCookies.add(cookie.toString());
ediWriter.putStringSet(uri.toString(), spePreferences.getStringSet(uri.toString(), setCookies));
ediWriter.commit();
}
/*
* Constructor
*
* @param ctxContext the context of the Activity
*/
@SuppressWarnings("unchecked")
public MyCookieStore(Context ctxContext) {
spePreferences = ctxContext.getSharedPreferences("CookiePrefsFile", 0);
Map<String, ?> prefsMap = spePreferences.getAll();
for(Map.Entry<String, ?> entry : prefsMap.entrySet()) {
for (String strCookie : (HashSet<String>) entry.getValue()) {
if (!mapCookies.containsKey(entry.getKey())) {
List<HttpCookie> lstCookies = new ArrayList<HttpCookie>();
lstCookies.addAll(HttpCookie.parse(strCookie));
try {
mapCookies.put(new URI(entry.getKey()), lstCookies);
} catch (URISyntaxException e) {
e.printStackTrace();
}
} else {
List<HttpCookie> lstCookies = mapCookies.get(entry.getKey());
lstCookies.addAll(HttpCookie.parse(strCookie));
try {
mapCookies.put(new URI(entry.getKey()), lstCookies);
} catch (URISyntaxException e) {
e.printStackTrace();
}
}
System.out.println(entry.getKey() + ": " + strCookie);
}
}
}
/*
* @see java.net.CookieStore#get(java.net.URI)
*/
public List<HttpCookie> get(URI uri) {
List<HttpCookie> lstCookies = mapCookies.get(uri);
if (lstCookies == null)
mapCookies.put(uri, new ArrayList<HttpCookie>());
return mapCookies.get(uri);
}
/*
* @see java.net.CookieStore#removeAll()
*/
public boolean removeAll() {
mapCookies.clear();
return true;
}
/*
* @see java.net.CookieStore#getCookies()
*/
public List<HttpCookie> getCookies() {
Collection<List<HttpCookie>> values = mapCookies.values();
List<HttpCookie> result = new ArrayList<HttpCookie>();
for (List<HttpCookie> value : values) {
result.addAll(value);
}
return result;
}
/*
* @see java.net.CookieStore#getURIs()
*/
public List<URI> getURIs() {
Set<URI> keys = mapCookies.keySet();
return new ArrayList<URI>(keys);
}
/*
* @see java.net.CookieStore#remove(java.net.URI, java.net.HttpCookie)
*/
public boolean remove(URI uri, HttpCookie cookie) {
List<HttpCookie> lstCookies = mapCookies.get(uri);
if (lstCookies == null)
return false;
return lstCookies.remove(cookie);
}
}
这篇关于如何使用HttpURLConnection的时候我坚持饼干?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!