Android混合应用程序&x2B;截击、Cookies和网络视图
关于这个问题,我已经研究了超过15个小时: 我正在构建一个混合应用程序,我使用Volley框架(本机视图)登录, 我一登录就获取响应头,提取Cookie并将其保存到我的sharedprefs中。成功登录后,我有一个本机主屏幕,其中包含多个Web视图的链接, 如何将登录时收到的Cookie传递到Webview? 互联网上90%的答案使用CookieSyncManager,这是不推荐的。 我也试过java.net.CookieManager,但都不管用 这是我的密码Android混合应用程序&x2B;截击、Cookies和网络视图,android,cookies,webview,android-volley,Android,Cookies,Webview,Android Volley,关于这个问题,我已经研究了超过15个小时: 我正在构建一个混合应用程序,我使用Volley框架(本机视图)登录, 我一登录就获取响应头,提取Cookie并将其保存到我的sharedprefs中。成功登录后,我有一个本机主屏幕,其中包含多个Web视图的链接, 如何将登录时收到的Cookie传递到Webview? 互联网上90%的答案使用CookieSyncManager,这是不推荐的。 我也试过java.net.CookieManager,但都不管用 这是我的密码 @Override prot
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mWebView = new WebView(this);
setContentView(mWebView);
cookies = pref.getString(Const.COOKIE_KEY,"null");
userID = pref.getString(Const.USER_ID_KEY,"null");
mUrl = Const.PERFORMANCE_WEBVIEW_LINK + Const.USER_ID;
String cookieText = Const.COOKIE_KEY + "=" + cookies;
//Approach A Environment for the Cookies
//Does not work
cookieSync = CookieSyncManager.createInstance(this);
cookieManager = CookieManager.getInstance();
cookieManager.removeSessionCookie();
cookieManager.setCookie(mUrl, cookieText);
cookieSync.sync();
SystemClock.sleep(10000);
/*APPROACH B, sending the Cookies with header
##Did not Work##
final Map<String, String> headers = new HashMap<>();
Log.d("cookie", cookieText);
headers.put("Cookie",cookieText);
*/
if(cookies.equals("null") || userID.equals("null")) {
Toast.makeText(PerformanceWebview.this, "Error", Toast.LENGTH_SHORT).show();
//Logging Out
Intent intent = new Intent(this, LoginActivity.class);
startActivity(intent);
}
else {
//mWebView.loadUrl(mUrl,headers);
mWebView.loadUrl(mUrl);
Toast.makeText(PerformanceWebview.this,cookieText, Toast.LENGTH_SHORT).show();
Log.d("URL", "URL: " + mUrl);
}
}
@覆盖
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
mWebView=新的WebView(此);
setContentView(mWebView);
cookies=pref.getString(常量COOKIE_键,“null”);
userID=pref.getString(Const.USER_ID_KEY,“null”);
mUrl=Const.PERFORMANCE\u WEBVIEW\u LINK+Const.USER\u ID;
字符串cookieText=Const.COOKIE\u KEY+“=”+cookies;
//接近Cookies的环境
//不起作用
cookieSync=CookieSyncManager.createInstance(此);
cookieManager=cookieManager.getInstance();
cookieManager.removeSessionCookie();
cookieManager.setCookie(mUrl,cookieText);
cookieSync.sync();
系统时钟。睡眠(10000);
/*方法B,发送带有标题的cookie
##不起作用##
最终映射头=新HashMap();
Log.d(“cookie”,cookieText);
headers.put(“Cookie”,cookieText);
*/
if(cookies.equals(“null”)| | userID.equals(“null”)){
Toast.makeText(PerformanceWebview.this,“Error”,Toast.LENGTH_SHORT.show();
//注销
Intent Intent=新Intent(这是LoginActivity.class);
星触觉(意向);
}
否则{
//loadUrl(mUrl,headers);
mWebView.loadUrl(mUrl);
Toast.makeText(PerformanceWebview.this、cookieText、Toast.LENGTH_SHORT).show();
Log.d(“URL”,“URL:+mUrl”);
}
}
我还尝试将WebClient传递给WebView,并重写它的shouldOverrideURl方法,并将标题传递给它。
我做的另一个方法是使用WebSettings并传递ChromeClient
这里的答案似乎都不起作用
class CookieStore_ implements CookieStore{
/** this map may have null keys! */
private final Map<URI, List<HttpCookie>> map = new HashMap<URI, List<HttpCookie>>();
private android.webkit.CookieManager manager;
public CookieStore_() {
manager = android.webkit.CookieManager.getInstance();
}
public synchronized void add(URI uri, HttpCookie cookie) {
if (cookie == null) {
throw new NullPointerException("cookie == null");
}
uri = cookiesUri(uri);
//add cookie to the CookieManager,be sure you have called
//CookieSyncManager.createInstance(context) if your android version
//is lower than Lollipop
manager.setCookie(uri.toString(),cookie.toString());
List<HttpCookie> cookies = map.get(uri);
if (cookies == null) {
cookies = new ArrayList<HttpCookie>();
map.put(uri, cookies);
} else {
cookies.remove(cookie);
}
cookies.add(cookie);
}
private URI cookiesUri(URI uri) {
if (uri == null) {
return null;
}
try {
return new URI("http", uri.getHost(), null, null);
} catch (URISyntaxException e) {
return uri; // probably a URI with no host
}
}
public synchronized List<HttpCookie> get(URI uri) {
if (uri == null) {
throw new NullPointerException("uri == null");
}
List<HttpCookie> result = new ArrayList<HttpCookie>();
// get cookies associated with given URI. If none, returns an empty list
List<HttpCookie> cookiesForUri = map.get(uri);
if (cookiesForUri != null) {
for (Iterator<HttpCookie> i = cookiesForUri.iterator(); i.hasNext(); ) {
HttpCookie cookie = i.next();
if (cookie.hasExpired()) {
i.remove(); // remove expired cookies
} else {
result.add(cookie);
}
}
}
// get all cookies that domain matches the URI
for (Map.Entry<URI, List<HttpCookie>> entry : map.entrySet()) {
if (uri.equals(entry.getKey())) {
continue; // skip the given URI; we've already handled it
}
List<HttpCookie> entryCookies = entry.getValue();
for (Iterator<HttpCookie> i = entryCookies.iterator(); i.hasNext(); ) {
HttpCookie cookie = i.next();
if (!HttpCookie.domainMatches(cookie.getDomain(), uri.getHost())) {
continue;
}
if (cookie.hasExpired()) {
i.remove(); // remove expired cookies
} else if (!result.contains(cookie)) {
result.add(cookie);
}
}
}
return Collections.unmodifiableList(result);
}
public synchronized List<HttpCookie> getCookies() {
List<HttpCookie> result = new ArrayList<HttpCookie>();
for (List<HttpCookie> list : map.values()) {
for (Iterator<HttpCookie> i = list.iterator(); i.hasNext(); ) {
HttpCookie cookie = i.next();
if (cookie.hasExpired()) {
i.remove(); // remove expired cookies
} else if (!result.contains(cookie)) {
result.add(cookie);
}
}
}
return Collections.unmodifiableList(result);
}
public synchronized List<URI> getURIs() {
List<URI> result = new ArrayList<URI>(map.keySet());
result.remove(null); // sigh
return Collections.unmodifiableList(result);
}
public synchronized boolean remove(URI uri, HttpCookie cookie) {
if (cookie == null) {
throw new NullPointerException("cookie == null");
}
List<HttpCookie> cookies = map.get(cookiesUri(uri));
if (cookies != null) {
return cookies.remove(cookie);
} else {
return false;
}
}
public synchronized boolean removeAll() {
boolean result = !map.isEmpty();
map.clear();
return result;
}
public void clearCookies(){
map.clear();
}
}
如果您不需要将cookie保存到本地存储,您可以通过一行代码将批量cookie共享到webview
CookieUtil.getCookieUtil(context).initCookieHandler();
注销时不要忘记调用clearCookies(),首先确保Volley使用cookies:(请参阅) 然后,例如,在Volley中进行登录调用后,将Cookie同步到WebView:
// Sync cookies to webview
android.webkit.CookieManager webkitCookies = android.webkit.CookieManager.getInstance();
for (HttpCookie cookie : App.cookieManager.getCookieStore().getCookies()) {
webkitCookies.setCookie(cookie.getDomain(), cookie.getName() + "=" + cookie.getValue());
}
if (Build.VERSION.SDK_INT >= 21) {
webkitCookies.flush();
} else {
CookieSyncManager.getInstance().sync();
}
找到了一个解决方案:打电话给客户,告诉他们的后端开发人员允许一种避免cookie并接受我的客户请求的方法
// Make volley remember cookies
// Do this only once on app startup, and keep the reference to the cookiemanager.
// I'm saving it on an App class, but you can do something different.
App.cookieManager = new CookieManager();
CookieHandler.setDefault(App.cookieManager);
// Note, we are using the java.net.CookieManager above.
// Sync cookies to webview
android.webkit.CookieManager webkitCookies = android.webkit.CookieManager.getInstance();
for (HttpCookie cookie : App.cookieManager.getCookieStore().getCookies()) {
webkitCookies.setCookie(cookie.getDomain(), cookie.getName() + "=" + cookie.getValue());
}
if (Build.VERSION.SDK_INT >= 21) {
webkitCookies.flush();
} else {
CookieSyncManager.getInstance().sync();
}