&quot“ esturejavascript()返回“ null”作为WebView的LocalStorage值

发布于 2025-02-13 02:06:32 字数 2000 浏览 1 评论 0原文

(对不起,我的英语差)

我有一个网页,该网页设置了一个本地存储值,例如localstorage.setitem('name','myname'),我将此网页加载到WebView中,我想要要访问Android应用程序中的此本地存储值,因此我使用了以下代码,但它返回“ null”,它需要半天,但我无法修复它,我不是Android开发人员,所以请再向我解释一下。

package com.example.myWebViewApp

import android.os.Bundle
import android.os.Handler
import android.util.Log
import android.webkit.ValueCallback
import android.webkit.WebView
import android.webkit.WebViewClient
import androidx.appcompat.app.AppCompatActivity
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout


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

        val myWebView: WebView = findViewById(R.id.webview)

        val webSettings = myWebView.settings
        webSettings.javaScriptEnabled = true
        webSettings.javaScriptCanOpenWindowsAutomatically = true
        webSettings.allowFileAccess = true
        webSettings.allowContentAccess = true
        webSettings.domStorageEnabled = true
        webSettings.textZoom = 100
        webSettings.allowFileAccess = true

        myWebView.loadUrl("http://192.168.1.99:3333")

        myWebView.webViewClient = object : WebViewClient() {
            override fun onPageFinished(view: WebView, url: String) {

                myWebView.evaluateJavascript(
                    "window.localStorage.getItem('name');",
                    ValueCallback<String?> {
                            n -> run { Log.d("nameTag", "$n") }
                    }
                )
            }
        }

        val refreshLayout = findViewById<SwipeRefreshLayout>(R.id.swipeRefresher)

        refreshLayout.setOnRefreshListener {
            refreshLayout.isRefreshing = true
            Handler().postDelayed(Runnable {
                refreshLayout.isRefreshing = false
                myWebView.reload()
            }, 2000)
        }
    }
}

(Sorry for my poor English)

I have a web page which set a local storage value like localStorage.setItem('name', 'myName') , I load this web page inside a webView and I want to access to this local storage value inside the android app so I used below code but it returns "null", it takes half a day but i can't fix it, I'm not android developer so explain me a little more please.

package com.example.myWebViewApp

import android.os.Bundle
import android.os.Handler
import android.util.Log
import android.webkit.ValueCallback
import android.webkit.WebView
import android.webkit.WebViewClient
import androidx.appcompat.app.AppCompatActivity
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout


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

        val myWebView: WebView = findViewById(R.id.webview)

        val webSettings = myWebView.settings
        webSettings.javaScriptEnabled = true
        webSettings.javaScriptCanOpenWindowsAutomatically = true
        webSettings.allowFileAccess = true
        webSettings.allowContentAccess = true
        webSettings.domStorageEnabled = true
        webSettings.textZoom = 100
        webSettings.allowFileAccess = true

        myWebView.loadUrl("http://192.168.1.99:3333")

        myWebView.webViewClient = object : WebViewClient() {
            override fun onPageFinished(view: WebView, url: String) {

                myWebView.evaluateJavascript(
                    "window.localStorage.getItem('name');",
                    ValueCallback<String?> {
                            n -> run { Log.d("nameTag", "$n") }
                    }
                )
            }
        }

        val refreshLayout = findViewById<SwipeRefreshLayout>(R.id.swipeRefresher)

        refreshLayout.setOnRefreshListener {
            refreshLayout.isRefreshing = true
            Handler().postDelayed(Runnable {
                refreshLayout.isRefreshing = false
                myWebView.reload()
            }, 2000)
        }
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文