Android WebView not loading URL

前端 未结 10 935
鱼传尺愫
鱼传尺愫 2020-12-05 02:15

I want to load the URL in WebView

I have used the following Code:

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


        
10条回答
  •  陌清茗
    陌清茗 (楼主)
    2020-12-05 02:22

    First, check if you have internet permission in Manifest file.

    
    

    You can then add following code in onCreate() or initialize() method-

    final WebView webview = (WebView) rootView.findViewById(R.id.webview);
            webview.setWebViewClient(new MyWebViewClient());
            webview.getSettings().setBuiltInZoomControls(false);
            webview.getSettings().setSupportZoom(false);
            webview.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);
            webview.getSettings().setAllowFileAccess(true);
            webview.getSettings().setDomStorageEnabled(true);
            webview.loadUrl(URL);
    

    And write a class to handle callbacks of webview -

    public class MyWebViewClient extends WebViewClient {
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                               //your handling...
                    return super.shouldOverrideUrlLoading(view, url);
            }
        }
    

    in same class, you can also use other important callbacks such as -

    - onPageStarted()
    - onPageFinished()
    - onReceivedSslError()
    

    Also, you can add "SwipeRefreshLayout" to enable swipe refresh and refresh the webview.

    
            
        
    

    And refresh the webview when user swipes screen:

    SwipeRefreshLayout mSwipeRefreshLayout = (SwipeRefreshLayout) findViewById(R.id.swipeRefreshLayout);
                mSwipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
                    @Override
                    public void onRefresh() {
                        new Handler().postDelayed(new Runnable() {
                            @Override
                            public void run() {
                                mSwipeRefreshLayout.setRefreshing(false);
                                webview.reload();
                            }
                        }, 3000);
                    }
                });
    

提交回复
热议问题