WebView does not load with that page

1

I wanted to know if someone can tell me why the webView can not load the next page? I tried with heavier pages and load them without problems ... but that does not even appear

override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        val settings = webView.settings
        settings.javaScriptEnabled = true
        webView.loadUrl("https://www.venze.es/terms-conditions/")
}
    
asked by Diego Cardenas 20.12.2018 в 00:28
source

1 answer

1

First you must have the permission to connect to the internet:

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

Now I do not see that you get the reference of WebView within the layout activity_main.xml .

You must have the WebView and you must obtain the reference.

As an example, assuming that your WebView has the id webview :

<WebView
    android:id="@+id/webview"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintRight_toRightOf="parent"
    app:layout_constraintTop_toTopOf="parent" />

In this way you would load the url within WebView :

override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        //Obtiene referencia de la vista.
         var webView = findViewById<WebView>(R.id.webview)

        val settings = webView.settings
        settings.javaScriptEnabled = true
        webView.loadUrl("https://www.venze.es/terms-conditions/")
}

Kotlin, how to load an unencrypted URL in a WebView.

Now another important detail, the url that you sample is not encrypted:

I imagine they have a balancer, sometimes it shows that it has a certificate and in others it does not.

Therefore you need to create a class that extends from WebViewClient and about writing the method:

override fun onReceivedSslError(view: WebView, handler: SslErrorHandler, error: SslError) {
    handler.proceed() // Ignora errores de certificado SSL
}

Finding an error ignores any SSL certificate error and continues loading.

This would be an example of the code:

class MainActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        //Obtiene referencia de la vista.
        var webView = findViewById<WebView>(R.id.webview)

        val settings = webView.settings
        settings.javaScriptEnabled = true

        //Define WebViewClient
        webView.webViewClient = HelloWebViewClient()
        //Carga url
        webView.loadUrl("https://www.venze.es/terms-conditions/")

    }

}

class HelloWebViewClient : WebViewClient() {
    override fun onReceivedSslError(view: WebView, handler: SslErrorHandler, error: SslError) {
        handler.proceed() // Ignora errores de certificado SSL
    }
}
    
answered by 20.12.2018 / 03:13
source