Android WebView无法加载URL

时间:2013-04-25 05:35:16

标签: android android-layout android-intent android-webview

我想在WebView

中加载URL

我使用了以下代码:

webView = (WebView) findViewById(R.id.webview1);
webView.setWebViewClient(new HostsWebClient());
webView.getSettings().setPluginState(PluginState.ON);
webView.getSettings().setJavaScriptEnabled(true);
webView.getSettings().setJavaScriptCanOpenWindowsAutomatically(false);
webView.getSettings().setPluginsEnabled(true);
webView.getSettings().setSupportMultipleWindows(false);
webView.getSettings().setSupportZoom(false);
webView.setVerticalScrollBarEnabled(false);
webView.setHorizontalScrollBarEnabled(false);

webView.loadUrl(URL);

但是当我执行它时,我无法加载网址。我的网页不可用。

有人可以帮忙吗?

11 个答案:

答案 0 :(得分:82)

您是否在清单文件中添加了互联网权限?如果没有添加以下行。

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

希望这会对你有所帮助。

修改

使用以下行。

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");

       }

答案 1 :(得分:15)

在清单中添加权限Internet权限。

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

此代码正常工作

  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;

    }
}

答案 2 :(得分:10)

也许是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);
        }
    }

答案 3 :(得分:5)

在您的网页浏览中使用以下内容

webview.setWebChromeClient(new WebChromeClient());

然后为WebChromeClient类实现所需的方法。

答案 4 :(得分:3)

注意:请确保已授予互联网许可。

在android 9.0中,

Webview或Imageloader无法加载url或图像,因为android 9存在网络安全问题,需要通过清单文件为所有子域启用此功能。这样您就可以添加安全配置文件。

  1. 将@ xml / network_security_config添加到您的资源中:

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

  1. 将此安全配置添加到清单中,如下所示:

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

如果要允许所有子域

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

答案 5 :(得分:2)

首先,检查您是否在清单文件中拥有互联网权限。

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

然后,您可以在onCreate()或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);

编写一个类来处理webview的回调 -

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

在同一课程中,您还可以使用其他重要的回调,例如 -

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

此外,您可以添加&#34; SwipeRefreshLayout&#34;启用滑动刷新并刷新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>

在用户滑动屏幕时刷新webview:

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);
                }
            });

答案 6 :(得分:1)

作为替代解决方案:

对我来说webView.getSettings().setUserAgentString("Android WebView")做了伎俩。

我已经实施了INTERNET权限和WebViewClient以及WebChromeClient

答案 7 :(得分:1)

使用它应该会有所帮助。

 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. 
} 

答案 8 :(得分:0)

最简单的解决方案是转到包含Web视图的XML布局。将您的android:layout_width和android:layout_height从“ wrap_content”更改为“ match_parent”。

  <WebView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/webView"/>

答案 9 :(得分:0)

就我而言,向WebViewClient添加以下功能可修复该错误。 函数包括:onReceivedSslErrorshouldOverrideUrlLoading的api版本和新的api版本

        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();
        }

    });

答案 10 :(得分:0)

对于某些网页,关键是启用 DOM 存储

webview.getSettings().setDomStorageEnabled(true);