私のApplication
クラスでは、次のことを行います。
public class MyApplication extends Application {
private static HttpURLConnection conn = null;
public static CookieStore cookieStore;
public static HttpContext localContext;
public static DefaultHttpClient client = new DefaultHttpClient();
@Override
public void onCreate() {
// TODO Auto-generated method stub
super.onCreate();
CookieSyncManager.createInstance(this);
cookieStore = new BasicCookieStore();
localContext = new BasicHttpContext();
localContext.setAttribute(ClientContext.COOKIE_STORE, cookieStore);
}
...
}
そして、私はRunnable
部分的につながりを持っています:
HttpURLConnection conn = null;
URL url;
try {
url = new URL(requestUrl);
conn = (HttpURLConnection) url.openConnection();
if (Integer.parseInt(Build.VERSION.SDK) < Build.VERSION_CODES.FROYO) {
System.setProperty("http.keepAlive", "false");
}
conn.setConnectTimeout(8000);
conn.setRequestMethod(method);
conn.setInstanceFollowRedirects(false);
conn.setRequestProperty("Content-Type","application/x-www-form-urlendcoded");
MyApplication app = (MyApplication) mContext.getApplicationContext();
*******************************************
if(app.cookieStore.getCookies()!=null){
conn.setRequestProperty("Cookie", app.cookieStore.getCookies().toString());
Log.d("tag", "cookie get " + cookie.getCookie(mContext.getString(R.string.host_url)));
}
********** this part not work **********
Map m = conn.getHeaderFields();
if (m.containsKey("Set-Cookie")) {
String cookies = "";
Collection c = (Collection) m.get("Set-Cookie");
for (Iterator i = c.iterator(); i.hasNext();) {
cookies += (String) i.next() + ",";
}
cookie.setCookie(mContext.getString(R.string.host_url), cookies);
Log.d("tag", "cookie set " + cookies);
}
} catch(...) { ... }
クッキーをCookiee
使っcookieStore
て設定しましたが、何も得られず、戻りますnull
。
どのように使用しCookieStore
ますか?
いくつかの例と答えはこれを与えます:
List<Cookie> cookies = app.client.getCookieStore().getCookies();
それはまた私のために戻りますnull
。