【问题标题】:Android webView.loadUrl with quotesAndroid webView.loadUrl 带引号
【发布时间】:2015-12-26 07:40:25
【问题描述】:

使用下面的 java 和 javascript 代码;我在这条线上遇到错误document.getElementById('txtCardSwipePOS').value = "[SWIPED_CARD]";。如果我将 [SWIPED_CARD] 周围的双引号更改为单引号,它就可以工作。

但是,如果我的字符串中有单引号,它就不起作用。理想情况下,我想在 "[SWIPED_CARD]" 周围加上双引号,这样 json 编码才能正常工作;但似乎通过带有双引号的 url 加载 javascript 不起作用。有没有办法解决这个问题?

    String creditCardSwipe = new String (cardData);

    creditCardSwipe = StoreWebActivity.jsonString(creditCardSwipe);
    String javascriptCode = "";
    try {
        javascriptCode = StoreWebActivity.convertStreamToString(getResources().openRawResource(R.raw.credit_card_swipe));
    } catch (NotFoundException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (Exception e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    int API = android.os.Build.VERSION.SDK_INT;
    javascriptCode = javascriptCode.replace("[SWIPED_CARD]", creditCardSwipe);

    //old way of injecting javascript
    if (API < 19)
    {
        webView.loadUrl("javascript:"+javascriptCode);
    }
    else //Android 19 or above
    {
        webView.evaluateJavascript(javascriptCode,null);
    }

    //Sometimes swipes can be detected as valid, but in reality aren't so request swipe again
    myUniMagReader.startSwipeCard();
    isWaitingForSwipe = true;

R.raw.credit_card_swipe(上面加载了 JS)

if (document.getElementById('txtCardSwipePOS'))
{
    document.getElementById('txtCardSwipePOS').value = "[SWIPED_CARD]";
}

if (document.getElementById('divProgressOverlay'))
{
    document.getElementById('divProgressOverlay').style.display = '';
}

if (document.getElementById('imgProgress'))
{
    ProgressImg = document.getElementById('imgProgress');
    setTimeout('ProgressImg.src = ProgressImg.src', 0);
}

if (document.getElementById('frmCheckout'))
{
    //Need to use setTimeout for form submission so loading indicators have time to display (this is for Android)
    setTimeout(function()
    {
        document.getElementById('frmCheckout').submit();
    },0);
}

【问题讨论】:

  • str.replace("\"", """) 和 str.replace("'", "'") 你需要的所有文本怎么样在 Java 端进行消毒?(第一个是双精度,第二个是单精度)
  • 问题是如果任何 javascriptCode 中有双引号;它会导致问题。有没有解决的办法?我认为这是因为 js 代码是用 javascript 启动的:XX 另外你给我的代码我相信是 javascript。

标签: javascript java android json


【解决方案1】:

问题是在转换为 json 时它会自动添加双引号。所以从 js 文件中删除引号修复了它。

【讨论】:

    猜你喜欢
    • 2013-10-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-09-21
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多