【问题标题】:Kotlin WebView loadUrl() with redirect URL带有重定向 URL 的 Kotlin WebView loadUrl()
【发布时间】:2019-10-22 13:37:34
【问题描述】:

当我尝试使用方法加载 URL 时:

webView.loadUrl("https://example.com"),

webView.webViewClient = object : WebViewClient() {
   override fun shouldOverrideUrlLoading(view: WebView?, url: String?): Boolean {
      view?.loadUrl(url)
      return true
   }
}

结果为空。

我认为这是因为 URL 被重定向到另一个链接,但是如何解决呢?

【问题讨论】:

标签: android kotlin webview


【解决方案1】:

你有<uses-permission android:name="android.permission.INTERNET" />吗?

【讨论】:

  • 绝对是的。
  • 尝试覆盖 onPageFinished(WebView view, String url) 和 onPageStarted(WebView view, String url)
【解决方案2】:

这是示例解决方案。 试试看吧。

1.activity_main.xml

    <?xml version="1.0" encoding="utf-8"?>
    <android.support.constraint.ConstraintLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.kotlinwebview.MainActivity">


<WebView
    android:id="@+id/webview"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:ignore="MissingConstraints"
    tools:layout_editor_absoluteX="8dp"
    tools:layout_editor_absoluteY="8dp" />

2) MainActivity.kt

class MainActivity : AppCompatActivity() {
    var mywebview: WebView? = null
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        mywebview = findViewById<WebView>(R.id.webview)
        mywebview!!.webViewClient = object : WebViewClient() {
            override fun shouldOverrideUrlLoading(view: WebView?, url: String?): Boolean {
                view?.loadUrl(url)
                return true
            }
        }
        mywebview!!.loadUrl("https://www.google.co.in/")
    }
}

【讨论】:

  • 这很奇怪,因为我也测试过这段代码,它对我来说完美无缺。
  • 分享更多信息,例如分享您的清单、xml、kt 文件。
【解决方案3】:

这是完整的答案。 在 MainActivity.kt 中相应地更改您的 URL。 MainAcivity.kt

class MainActivity : AppCompatActivity() {

    @SuppressLint("SetJavaScriptEnabled")
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        setContentView(R.layout.activity_main)

        val mWebView = findViewById<WebView>(R.id.webview)

        val webSettings = mWebView.settings
        webSettings.javaScriptEnabled = true
        mWebView.loadUrl(getString(R.string.website_url))
        mWebView.webViewClient = HelloWebViewClient()
        WebView.setWebContentsDebuggingEnabled(false)

    }

    private inner class HelloWebViewClient : WebViewClient() {

        override fun shouldOverrideUrlLoading(view: WebView, url: String): Boolean {
            if (Uri.parse(url).host == getString(R.string.website_domain)) {
                return false
            }
            val intent = Intent(Intent.ACTION_VIEW, Uri.parse(url))
            startActivity(intent)
            return true
        }

        override fun onPageFinished(view: WebView, url: String) {
            // TODO Auto-generated method stub
            super.onPageFinished(view, url)
        }

    }

    override fun onKeyDown(keyCode: Int, event: KeyEvent): Boolean {
        if (keyCode == KeyEvent.KEYCODE_BACK && webview.canGoBack()) {
            webview.goBack()
            return true
        }
        return super.onKeyDown(keyCode, event)
    }

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".MainActivity">

    <WebView
        android:id="@+id/webview"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" />

</LinearLayout>

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    package="com.example.learnkotlin">

    <uses-permission android:name="android.permission.INTERNET" />
    <uses-feature
        android:name="android.hardware.touchscreen"
        android:required="false" />
    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>

            <intent-filter
                android:autoVerify="true"
                tools:targetApi="m">
                <action android:name="android.intent.action.VIEW" />

                <category android:name="android.intent.category.DEFAULT" />
                <category android:name="android.intent.category.BROWSABLE" />

                <data
                    android:host="quaestio.org"
                    android:scheme="https" />
            </intent-filter>
        </activity>
        <meta-data
            android:name="android.webkit.WebView.EnableSafeBrowsing"
            android:value="true" />
    </application>

</manifest>

字符串.xml

<resources>
    <string name="app_name">LearnKotlin</string>
    <string name="website_domain">quaestio.org</string>
    <string name="website_url">https://quaestio.org</string>
</resources>

我已经对此进行了测试,它绝对可以正常工作。如果您遇到问题,请告诉我。 祝你好运。

【讨论】:

    猜你喜欢
    • 2021-12-02
    • 2016-03-25
    • 2013-08-24
    • 1970-01-01
    • 1970-01-01
    • 2011-06-25
    • 2013-02-05
    • 2018-06-14
    • 1970-01-01
    相关资源
    最近更新 更多