Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android WebView not loading URL

I want to load the URL in WebView

I have used the following Code:

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

But when I execute it, I'm not able to load the url. I am getting web page not available.

Could anyone help?

like image 761
String Avatar asked Apr 25 '13 05:04

String


People also ask

Why is my WebView not working?

You might often face issues in updating the chrome and Android System Webview. To fix this problem, you can reboot your device, check your internet connection, stop auto-updating all apps, clear Google Playstore cache, and storage, leave the beta testing program, and manually update Android WebView app from Playstore.

What is the use of WebView in android?

Android WebView is a system component for the Android operating system (OS) that allows Android apps to display content from the web directly inside an application.


4 Answers

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

           }
    }
like image 104
itsrajesh4uguys Avatar answered Oct 19 '22 11:10

itsrajesh4uguys


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;

    }
}
like image 30
Rank Avatar answered Oct 19 '22 12:10

Rank


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);
        }
    }
like image 22
wanpen Avatar answered Oct 19 '22 12:10

wanpen


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

like image 17
Yogendra Avatar answered Oct 19 '22 11:10

Yogendra