简体   繁体   English

某些 URL 未在 WebView 中加载

[英]Some Url Not Load in WebView

I'm trying to load a URL in an android WebView, which is in a Activity.我正在尝试在 Activity 中的 android WebView 中加载 URL。 I was able to accomplish this, but the website was launching the default browser.我能够做到这一点,但该网站正在启动默认浏览器。 I found the setWebViewClient method and it kept the website within the WebView inside of the app versus launching the default browser.我找到了 setWebViewClient 方法,它将网站保存在应用程序内部的 WebView 中,而不是启动默认浏览器。 However - this was showing a blank page.但是 - 这显示了一个空白页。 I tried other websites ( http://www.google.com , http://www.amazon.com , etc) and these work...however the site that I am trying to get to load ( https://mizaajindia.com/ ) does not.我尝试了其他网站( http://www.google.comhttp://www.amazon.com等)并且这些网站有效......但是我试图加载的网站( https://mizaajindia .com/ ) 没有。

I've set javascript enabled just in case - but cannot understand why some URLs would load and others wont from within the app's WebView.我已经设置了 javascript 以防万一 - 但无法理解为什么某些 URL 会加载而其他 URL 不会从应用程序的 WebView 中加载。 Would you have any ideas?你有什么想法吗?

Thanks so much!非常感谢!

public class MainActivity1 extends AppCompatActivity {公共类 MainActivity1 扩展 AppCompatActivity {

private MainActivity1 activity;
private ProgressDialog progDailog;
private WebView webView;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.activity_main);
    activity = this;

    progDailog = ProgressDialog.show(activity, "Loading", "Please wait...", true);
    progDailog.setCancelable(false);
    webView = (WebView) findViewById(R.id.wb_webview);
    webView.getSettings().setJavaScriptEnabled(true);
    webView.getSettings().setLoadWithOverviewMode(true);
    webView.getSettings().setUseWideViewPort(true);
    webView.setWebViewClient(new WebViewClient() {

        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            progDailog.show();
            view.loadUrl(url);
            return false;
        }

        @Override
        public void onPageFinished(WebView view, final String url) {
            progDailog.dismiss();
        }
    });
    webView.loadUrl("https://mizaajindia.com/");
}

} }

The websites you've mentioned are on http and it internally resolves and redirects to https .您提到的网站在http ,它在内部解析并重定向到https

Look at this solution: Android WebView, how to handle redirects in app instead of opening a browser看看这个解决方案: Android WebView,如何在应用程序中处理重定向而不是打开浏览器

for my specific case, I had to enable dom storage just like @Aaryan Negi says .对于我的具体情况,我必须像@Aaryan Negi 所说的那样启用 dom 存储。 this is because the site uses local storage这是因为该站点使用本地存储

further information can be found in astackoverflow response and MDN's information on Web storage API更多信息可以在stackoverflow响应MDN关于Web存储API的信息中找到

Code sample of a webview started in a fragment. webview 的代码示例在片段中开始。


    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?,
                              savedInstanceState: Bundle?): View? {
        _binding = FragmentGuestWebViewBinding.inflate(inflater, container, false)
        val view = binding.root
        
        //declare webview domStorage
        val myWebView: WebView = binding.webView

        //enable javascript and 
        myWebView.settings.javaScriptEnabled = true
        myWebView.settings.domStorageEnabled = true

        myWebView.loadUrl("https://mixerlogic.herokuapp.com")

        // Force links and redirects to open in the WebView instead of in a browser
        myWebView.webViewClient = WebViewClient()

        return view
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM