javascript - 将数组从js传递到android

标签 javascript android arrays android-webview

您好,我有这个 Activity 代码:

public class WebActivity extends ActionBarActivity {

    TextView number;

    WebView mWebView;
    CountDownTimer mTimer;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_web);


        number = (TextView) findViewById(R.id.number);
        mTimer=new CountDownTimer(10000, 1000) {
            String[] myArray={"javascript:document.getElementById(\"utente\").value=\""+LoginActivity.username+"\"; document.getElementById(\"j_password\").value=\""+LoginActivity.password+"\"; document.querySelector(\"input[type=submit]\").click();","javascript:document.getElementById(\"menu-servizialunno:_idJsp14\").click();"};


            int currentIndex=0;
            public void onTick(long millisUntilFinished) {

                number.setText("seconds remaining: " + millisUntilFinished / 1000 + " " + (currentIndex + 1) + "/" + (myArray.length + 1));
            }
            //code comment start
            // i think this part could be written better
            // but it works!!
            public void onFinish() {
                if (currentIndex<myArray.length) {
                    number.setText("done!");
                    mWebView.loadUrl(myArray[currentIndex]);
                    currentIndex++;
                    mTimer.start();
                } else{
                    mTimer.cancel();

                }

            }
            //code comment end
        };

        mTimer.start();
        mWebView = (WebView) findViewById(R.id.webview);
        mWebView.getSettings().setJavaScriptEnabled(true);
        mWebView.setWebViewClient(new WebSliderWebViewClient() {
            @Override
            public void onPageFinished(WebView view, String url) {
                super.onPageFinished(mWebView, url);
                Toast.makeText(getApplicationContext(), "Done!", Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
                Toast.makeText(getApplicationContext(), "Oh no! " + description, Toast.LENGTH_SHORT).show();

            }
        });
        mWebView.loadUrl("http://www.ss16374.scuolanext.info");



    }

和这个 js 函数:

function voti( showSubject, showData, showVote ){
    var votitotali = []
    document.getElementById("menu-servizialunno:_idJsp14").click();
    setTimeout(function(){
        elems = document.querySelectorAll("td, legend");
        for(var i = 0; i < elems.length; i++){
            curText = elems[i].innerHTML;
            if( elems[i].tagName == "LEGEND" && showSubject ){
                votitotali += [curText]
                //console.log( curText );
            }else if( elems[i].innerHTML.indexOf("Voto") != -1 && showVote ){
                votitotali += [curText.replace(/.*\(([0-9\.]+)\)/,"$1")]
                //console.log( curText.replace(/.*\(([0-9\.]+)\)/,"$1") );
            }else if( /\d{2}\/\d{2}\/\d{4}/.test(elems[i].innerHTML) && showData ){
                votitotali += [curText.replace(/.*(\d{2}\/\d{2}\/\d{4}).*/,"$1")]
                //console.log( curText.replace(/.*(\d{2}\/\d{2}\/\d{4}).*/,"$1") );
            }
        }
        document.getElementsByClassName("btl-modal-closeButton")[0].click()
    },3000);
    return votitotali
}

我可以在我的 Android 应用程序中存储 votitotali 数组吗?因为我需要从网站获取一些信息,我必须在应用程序的 textView 上打印它们,但我真的不知道如何使用 webview 来做到这一点...

最佳答案

其实很简单。您需要注入(inject)一个 Java 对象,该对象具有接收数组的方法:

class MyReceiver {
    @JavascriptInterface
    public void receive(String[] input) {
        // input is your data!
    }
}

// At the very beginning
mWebView.addJavascriptInterface(new MyReceiver(), "receiver");
// ...

然后,如果您在 JavaScript 代码中这样调用它:

receiver.receive(voti( ... ));

您将在 MyReceiver.receive 中获取数组。

请注意,非字符串数组元素(例如数字)不会转换为字符串,而是会被替换为 null

关于javascript - 将数组从js传递到android,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31257132/

相关文章:

javascript - Angular5 Instascan - 打开相机不工作 <视频>

javascript - node.js 不导入 css 和 js 文件

javascript - 如何使用选择标签隐藏/显示选择标签?

javascript - 如何使用 :not() and :contains()? 反转 <li> 选择

android - 触摸监听器不工作 android

arrays - 在给定矩阵的每一行中找到最后一个非零元素的索引?

android - 如何在 CameraX API 中切换相机?

android - 如何为EditText设置边框颜色

JavaScript 转置数据

ruby-on-rails - Ruby:将数组传递给 starts_with?