Android WebView not loading URL

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

    Add Permission Internet permission in manifest.

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

    This code it working

      public class WebActivity extends Activity {
      WebView wv;
    
     String url="http://www.teluguoneradio.com/rssHostDescr.php?hostId=147";
    
     @Override
     protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_web);
        wv=(WebView)findViewById(R.id.webUrl_WEB);
    
    
    
    WebSettings webSettings = wv.getSettings();
        wv.getSettings().setLoadWithOverviewMode(true);
        wv.getSettings().setUseWideViewPort(true);
        wv.getSettings().setBuiltInZoomControls(true);
        wv.getSettings().setPluginState(PluginState.ON);
    
    
        wv.setWebViewClient(new myWebClient());
    
        wv.loadUrl(url);
    }
    
    
    
    
    public class myWebClient extends WebViewClient {
        @Override
        public void onPageStarted(WebView view, String url, Bitmap favicon) {
            // TODO Auto-generated method stub
            super.onPageStarted(view, url, favicon);
        }
    
        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            // TODO Auto-generated method stub
    
            view.loadUrl(url);
            return true;
    
        }
    }
    
    0 讨论(0)
  • 2020-12-05 02:22

    maybe SSL

        @Override
        public void onReceivedSslError(WebView view, SslErrorHandler handler, SslError error) {
            // ignore ssl error
            if (handler != null){
                handler.proceed();
            } else {
                super.onReceivedSslError(view, null, error);
            }
        }
    
    0 讨论(0)
  • 2020-12-05 02:22

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

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

    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.

    <android.support.v4.widget.SwipeRefreshLayout
            android:id="@+id/swipeRefreshLayout"
            android:layout_width="match_parent"
            android:layout_height="match_parent">
            <WebView
                android:id="@+id/webview"
                android:layout_width="match_parent"
                android:layout_height="match_parent" />
        </android.support.v4.widget.SwipeRefreshLayout>
    

    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);
                    }
                });
    
    0 讨论(0)
  • 2020-12-05 02:29

    The simplest solution is to go to your XML layout containing your webview. Change your android:layout_width and android:layout_height from "wrap_content" to "match_parent".

      <WebView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:id="@+id/webView"/>
    
    0 讨论(0)
  • 2020-12-05 02:31

    Note : Make sure internet permission is given.

    In android 9.0,

    Webview or Imageloader can not load url or image because android 9 have network security issue which need to be enable by manifest file for all sub domain. so either you can add security config file.

    1. Add @xml/network_security_config into your resources:

    <network-security-config>
        <domain-config cleartextTrafficPermitted="true">
        <domain includeSubdomains="true">www.google.com</domain>
        </domain-config>
    </network-security-config>

    1. Add this security config to your Manifest like this:

    <application
       
        android:networkSecurityConfig="@xml/network_security_config"
        ...>
    </application>

    if you want to allow all sub domain

    <application
       android:usesCleartextTraffic="true"
        ...>
    </application>

    Note: To solve the problem, don't use both of point 2 (android:networkSecurityConfig="@xml/network_security_config" and android:usesCleartextTraffic="true") choose one of them

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

    Just as an alternative solution:

    For me webView.getSettings().setUserAgentString("Android WebView") did the trick.

    I already had implemented INTERNET permission and WebViewClient as well as WebChromeClient

    0 讨论(0)
提交回复
热议问题