尝试从Android webview获取用户会话userid,但使用下面的代码我的应用程序将强制停止工作,在构建apk时没有任何错误。请我仍然在尝试完成我的第一个webview应用程序,我仍然是android的新手
public String getCookie(String siteName,String CookieName){
String CookieValue = null;
CookieManager cookieManager = CookieManager.getInstance();
String cookies = cookieManager.getCookie(siteName);
String[] temp=cookies.split(";");
for (String ar1 : temp ){
if(ar1.contains(CookieName)){
String[] temp1=ar1.split("=");
CookieValue = temp1[1];
break;
}
}
return CookieValue;
}
参考上述代码https://stackoverflow.com/a/20241926/8778864
public void onPageFinished(WebView view, String url) {
String cookies = CookieManager.getInstance().getCookie(url);
showAlert(FoodShopMainActivity.this, "Cookies", "cookies string:" + cookies);
}
上面的代码将输出PHPSESSID=g675654hghghf7ghgyty
,但只有会话用户ID。
public void onPageFinished(WebView view, String url) {
/*String userCookie = getCookie("http://www.example.com","online_users");
showAlert(FoodShopMainActivity.this, "Cookies", "cookies string:" + userCookie);*/
}
以上代码将show app has stopped working
一旦推出