在登陆界面获取验证码的时候:
new Thread(){
@Override
public void run() {
try {
SharedPreferences spf = getSharedPreferences("Cookie", Context.MODE_PRIVATE);
HttpClient client = new DefaultHttpClient();
HttpGet get = new HttpGet(Gloable.DOLOAD+"code.gif");
HttpResponse response = client.execute(get);
Cookie cookie = ((AbstractHttpClient) client).getCookieStore().getCookies().get(0);
String sessionId = cookie.getValue();
SharedPreferences.Editor editor = spf.edit();
editor.putString("sessionId", sessionId);
String cookieString = cookie.getName()+"="+cookie.getValue()+
";domain="+cookie.getDomain();
Log.e("test", "cookieString:"+cookieString);
editor.putString("cookieString", cookieString);
editor.commit();
Log.i("info", "b--JSESSIONID=" + sessionId);
if (response.getStatusLine().getStatusCode() == 200) {
byte[] bytes = EntityUtils.toByteArray(response.getEntity());
final Bitmap bitmap = BitmapFactory.decodeByteArray(bytes, 0, bytes.length);
runOnUiThread(new Runnable() {
public void run() {
Drawable drawable = new BitmapDrawable(bitmap);
iv_showCode.setBackgroundDrawable(drawable);
}
});
}
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}.start();
在webview加载url之前:
SharedPreferences spf = getSharedPreferences("Cookie", Context.MODE_PRIVATE);
CookieSyncManager.createInstance(this);
CookieManager cookieManager = CookieManager.getInstance();
String cookieString = spf.getString("cookieString", "");
cookieManager.setCookie(url, cookieString);
CookieSyncManager.getInstance().sync();
webview.loadUrl(url);