下面列出了android.webkit.CookieManager#flush ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
@Nullable
@Override
@SuppressWarnings("deprecation")
@SuppressLint("SetJavaScriptEnabled")
public View onCreateView2(LayoutInflater inflater,
@Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
Context context = getContext2();
AssertUtils.assertNotNull(context);
EhUtils.signOut(context);
// http://stackoverflow.com/questions/32284642/how-to-handle-an-uncatched-exception
CookieManager cookieManager = CookieManager.getInstance();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
cookieManager.flush();
cookieManager.removeAllCookies(null);
cookieManager.removeSessionCookies(null);
} else {
CookieSyncManager cookieSyncManager = CookieSyncManager.createInstance(context);
cookieSyncManager.startSync();
cookieManager.removeAllCookie();
cookieManager.removeSessionCookie();
cookieSyncManager.stopSync();
}
mWebView = new WebView(context);
mWebView.getSettings().setJavaScriptEnabled(true);
mWebView.setWebViewClient(new LoginWebViewClient());
mWebView.loadUrl(EhUrl.URL_SIGN_IN);
return mWebView;
}
@SuppressLint("SetJavaScriptEnabled")
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// http://stackoverflow.com/questions/32284642/how-to-handle-an-uncatched-exception
CookieManager cookieManager = CookieManager.getInstance();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
cookieManager.flush();
cookieManager.removeAllCookies(null);
cookieManager.removeSessionCookies(null);
} else {
CookieSyncManager cookieSyncManager = CookieSyncManager.createInstance(this);
cookieSyncManager.startSync();
cookieManager.removeAllCookie();
cookieManager.removeSessionCookie();
cookieSyncManager.stopSync();
}
// Copy cookies from okhttp cookie store to CookieManager
url = EhUrl.getMyTagsUrl();
EhCookieStore store = EhApplication.getEhCookieStore(this);
for (Cookie cookie : store.getCookies(HttpUrl.parse(url))) {
cookieManager.setCookie(url, cookie.toString());
}
setContentView(R.layout.activity_my_tags);
setNavigationIcon(R.drawable.v_arrow_left_dark_x24);
webView = findViewById(R.id.webview);
webView.getSettings().setJavaScriptEnabled(true);
webView.setWebViewClient(new MyTagsWebViewClient());
webView.setWebChromeClient(new DialogWebChromeClient(this));
webView.loadUrl(url);
progress = findViewById(R.id.progress);
}
@SuppressLint("SetJavaScriptEnabled")
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// http://stackoverflow.com/questions/32284642/how-to-handle-an-uncatched-exception
CookieManager cookieManager = CookieManager.getInstance();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
cookieManager.flush();
cookieManager.removeAllCookies(null);
cookieManager.removeSessionCookies(null);
} else {
CookieSyncManager cookieSyncManager = CookieSyncManager.createInstance(this);
cookieSyncManager.startSync();
cookieManager.removeAllCookie();
cookieManager.removeSessionCookie();
cookieSyncManager.stopSync();
}
// Copy cookies from okhttp cookie store to CookieManager
url = EhUrl.getUConfigUrl();
EhCookieStore store = EhApplication.getEhCookieStore(this);
for (Cookie cookie : store.getCookies(HttpUrl.parse(url))) {
cookieManager.setCookie(url, cookie.toString());
}
setContentView(R.layout.activity_u_config);
setNavigationIcon(R.drawable.v_arrow_left_dark_x24);
webView = (WebView) findViewById(R.id.webview);
webView.getSettings().setJavaScriptEnabled(true);
webView.setWebViewClient(new UConfigWebViewClient());
webView.setWebChromeClient(new DialogWebChromeClient(this));
webView.loadUrl(url);
progress = (ProgressView) findViewById(R.id.progress);
Snackbar.make(webView, R.string.apply_tip, Snackbar.LENGTH_LONG).show();
}
/**
* 同步cookie
*/
private void syncCookie(String url) {
if (!TextUtils.isEmpty(url) && url.contains("wanandroid")) {
try {
CookieSyncManager.createInstance(this);
CookieManager cookieManager = CookieManager.getInstance();
cookieManager.setAcceptCookie(true);
cookieManager.removeSessionCookie();// 移除
cookieManager.removeAllCookie();
String cookie = SPUtils.getString("cookie", "");
if (!TextUtils.isEmpty(cookie)) {
String[] split = cookie.split(";");
for (int i = 0; i < split.length; i++) {
cookieManager.setCookie(url, split[i]);
}
}
// String cookies = cookieManager.getCookie(url);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
cookieManager.flush();
} else {
CookieSyncManager.getInstance().sync();
}
} catch (Exception e) {
DebugUtil.error("==异常==", e.toString());
}
}
}
@SuppressLint("NewApi")
private void setCookie(String url) {
URI uri = URI.create(url);
if (TextUtils.isEmpty(uri.getHost())) {
return;
}
if (Build.VERSION.SDK_INT < 21) {
CookieSyncManager.createInstance(getContext());
}
CookieManager cookieManager = CookieManager.getInstance();
cookieManager.setAcceptCookie(true);
cookieManager.removeSessionCookie();
// 注入cookies
List<String> cookies = getCookies();
for (String cookie : cookies) {
cookieManager.setCookie(uri.getHost(), cookie);
}
if (Build.VERSION.SDK_INT >= 21) {
cookieManager.flush();
} else {
CookieSyncManager.getInstance().sync();
}
}
@Nullable
@Override
@SuppressWarnings("deprecation")
@SuppressLint("SetJavaScriptEnabled")
public View onCreateView2(LayoutInflater inflater,
@Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
Context context = getContext2();
AssertUtils.assertNotNull(context);
EhUtils.signOut(context);
// http://stackoverflow.com/questions/32284642/how-to-handle-an-uncatched-exception
CookieManager cookieManager = CookieManager.getInstance();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
cookieManager.flush();
cookieManager.removeAllCookies(null);
cookieManager.removeSessionCookies(null);
} else {
CookieSyncManager cookieSyncManager = CookieSyncManager.createInstance(context);
cookieSyncManager.startSync();
cookieManager.removeAllCookie();
cookieManager.removeSessionCookie();
cookieSyncManager.stopSync();
}
mWebView = new WebView(context);
mWebView.getSettings().setJavaScriptEnabled(true);
mWebView.setWebViewClient(new LoginWebViewClient());
mWebView.loadUrl(EhUrl.URL_SIGN_IN);
return mWebView;
}
@SuppressLint("SetJavaScriptEnabled")
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// http://stackoverflow.com/questions/32284642/how-to-handle-an-uncatched-exception
CookieManager cookieManager = CookieManager.getInstance();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
cookieManager.flush();
cookieManager.removeAllCookies(null);
cookieManager.removeSessionCookies(null);
} else {
CookieSyncManager cookieSyncManager = CookieSyncManager.createInstance(this);
cookieSyncManager.startSync();
cookieManager.removeAllCookie();
cookieManager.removeSessionCookie();
cookieSyncManager.stopSync();
}
// Copy cookies from okhttp cookie store to CookieManager
url = EhUrl.getMyTagsUrl();
EhCookieStore store = EhApplication.getEhCookieStore(this);
for (Cookie cookie : store.getCookies(HttpUrl.parse(url))) {
cookieManager.setCookie(url, cookie.toString());
}
setContentView(R.layout.activity_my_tags);
setNavigationIcon(R.drawable.v_arrow_left_dark_x24);
webView = findViewById(R.id.webview);
webView.getSettings().setJavaScriptEnabled(true);
webView.setWebViewClient(new MyTagsWebViewClient());
webView.setWebChromeClient(new DialogWebChromeClient(this));
webView.loadUrl(url);
progress = findViewById(R.id.progress);
}
@SuppressLint("SetJavaScriptEnabled")
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// http://stackoverflow.com/questions/32284642/how-to-handle-an-uncatched-exception
CookieManager cookieManager = CookieManager.getInstance();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
cookieManager.flush();
cookieManager.removeAllCookies(null);
cookieManager.removeSessionCookies(null);
} else {
CookieSyncManager cookieSyncManager = CookieSyncManager.createInstance(this);
cookieSyncManager.startSync();
cookieManager.removeAllCookie();
cookieManager.removeSessionCookie();
cookieSyncManager.stopSync();
}
// Copy cookies from okhttp cookie store to CookieManager
url = EhUrl.getUConfigUrl();
EhCookieStore store = EhApplication.getEhCookieStore(this);
for (Cookie cookie : store.getCookies(HttpUrl.parse(url))) {
cookieManager.setCookie(url, cookie.toString());
}
setContentView(R.layout.activity_u_config);
setNavigationIcon(R.drawable.v_arrow_left_dark_x24);
webView = (WebView) findViewById(R.id.webview);
webView.getSettings().setJavaScriptEnabled(true);
webView.setWebViewClient(new UConfigWebViewClient());
webView.setWebChromeClient(new DialogWebChromeClient(this));
webView.loadUrl(url);
progress = (ProgressView) findViewById(R.id.progress);
Snackbar.make(webView, R.string.apply_tip, Snackbar.LENGTH_LONG).show();
}
public void synCookie() {
//下面的变量都是暂时处理 日后逻辑清晰重新构成
Boolean isHistory = mUrl.contains("main_controller");
CookieSyncManager.createInstance(this);
CookieManager cookieManager = CookieManager.getInstance();
cookieManager.setAcceptCookie(true);
if (isHistory) {
cookieManager.removeAllCookie();
}
if (from != null && from.equals(Constants.BundleValue.UC_LOGIN)) {
cookieManager.removeAllCookie();
}
if (!TextUtils.isEmpty(mUrl) && mUrl.contains(DOMAIN)) {
String qvtStr = CurrentPreference.getInstance().getQvt();
boolean qvt = false;
if (!TextUtils.isEmpty(qvtStr) && !isHistory) {
QVTResponseResult qvtResponseResult = JsonUtils.getGson().fromJson(qvtStr, QVTResponseResult.class);
if (qvtResponseResult.ret) {
cookieManager.setCookie(DOMAIN, "_v=" + qvtResponseResult.data.vcookie + ";");
cookieManager.setCookie(DOMAIN, "_t=" + qvtResponseResult.data.tcookie + ";");
cookieManager.setCookie(DOMAIN, "_q=" + qvtResponseResult.data.qcookie + ";");
cookieManager.setCookie(DOMAIN, "q_d=" + QtalkNavicationService.getInstance().getXmppdomain() + ";");
qvt = true;
}
}
// if (!isHistory) {
// if (!qvt) {
// cookieManager.setCookie(DOMAIN, "_v=;");
// cookieManager.setCookie(DOMAIN, "_t=;");
// cookieManager.setCookie(DOMAIN, "_q=;");
// cookieManager.setCookie(DOMAIN, "q_d=;");
// }
// }
}
if (mUrl.contains("/package/plts/dashboard")) {
cookieManager.setCookie(DOMAIN, "q_u=" + CurrentPreference.getInstance().getUserid() + ";");
cookieManager.setCookie(DOMAIN, "q_nm=" + CurrentPreference.getInstance().getUserid() + ";");
cookieManager.setCookie(DOMAIN, "q_d=" + QtalkNavicationService.getInstance().getXmppdomain() + ";");
}
if(isVideoAudioCall){
cookieManager.setCookie(DOMAIN, "_caller=" + QtalkStringUtils.parseId(videoCaller) + ";");
cookieManager.setCookie(DOMAIN, "_callee=" + QtalkStringUtils.parseId(videoCallee) + ";");
cookieManager.setCookie(DOMAIN, "_calltime=" + System.currentTimeMillis() + ";");
cookieManager.setCookie(DOMAIN, "u=" + CurrentPreference.getInstance().getUserid() + ";");
}else {
cookieManager.setCookie(DOMAIN, "_caller=;");
cookieManager.setCookie(DOMAIN, "_callee=;");
cookieManager.setCookie(DOMAIN, "_calltime=;");
cookieManager.setCookie(DOMAIN, "u=;");
}
//默认种qckey
cookieManager.setCookie(DOMAIN, "q_ckey=" + Protocol.getCKEY() + ";");
if (QtalkSDK.getInstance().isLoginStatus()) {
cookieManager.setCookie(DOMAIN, "_u=" + CurrentPreference.getInstance().getUserid() + ";");
cookieManager.setCookie(DOMAIN, "q_u=" + CurrentPreference.getInstance().getUserid() + ";");
cookieManager.setCookie(DOMAIN, "_k=" + CurrentPreference.getInstance().getVerifyKey() + ";");
cookieManager.setCookie(DOMAIN, "q_d=" + QtalkNavicationService.getInstance().getXmppdomain() + ";");
} else {
cookieManager.setCookie(DOMAIN, "_u=;");
cookieManager.setCookie(DOMAIN, "_k=;");
cookieManager.setCookie(DOMAIN, "q_d=;");
}
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.LOLLIPOP) {
cookieManager.flush();
} else {
CookieSyncManager.getInstance().sync();
}
Logger.i("QunarWebActvity getCookie:"+ cookieManager.getCookie(DOMAIN));
}
/**
* Synchronize the uuid2 cookie to the Webview Cookie Jar
* This is only done if there is no present cookie.
*
* @param headers headers to extract cookies from for syncing
*/
@SuppressWarnings("deprecation")
private void httpCookieSync(Map<String, List<String>> headers) {
if (headers == null || headers.isEmpty()) return;
CookieManager cm = CookieManager.getInstance();
if (cm == null) {
LogUtil.i("PrebidNewAPI", "Unable to find a CookieManager");
return;
}
try {
String existingUUID = getExistingCookie();
for (Map.Entry<String, List<String>> entry : headers.entrySet()) {
String key = entry.getKey();
// Only "Set-cookie" and "Set-cookie2" pair will be parsed
if (key != null && (key.equalsIgnoreCase(PrebidServerSettings.VERSION_ZERO_HEADER)
|| key.equalsIgnoreCase(PrebidServerSettings.VERSION_ONE_HEADER))) {
for (String cookieStr : entry.getValue()) {
if (!TextUtils.isEmpty(cookieStr) && cookieStr.contains(PrebidServerSettings.AN_UUID)) {
// pass uuid2 to WebView Cookie jar if it's empty or outdated
if (existingUUID == null || !cookieStr.contains(existingUUID)) {
cm.setCookie(PrebidServerSettings.COOKIE_DOMAIN, cookieStr);
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {
// CookieSyncManager is deprecated in API 21 Lollipop
CookieSyncManager.createInstance(PrebidMobile.getApplicationContext());
CookieSyncManager csm = CookieSyncManager.getInstance();
if (csm == null) {
LogUtil.i("Unable to find a CookieSyncManager");
return;
}
csm.sync();
} else {
cm.flush();
}
}
}
}
}
}
} catch (IllegalStateException ise) {
} catch (Exception e) {
}
}
/**
* Synchronize the uuid2 cookie to the Webview Cookie Jar
* This is only done if there is no present cookie.
* @param headers headers to extract cookies from for syncing
*/
@SuppressWarnings("deprecation")
public static void cookieSync(Map<String, List<String>> headers) {
if (headers == null || headers.isEmpty()) return;
CookieManager cm = CookieManager.getInstance();
if (cm == null) {
// Logger.i(Logger.LOGTAG, "Unable to find a CookieManager");
return;
}
try {
String existingUUID = getExistingANUUID();
for (Map.Entry<String, List<String>> entry : headers.entrySet()) {
String key = entry.getKey();
// Only "Set-cookie" and "Set-cookie2" pair will be parsed
if (key != null && (key.equalsIgnoreCase(VERSION_ZERO_HEADER)
|| key.equalsIgnoreCase(VERSION_ONE_HEADER))) {
for (String cookieStr : entry.getValue()) {
if (!TextUtils.isEmpty(cookieStr) && cookieStr.contains(Settings.AN_UUID)) {
// pass uuid2 to WebView Cookie jar if it's empty or outdated
if ((existingUUID == null || !cookieStr.contains(existingUUID))) {
cm.setCookie(Settings.getCookieDomain(), cookieStr);
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {
// CookieSyncManager is deprecated in API 21 Lollipop
CookieSyncManager csm = CookieSyncManager.getInstance();
if (csm == null) {
Clog.i(Clog.httpRespLogTag,"Unable to find a CookieSyncManager");
return;
}
csm.sync();
} else {
cm.flush();
}
}
}
}
}
}
} catch (IllegalStateException ise) {
} catch (Exception e) {
}
}