将var从html检索到android

时间:2017-07-17 15:21:04

标签: javascript android html web-services

我有一点问题,我使用服务器生成令牌,但是当我尝试在android中检索它时,我无法成功。请帮忙。 这是我在Java Spring中添加它的方法:

@RequestMapping(value = "/checkouts", method = RequestMethod.GET)
    public String checkout(Model model) {
        String clientToken = gateway.clientToken().generate();
        model.addAttribute("clientToken", clientToken);

        return "checkouts/new";
    }

这是我收到的html代码:

<!DOCTYPE HTML>

<html>
<head>
    <title>Braintree Checkout</title>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
    <link rel="stylesheet" type="text/css" href="/css/app.css" />
    <link rel="icon" type="image/png" href="/images/favicon.png" />
    <script src="/javascript/vendor/jquery-2.1.4.min.js"></script>
    <script src="/javascript/vendor/jquery.lettering-0.6.1.min.js"></script>
    <script src="/javascript/demo.js"></script>
</head>
<body>
  <header class="main">
    <div class="container wide">
      <div class="content slim">
        <div class="set">
          <div class="fill">
            <a class="pseudoshop" href="/">PSEUDO<strong>SHOP</strong></a>
          </div>

          <div class="fit">
            <a class="braintree" href="https://developers.braintreepayments.com/guides/drop-in" target="_blank">Braintree</a>
          </div>
        </div>
      </div>
    </div>
    <div class="notice-wrapper">

    </div>
  </header>

  <div class="wrapper">
    <div class="checkout container">

      <header>
        <h1>Hi, <br />Let's test a transaction</h1>
        <p>
          Make a test payment with Braintree using PayPal or a card
        </p>
      </header>

      <form id="payment-form" method="post" action="/checkouts">
        <section>
          <label for="amount">
            <span class="input-label">Amount</span>
            <div class="input-wrapper amount-wrapper">
              <input id="amount" name="amount" type="tel" min="1" placeholder="Amount" value="10" />
            </div>
          </label>

          <div class="bt-drop-in-wrapper">
            <div id="bt-dropin"></div>
          </div>
        </section>

        <input type="hidden" id="nonce" name="payment_method_nonce" />
        <button class="button" type="submit"><span>Test Transaction</span></button>
      </form>
    </div>
  </div>

  <script src="https://js.braintreegateway.com/web/dropin/1.2.0/js/dropin.min.js"></script>
  <script>
    /*<![CDATA[*/
    var form = document.querySelector('#payment-form');
    var client_token = 'xxx';

    braintree.dropin.create({
      authorization: client_token,
      container: '#bt-dropin',
      paypal: {
        flow: 'vault'
      }
    }, function (createErr, instance) {
      form.addEventListener('submit', function (event) {
      event.preventDefault();

      instance.requestPaymentMethod(function (err, payload) {
        if (err) {
          console.log('Error', err);
          return;
        }

        // Add the nonce to the form and submit
        document.querySelector('#nonce').value = payload.nonce;
          form.submit();
        });
      });
    });

    var checkout = new Demo({
      formID: 'payment-form'
    });
    /*]]>*/
  </script>
</body>
</html>

这是我的Android代码,试图检索它:

public void onSuccess(int statusCode, Header[] headers, String clientToken) {
                Document document = Jsoup.parse(clientToken);
                Elements elements = document.getAllElements();
                for(Element e: elements){
                    Elements str = e.getElementsByTag("script");
                    for(Element el: str){
                        mClientToken = el.attr("clientToken");
                    }
                }
                onBraintreeSubmit(constraintLayout);
            }

当我这样做时,我拥有所有好的脚本标签,但当它到达好脚本标签的好部分时:

<script>
    /*<![CDATA[*/
    var form = document.querySelector('#payment-form');
    var client_token = 'xxx';

我的mClientToken仍然为空。有什么想法吗?

0 个答案:

没有答案