0

我想加載cookie在android web視圖。以下是我的java代碼。試圖在android webview中加載Cookie的URL

final String link = "http://www.iitjeeacademy.com/mobile/question/CHE/AAH"; 

final String domain = "iitjeeacademy.com"; 

String authToken = LoginService.getCookie(); 

String cookie = "auth-token=" + authToken + "; csrf-token=mobile"; 

webView = (WebView) findViewById(R.id.webView); 

Map<String, String> cookieMap = new HashMap<String, String>(); 

cookieMap.put("Cookie", cookie); 


android.webkit.CookieSyncManager.createInstance(QuestionActivity.this); 
android.webkit.CookieManager.getInstance().setAcceptCookie(true); 

WebkitCookieManagerProxy coreCookieManager = new WebkitCookieManagerProxy(null, java.net.CookiePolicy.ACCEPT_ALL); 

java.net.CookieHandler.setDefault(coreCookieManager); 

android.webkit.CookieSyncManager.getInstance().sync(); 

final WebSettings settings = webView.getSettings(); 
settings.setLoadsImagesAutomatically(true); 
settings.setJavaScriptEnabled(true); 
settings.setDomStorageEnabled(true); 

webView.setWebViewClient(new CustomBrowser()); 
webView.setScrollBarStyle(WebView.SCROLLBARS_INSIDE_OVERLAY); 
webView.loadUrl(link, cookieMap); 

private class CustomBrowser extends WebViewClient { 
    @Override 
    public boolean shouldOverrideUrlLoading(WebView view, String url){ 
     view.loadUrl(url); 
     return true; 
    } 
} 

的WebView layout.xml

<WebView 
    android:id="@+id/webView" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent"/> 

我試圖實現自定義的cookie存儲WebkitCookieManagerProxy如圖 - >Pass cookies from HttpURLConnection (java.net.CookieManager) to WebView (android.webkit.CookieManager)

但它不工作。當我運行這段代碼,在網上查看我收到重定向到網站的索引頁...

回答

1
WebView webview = (WebView) this.findViewById(R.id.webView); 
    final WebSettings settings = webview.getSettings(); 
    settings.setJavaScriptEnabled(true); 
    settings.setAppCacheEnabled(true); 
    settings.setBuiltInZoomControls(true); 
    settings.setPluginState(WebSettings.PluginState.ON); 

    webview.setWebChromeClient(new WebChromeClient()); 

    CookieSyncManager.createInstance(YourClass.this); 
    CookieManager cookieManager = CookieManager.getInstance(); 
    cookieManager.removeSessionCookie(); 
    cookieManager.setCookie("your domain name", cookie); 
    CookieSyncManager.getInstance().sync(); 

    Map<String, String> header = new HashMap<String, String>(); 
    header.put("Cookie", cookie); 
    if(isConnected()) { 
     webview.loadUrl(url, header); 
    } 
    else { 
     webview.setVisibility(View.GONE); 
     textView.setText("Your custom error message."); 
     textView.setVisibility(View.VISIBLE); 
    } 
+0

爲什麼'textView.setText( 「您的自定義錯誤消息。」);'?它來自哪裏? – Omkar

+0

其實在我的代碼中,我首先檢查用戶是否連接到互聯網或沒有,如果不是webview顯示網址給用戶,在某些情況下可能有風險,這完全取決於你如何使用此代碼,主webview cookie代碼以loadUrl()方法結束 –

+0

不可以。這是行不通的...仍然重定向到主頁:( – Omkar

0

**這可以幫助**

WebView webview = (WebView) this.findViewById(R.id.wv_file); 
    final WebSettings settings = webview.getSettings(); 
    settings.setJavaScriptEnabled(true); 
    settings.setAppCacheEnabled(true); 
    settings.setBuiltInZoomControls(true); 
    settings.setPluginState(WebSettings.PluginState.ON); 

    webview.setWebChromeClient(new WebChromeClient()); 

    CookieSyncManager.createInstance(ActivityName.this); 
    CookieManager cookieManager = CookieManager.getInstance(); 
    cookieManager.removeSessionCookie(); 
    String cookieString = "param=value"; 
    cookieManager.setCookie(domain_of_url("domain name"), cookieString); 
    CookieSyncManager.getInstance().sync(); 

    Map<String, String> abc = new HashMap<String, String>(); 
    abc.put("Cookie", cookieString); 
    webview.loadUrl("domain name", 
      abc); 

你可能會得到解決

也去throught該鏈接以獲得正確的想法Android WebView Cookie Problem

,或者您也可以做這些qestion Using session cookies with android

Using cookies with Android