javascript-将数组从js传递到android
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了javascript-将数组从js传递到android,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含3239字,纯文字阅读大概需要5分钟。
内容图文
嗨,我有这个活动代码:
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
}
我可以将votitotali数组存储在我的android应用中吗?因为我需要从网站上获取一些信息,并且必须将它们打印在应用程序的textView中,但是我真的不知道如何使用webview来做到这一点…
解决方法:
其实很简单.您需要注入一个具有接收数组方法的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全部内容,希望文章能够帮你解决javascript-将数组从js传递到android所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。