带引号的Android webView.loadUrl

时间:2015-09-28 23:54:42

标签: javascript java android json

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

但是如果我在我的字符串中有单引号它就不起作用。理想情况下,我想在"[SWIPED_CARD]"周围加上双引号,以便json编码正常工作;但似乎通过带双引号的网址加载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 Loaded Above)

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);
}

1 个答案:

答案 0 :(得分:0)

问题在于,当转换为json时,它会自动添加双引号。因此,从js文件中删除引号会修复它。