gpt4 book ai didi

javascript - Webview localStorage.setItem(javascript) 无法在加载 url 之前设置 (android)

转载 作者:行者123 更新时间:2023-12-05 00:01:18 25 4
gpt4 key购买 nike

在加载我的 URL 之前,我需要设置 localStorage.setItem

但是,当我第一次加载 URL 时,什么也没有出现。如果我第二次刷新页面,它就会出现。如何在第一次加载页面时设置 localStorage.setItem

webView.getSettings().setDomStorageEnabled(true);
webView.getSettings().setJavaScriptEnabled(true);

//set token and role
webView.setWebViewClient(new WebViewClient() {

@Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
webView.loadUrl(
"javascript:" +
"localStorage.setItem('token', '" + token + "');");
}

});


webView.setWebChromeClient(new WebChromeClient() {
private ProgressDialog mProgress;

@Override
public void onProgressChanged(WebView view, int progress) {
try {
if (mProgress == null) {
mProgress = new ProgressDialog(getActivity());
if (!getActivity().isFinishing()) {
mProgress.show();
}
}

mProgress.setMessage(getString(R.string.loading) + String.valueOf(progress) + "%");
if (progress == 100) {
mProgress.dismiss();
mProgress = null;
}

} catch (Exception e) {
Log.e("item menu", e.toString());
}
}
});



webView.loadUrl(url);

最佳答案

如您所说,本地存储数据不是第一次出现......我也遇到过同样的问题......这是解决方案。将 localstorage.setItem(key, val) 放在 onPageStarted() 和 onPageFinished() 方法中。

公共(public)类 AppWebViewClients 扩展 WebViewClient {私有(private) ProgressBar progressBar;

    public AppWebViewClients(ProgressBar progressBar) {
this.progressBar=progressBar;
progressBar.setVisibility(View.VISIBLE);
}
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
// TODO Auto-generated method stub
view.loadUrl(url);
return true;
}

@Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
super.onPageStarted(view, url, favicon);

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
webview.evaluateJavascript("window.localStorage.setItem('"+ key1 +"','"+ val1 +"');", null);
webview.evaluateJavascript("window.localStorage.setItem('"+ key2 +"','"+ val2 +"');", null);

} else {
webview.loadUrl("javascript:localStorage.setItem('"+ key1 +"','"+ val1 +"');");
webview.loadUrl("javascript:localStorage.setItem('"+ key2 +"','"+ val2 +"');");

}

}

}

@Override
public void onPageFinished(WebView view, String url) {
// TODO Auto-generated method stub
super.onPageFinished(view, url);
progressBar.setVisibility(View.GONE);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
webview.evaluateJavascript("window.localStorage.setItem('"+ key1 +"','"+ val1 +"');", null);
webview.evaluateJavascript("window.localStorage.setItem('"+ key2 +"','"+ val2 +"');", null);

} else {
webview.loadUrl("javascript:localStorage.setItem('"+ key1 +"','"+ val1 +"');");
webview.loadUrl("javascript:localStorage.setItem('"+ key2 +"','"+ val2 +"');");

}

}
}
}

希望它能解决您的问题。谢谢你

关于javascript - Webview localStorage.setItem(javascript) 无法在加载 url 之前设置 (android),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39104936/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com