Android WebView not loading URL

前端 未结 10 906
鱼传尺愫
鱼传尺愫 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:32

    In my Case, Adding the below functions to WebViewClient fixed the error. the functions are:onReceivedSslError and Depricated and new api versions of shouldOverrideUrlLoading

            webView.setWebViewClient(new WebViewClient() {
    
            @SuppressWarnings("deprecation")
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                view.loadUrl(url);
                Log.i(TAG, "loading: deprecation");
                return  true;
                //return super.shouldOverrideUrlLoading(view, url);
            }
    
            @Override
            @TargetApi(Build.VERSION_CODES.N)
            public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) {
                view.loadUrl(request.getUrl().toString());
                Log.i(TAG, "loading: build.VERSION_CODES.N");
                return true;
                //return super.shouldOverrideUrlLoading(view, request);
            }
    
            @Override
            public void onPageStarted(
                    WebView view, String url, Bitmap favicon) {
                Log.i(TAG, "page started:"+url);
                super.onPageStarted(view, url, favicon);
            }
    
            @Override
            public void onPageFinished(WebView view, final String url) {
    
                Log.i(TAG, "page finished:"+url);
    
            }
    
            @Override
            public void onReceivedSslError(WebView view, SslErrorHandler handler, SslError er) {
                handler.proceed();
            }
    
        });
    
    0 讨论(0)
  • 2020-12-05 02:36

    Did you added the internet permission in your manifest file ? if not add the following line.

     <uses-permission android:name="android.permission.INTERNET"/> 
    

    hope this will help you.

    EDIT

    Use the below lines.

    
        public class WebViewDemo extends Activity {
    
    
            private WebView webView;
    
    
            Activity activity ;
            private ProgressDialog progDailog; 
    
            @SuppressLint("NewApi")
            @Override
            public void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);
                setContentView(R.layout.main);
    
                activity = this;
    
                progDailog = ProgressDialog.show(activity, "Loading","Please wait...", true);
                progDailog.setCancelable(false);
    
    
    
               webView = (WebView) findViewById(R.id.webview_compontent);
    
    
    
               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 true;                
                    }
                    @Override
                    public void onPageFinished(WebView view, final String url) {
                        progDailog.dismiss();
                    }
                });
    
                webView.loadUrl("http://www.teluguoneradio.com/rssHostDescr.php?hostId=147");
    
               }
        }
    
    0 讨论(0)
  • 2020-12-05 02:40

    Use the following things on your webview

    webview.setWebChromeClient(new WebChromeClient());
    

    then implement the required methods for WebChromeClient class.

    0 讨论(0)
  • 2020-12-05 02:40

    Use this it should help.

     var currentUrl = "google.com" 
     var partOfUrl = currentUrl.substring(0, currentUrl.length-2)
    
     webView.setWebViewClient(object: WebViewClient() {
    
     override fun onLoadResource(WebView view, String url) {
     //call loadUrl() method  here 
     // also check if url contains partOfUrl, if not load it differently.
     if(url.contains(partOfUrl, true)) {
         //it should work if you reach inside this if scope.
     } else if(!(currentUrl.startWith("w", true))) {
         webView.loadurl("www.$currentUrl")
    
     } else if(!(currentUrl.startWith("h", true))) {
         webView.loadurl("https://$currentUrl")
    
     } else { 
       //...
     }
     }
    
     override fun onReceivedSslError(view: WebView?, handler: SslErrorHandler?, error: SslError?) {
      // you can call again loadUrl from here too if there is any error.
    }
     // You should also override other override method for error such as
     // onReceiveError to see how all these methods are called one after another and how
    // they behave while debugging with break point. 
    } 
    
    0 讨论(0)
提交回复
热议问题