Webview 不加载 URL ,但浏览器加载 [英] Webview doesn't load the URL , but browser does
问题描述
因此,从我的第一个屏幕开始,我将 URL 传递给要在 webview 中启动的活动.但是当 webview 启动时,它显示网页不可用 - URL 处的网页可能暂时关闭,或者它可能已永久移动到新的网址
"
So from my first screen I am passing a URL to an activity to launch in webview. But when webview is launched , it shows "web page not available - The web page at URL might be temporarily down or it may have moved permanently to a new web address
"
但是当我在 android 浏览器中启动相同的 URL 时,它工作正常.这是我在 webview 中启动该 URL 的代码
But when i launch the same URL in android browser, it works fine. Here is my code for launching that URL in webview
super.onCreate(savedInstanceState);
String url = "";
url = getIntent().getStringExtra("loginURL");
WebView urlWebView = new WebView(this);
urlWebView.setWebViewClient(new WebViewClient());
urlWebView.getSettings().setJavaScriptEnabled(true);
urlWebView.loadUrl(url);
this.setContentView(urlWebView);
我做错了什么?
推荐答案
我发现了这个问题.问题是我使用的 URL 具有 https://
并且 URL 的 SSL 证书是自签名的.Android 上的 Web 视图是否支持 SSL? 的解决方案帮我解决了这个问题.
I found the issue. The issue was that the URL I was using has https://
and SSL certificate for the URL was self-signed. The solution from Does the Web View on Android support SSL? helped me fixed the issue.
我在代码中添加了以下部分
I added below part in my code
import android.net.http.*; //added this import statement
urlWebView.setWebViewClient(new WebViewClient(){
@Override
public void onReceivedSslError(WebView view, SslErrorHandler handler, SslError error){
handler.proceed();
}
});
希望这能帮助其他用户.
Hope this will help other users.
这篇关于Webview 不加载 URL ,但浏览器加载的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!