简体   繁体   English

如何在 android 中将 javascript localstorage.setItem 注入 webview?

[英]how to inject javascript localstorage.setItem to webview in android?

I have implemented webview in android and webview url also loaded succesddfully.我已经在 android 和 webview Z572D4E421E5E6B9BC11D815Ees8ADD0 中实现了 webview。

Now I need to inject javascript to webview.现在我需要将 javascript 注入 webview。 I need to set localstorage.setItem('key','value').我需要设置 localstorage.setItem('key','value')。 I found many suggestions but none of those are working.我找到了很多建议,但没有一个有效。

here is code how i set webview and injecting javascript这是我如何设置 webview 和注入 javascript 的代码

webviewContactUs.getSettings().setJavaScriptEnabled(true);
    webviewContactUs.getSettings().setDomStorageEnabled(true);
    webviewContactUs.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);
    webviewContactUs.getSettings().setLoadWithOverviewMode(true);
    webviewContactUs.getSettings().setSupportMultipleWindows(true);
    webviewContactUs.getSettings().setAllowFileAccess(true);
    webviewContactUs.getSettings().setAllowUniversalAccessFromFileURLs(true);
    webviewContactUs.addJavascriptInterface(javascriptInterfaceTest, "javascriptInterfaceTest");

    webviewContactUs.setWebViewClient(new MyWebViewClient());

    webviewContactUs.loadUrl(url);

    private class MyWebViewClient extends WebViewClient {
    @Override
    public void onPageStarted(WebView view, String url, Bitmap favicon) {
        super.onPageStarted(view, url, favicon);
        callFunction1(view);
    }

    @Override
    public boolean shouldOverrideUrlLoading(WebView view, String url) {
        return true;
    }

    @Override
    public void onPageFinished(WebView view, String url) {
        super.onPageFinished(view, url);
        callFunction1(view);

    }

}

private  void callFunction1(WebView view) {
    //webviewContactUs.addJavascriptInterface(javascriptInterfaceTest, "javascriptInterfaceTest");
    webviewContactUs.evaluateJavascript(javascriptInterfaceTest.getData(), new ValueCallback<String>() {
        @Override
        public void onReceiveValue(String value) {
            System.out.println("==== callFunction1 onReceiveValue 1 : " + value);
        }
    });
}

public class JavascriptInterfaceTest {
private Context mContext;


public JavascriptInterfaceTest(Context context) {
    mContext = context;
    System.out.println("===== JavascriptInterfaceTest");
}

@JavascriptInterface
public String getData() {
    String key = "\'key_abc\'";
    String val = "\'{\"token\":\"3b46a55ae773c48ad27e43ac7a649737e6ed70477f7562b102054253e5706318\"}\'";


    /*String key = "key_abc";
    String val = "{token:3b46a55ae773c48ad27e43ac7a649737e6ed70477f7562b102054253e5706318}";*/

    //String returnData = "window.sessionStorage.setItem(" + key + "," + val + ");";
    //String returnData = "window.localStorage.setItem(" + key + "," + val + ")";
    String returnData = "localStorage.setItem(" + key + "," + val + ")";
    //String returnData = "sessionStorage.setItem(" + key + "," + val + ");";

   
    System.out.println("==== returnData : " + returnData);
    return returnData;
 }


}

If this code will work then in url load with user data but some how this is not working because currently when url loaded opens login screen.如果此代码可以正常工作,那么在 url 中加载用户数据,但是由于当前加载 url 时打开登录屏幕,所以这不起作用。 So if any solution works then in webview, will not load log in screen.因此,如果任何解决方案有效,那么在 webview 中,将不会加载登录屏幕。

One more thing is there is no any problem from web side because in IOS its working, here is ios implementation另一件事是 web 方面没有任何问题,因为在 IOS 它的工作,这里是 ios 实现

在此处输入图像描述

As in image ios developer implemented.如图像 ios 开发人员实施。 And in ios its working, same thing i want to be done in webview.在 ios 中它的工作,我想在 webview 中做同样的事情。

Any help would be appriciated.任何帮助都将不胜感激。 Thanks in advance.提前致谢。

 _webView.setWebViewClient(new WebViewClient() {
            public void onPageFinished(WebView view, String url) {
                if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.KITKAT) {
                    _webView.evaluateJavascript("localStorage.setItem('testkey','testvalue');", null);
                } else {
                    _webView.loadUrl("javascript:localStorage.setItem('testkey','testvalue');");
                }
            }
        });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM