Webview无法加载https URL Xamarin Droid

时间:2019-05-13 07:25:12

标签: xamarin.android

我目前在NewsDetailView中使用WebView Xamarin Android来加载新闻内容,包括:html文本和图片的http / https URL

<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:local="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/LighterGray"
  android:clickable="true">
            <WebView
                android:id="@+id/webview"
                android:layout_marginBottom="@dimen/LayoutMargin"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content" />
</RelativeLayout>

但是WebView无法使用https url加载图片。 我已经设置了<uses-permission android:name="android.permission.INTERNET"并在AndroidManifest.xml中添加了android:hardwareAccelerated="true

这是“视图”中的代码:

public class NewsDetailsView
    {
        private WebView _webView;

        public string WebViewContent
        {
            get { return _webViewContent; }
            set
            {
                _webViewContent = value;
                LoadHtmlString();
            }
        }

        protected override void InitView(View view)
        {
            base.InitView(view);
            _webView = (WebView)view.FindViewById(Resource.Id.webview);
            _webView.Settings.JavaScriptEnabled = false;
            _webView.Settings.CacheMode = CacheModes.NoCache;
            _webView.SetWebViewClient(new WebViewClient());
        }

        private void LoadHtmlString()
        {
            _webView.LoadData(WebViewContent, "text/html; charset=UTF-8", null);
        }

这是一种内容模式:

Table 2:</strong>&nbsp;Time to feed pet MTF</span></p><p style="margin-bottom: 0px; padding: 0px;">&nbsp;</p>
 <p style="margin-bottom: 0px; padding: 0px; text-align: center;"><strong style="margin: 0px; padding: 0px; font-size: 14px;"><img alt="Chỉ số thức ăn cho heo" src="http://nguoichannuoi.com/upload_images/images/kien-thuc-nha-nong/cs-thuc-an1.jpg" style="margin: 10px 0px; padding: 0px; max-width: 100%; height: 386px; width: 600px;"></strong></p>

是否想为WebViewClient设置更多内容?已经进行了大量研究,但是似乎很难找到适用于Xamarin Android的教程,所以我还没有找到正确的解决方案!

需要帮助!

1 个答案:

答案 0 :(得分:0)

您应该覆盖OnReceivedSslError中的WebViewClient方法。(我允许所有SSL证书仅用于测试。您应根据需要更改MixedContentHandling和SSL证书)。

   if (Build.VERSION.SdkInt >= BuildVersionCodes.Lollipop)
   {
        _webView.Settings.MixedContentMode =MixedContentHandling.AlwaysAllow;
   }
        _webView.SetWebViewClient(new MyWebviewClient());

  public class MyWebviewClient:WebViewClient
  {

    public override void OnReceivedSslError(WebView view, SslErrorHandler handler, SslError error)
    {
      //  base.OnReceivedSslError(view, handler, error);
        handler.Proceed();
    }
}