当前位置: 动力学知识库 > 问答 > 编程问答 >

android - Webview renders blank page despite sending cookie

问题描述:

I've been battling with this issue for over a week now and I still don't know what to do.

I have this app where I need to read pdf files from an intranet.

I do manage to store the cookies (using sharedpreference) but when I want to read a (pdf) file all I receive in return is a blank page.

I came across a lot of answers here but so far none of them seem to work for me. I tested my work in Marshmallow and Lollipop devices but so far the result is the same: I have a blank page when I want to open a file. Here is a glimpse of my code (the webview is loaded in an activity...):

private WebView webView;

@Override

protected void onCreate(Bundle savedInstanceState)

{

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_project_web_view);

Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);

setSupportActionBar(toolbar);

getSupportActionBar().setDisplayHomeAsUpEnabled(true);

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

WebSettings webSettings = webView.getSettings();

webSettings.setJavaScriptEnabled(true);

CookieManager cookieManager = CookieManager.getInstance();

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP)

{

cookieManager.removeAllCookies(null);

cookieManager.setAcceptThirdPartyCookies(webView, true);

}

else

{

cookieManager.removeAllCookie();

cookieManager.setAcceptCookie(true);

}

cookieManager.setCookie(my_base_url, my_cookie);

Log.d("WEBVIEW", cookieManager.getCookie(my_base_url));

startWebView(my_base_url);

}

private void startWebView(String url)

{

webView.setWebViewClient(new WebViewClient()

{

ProgressDialog progressDialog;

@Override

public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request)

{

return false;

}

public void onLoadResource(WebView view, String url)

{

if (progressDialog == null)

{

progressDialog = new ProgressDialog(this);

progressDialog.setMessage("Loading...");

progressDialog.show();

}

}

public void onPageFinished(WebView view, String url)

{

Log.d("THE END", "Page Finished loading");

progressDialog.dismiss();

}

});

webView.clearCache(true);

webView.clearHistory();

webView.loadUrl(url);

}

分享给朋友:
您可能感兴趣的文章:
随机阅读: