在Codename One中通过WebBrowserComponent调用JS函数的正确方式
问题
开发内嵌本地网页的Codename One应用,网页由本地HTML/JS文件组成,通过WebBrowserComponent(简称bc)加载。原本用bc.postMessage调用网页JS函数,但出现消息队列未释放的问题,转而尝试bc.execute方法,却因用法不明确导致代码报错,NetBeans提示“no suitable method for execute”。
需求是通过Java代码调用网页中的setgpsdata(dat)函数,将GPS位置数据传入并填充到HTML表格中。错误的Java代码如下:
public void locationUpdated(Location loc) { locationReady=true; try { loc = Im.getCurrentLocation(); float Direction = loc.getDirection(); float acc = loc.getAccuracy(); double latp = loc.getLatitude(); double lonp = loc.getLongitude(); double speed = loc.getVelocity(); double speedo = 3.6 * speed; double roundspd = MathUtil.round(speedo *10); roundspd = roundspd/10; String result = cde+ String.valueOf( roundspd) + ","+ String.valueOf(Direction) + ","+ String.valueOf(acc) + ","+ String.valueOf(latp) + "," +String.valueOf(lonp); // bc.postMessage(result.toString(), "*"); String js = "setgpsdata( ${0} })"; bc.execute("setgpsdata( ${0} }), result); } catch (IOException e) { Dialog.show("GPS error4", "Your location could not be found, please try going outside for a better GPS signal", "Ok", null); } }
网页端对应的JS函数代码:
function setgpsdata(dat) { var dat = []; dat = data.split(","); var speed = dat[0]; var heading = dat[1]; var acc = dat[2]; var latwp = dat[3]; var lonwp = dat[4]; var myTable2 = document.getElementById("GPSdata"); myTable2.rows[0].cells[1].innerHTML = speed + ' [km/h]'; myTable2.rows[1].cells[1].innerHTML = heading; myTable2.rows[2].cells[1].innerHTML = acc + ' [m]'; myTable2.rows[3].cells[1].innerHTML = latTostring(latwp); myTable2.rows[4].cells[1].innerHTML = lonTostring(lonwp); }
解决方法
1. 修正execute方法的调用语法
Codename One的WebBrowserComponent.execute方法需要传入完整可执行的JavaScript语句字符串。由于setgpsdata接收的是字符串参数,需要将Java中的result变量用引号包裹,作为JS字符串传入函数。
错误代码中的字符串拼接存在语法错误,正确写法有两种:
方式一:直接拼接字符串
// 把result用单引号包裹,避免JS语法错误 String jsCommand = "setgpsdata('" + result + "');"; bc.execute(jsCommand);
注意:如果result中包含单引号,需要转义,这里假设GPS数据不含特殊字符,若有特殊字符需额外处理。
方式二:使用MessageFormat格式化(更清晰)
import java.text.MessageFormat; // ... String jsTemplate = "setgpsdata('{0}');"; String jsCommand = MessageFormat.format(jsTemplate, result); bc.execute(jsCommand);
2. 修复JavaScript函数的错误
JS函数中存在变量名错误:参数名为dat,但代码中用了data.split(","),会导致data未定义的错误,需修正为:
function setgpsdata(dat) { // 去掉重复声明的var dat = []; 直接使用参数 var datArray = dat.split(","); var speed = datArray[0]; var heading = datArray[1]; var acc = datArray[2]; var latwp = datArray[3]; var lonwp = datArray[4]; var myTable2 = document.getElementById("GPSdata"); myTable2.rows[0].cells[1].innerHTML = speed + ' [km/h]'; myTable2.rows[1].cells[1].innerHTML = heading; myTable2.rows[2].cells[1].innerHTML = acc + ' [m]'; myTable2.rows[3].cells[1].innerHTML = latTostring(latwp); myTable2.rows[4].cells[1].innerHTML = lonTostring(lonwp); }
(注:也可以直接用dat.split(","),但重新命名变量更清晰,避免和参数名混淆)
3. 完整修正后的Java代码片段
public void locationUpdated(Location loc) { locationReady = true; try { loc = Im.getCurrentLocation(); float Direction = loc.getDirection(); float acc = loc.getAccuracy(); double latp = loc.getLatitude(); double lonp = loc.getLongitude(); double speed = loc.getVelocity(); double speedo = 3.6 * speed; double roundspd = MathUtil.round(speedo * 10); roundspd = roundspd / 10; String result = cde + String.valueOf(roundspd) + "," + String.valueOf(Direction) + "," + String.valueOf(acc) + "," + String.valueOf(latp) + "," + String.valueOf(lonp); // 正确调用execute方法 String jsCommand = "setgpsdata('" + result + "');"; bc.execute(jsCommand); } catch (IOException e) { Dialog.show("GPS error4", "Your location could not be found, please try going outside for a better GPS signal", "Ok", null); } }
内容的提问来源于stack exchange,提问作者user3025401
相关产品推荐
相关产品推荐

