[英]Specific webview does not load in layout in Android
我在一個應用程序中有幾個 webviews,其中一個根本沒有加載,但是當在該特定 webview 中更改為一個簡單的 url 時,例如“ https://www.google.com/ ”,它可以正確加載。 我試圖加載的網址是“ https://mpi.mashie.eu/public/menu/v%C3%A4ster%C3%A5s+stad+skola/a4ec46b2?country=se ”,這是一份午餐菜單,在應用程序內的 webview 中,如包含的代碼和屏幕截圖所示。
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_lunch);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setDisplayShowHomeEnabled(true);
toolbar.setNavigationOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
finish();
}
});
lunch_view = (WebView)findViewById(R.id.webLunch);
lunch_view.getSettings().setJavaScriptEnabled(true);
lunch_view.setWebViewClient(new WebViewClient());
lunch_view.loadUrl("https://mpi.mashie.eu/public/menu/v%C3%A4ster%C3%A5s+stad+skola/a4ec46b2?country=se");
}
帶有 Webview 的 Android Studio 布局文件
我在這里測試了類似問題的答案: Android webview not loading url
您正在嘗試加載 SSL 安全網站(由 https:// 表示)並且您沒有在 webviewclient 中處理 ssl-error 事件。 您需要在 webviewclient 中重載 onReceivedSslError。 要通過 Google Play 商店認證,您需要在繼續處理 url 中的 SSL 證書錯誤之前創建對話框,並讓用戶決定繼續/取消 ssl 錯誤。
此示例代碼來自 StackOverflow 中的另一篇文章,該文章是針對類似問題發布的。
private class MyWebViewClient extends WebViewClient {
@Override
public void onReceivedSslError(WebView view, final SslErrorHandler handler, SslError error) {
AlertDialog.Builder builder = new AlertDialog.Builder(getSupportActionBar().getThemedContext());
AlertDialog alertDialog = builder.create();
String message = "SSL Certificate error. ";
switch (error.getPrimaryError()) {
case SslError.SSL_UNTRUSTED:
message += "The certificate authority is not trusted.";
break;
case SslError.SSL_EXPIRED:
message += "The certificate has expired.";
break;
case SslError.SSL_IDMISMATCH:
message += "The certificate Hostname mismatch.";
break;
case SslError.SSL_NOTYETVALID:
message += "The certificate is not yet valid.";
break;
}
Log.d(TAG, message);
message += " Do you want to continue anyway?";
alertDialog.setTitle("SSL Certificate Error");
alertDialog.setMessage(message);
alertDialog.setButton(DialogInterface.BUTTON_POSITIVE, "OK", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// Ignore SSL certificate errors
handler.proceed();
}
});
alertDialog.setButton(DialogInterface.BUTTON_NEGATIVE, "Cancel", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
handler.cancel();
}
});
alertDialog.show();
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.