2013-01-22 120 views

回答

1

CookieManager是你在找什么!

CookieSyncManager.createInstance(context) 

创建经理

CookieSyncManager.getInstance().startSync() 
在Activity.onResume

(),并在Activity.onPause调用

CookieSyncManager.getInstance().stopSync() 

()。

为了获得即时的同步,而不是等待定时器触发,主机可拨打

CookieSyncManager.getInstance().sync() 

注意,甚至同步()异步发生,所以不这样做,就像你的活动被关闭。

继承人,你会如何使用它:

// use cookies to remember a logged in status 
CookieSyncManager.createInstance(this); 
CookieSyncManager.getInstance().startSync(); 
WebView webview = new WebView(this); 
webview.getSettings().setJavaScriptEnabled(true); 
setContentView(webview);  
webview.loadUrl([MY URL]); 

Referenced from this question

编辑: 如果你想用HttpClient的做到这一点,你需要创建一个HttpContext的。

// Create a local instance of cookie store 
CookieStore cookieStore = new BasicCookieStore(); 

// Create local HTTP context 
HttpContext localContext = new BasicHttpContext(); 
// Bind custom cookie store to the local context 
localContext.setAttribute(ClientContext.COOKIE_STORE, cookieStore); 

HttpGet httpget = new HttpGet("http://www.google.com/"); 

System.out.println("executing request " + httpget.getURI()); 

// Pass local context as a parameter 
HttpResponse response = httpclient.execute(httpget, localContext); 

Referenced from this question

+0

嗯,我可以将它们添加在DefaultHttpClient? –

+0

我编辑了一个包含使用httppost或httpget客户端的方式 – Jameo

+0

我想你不理解我。我想在我的http客户端中使用我的webview的cookie。对不起,误解 –