简体   繁体   English

恒星支付代码在Java中不起作用

[英]Stellar Payment code not working in Java

I have referred documentation on Stellar . 我已经参考了Stellar的文档。

Then I have started to run the send payment and receiving payment code after creating an account in Java. 然后,在Java中创建帐户后,我就开始运行发送付款接收付款代码。

Send payment code is working , but receiving payment code got terminated. 发送付款代码有效,但是接收付款代码已终止。 I have mentioned code below : 我在下面提到了代码:

public class receivePayment {

public static void main(String args[]) {

    Server server = new Server("https://horizon-testnet.stellar.org");
    KeyPair account = KeyPair.fromAccountId("GC2BKLYOOYPDEFJKLKY6FNNRQMGFLVHJKQRGNSSRRGSMPGF32LHCQVGF");

    // Create an API call to query payments involving the account.
    PaymentsRequestBuilder paymentsRequest = server.payments().forAccount(account);

    // If some payments have already been handled, start the results from
    // the
    // last seen payment. (See below in `handlePayment` where it gets
    // saved.)
    /*
     * String lastToken = loadLastPagingToken(); if (lastToken != null) {
     * paymentsRequest.cursor(lastToken); }
     */

    // `stream` will send each recorded payment, one by one, then keep the
    // connection open and continue to send you new payments as they occur.
    paymentsRequest.stream(new EventListener<OperationResponse>() {
        @Override
        public void onEvent(OperationResponse payment) {
            // Record the paging token so we can start from here next time.
            // savePagingToken(payment.getPagingToken());

            // The payments stream includes both sent and received payments.
            // We only
            // want to process received payments here.
            if (payment instanceof PaymentOperationResponse) {
                if (((PaymentOperationResponse) payment).getTo().equals(account)) {
                    return;
                }

                String amount = ((PaymentOperationResponse) payment).getAmount();

                Asset asset = ((PaymentOperationResponse) payment).getAsset();
                String assetName;
                if (asset.equals(new AssetTypeNative())) {
                    assetName = "lumens";
                } else {
                    StringBuilder assetNameBuilder = new StringBuilder();
                    assetNameBuilder.append(((AssetTypeCreditAlphaNum) asset).getCode());
                    assetNameBuilder.append(":");
                    assetNameBuilder.append(((AssetTypeCreditAlphaNum) asset).getIssuer().getAccountId());
                    assetName = assetNameBuilder.toString();
                }

                StringBuilder output = new StringBuilder();
                output.append(amount);
                output.append(" ");
                output.append(assetName);
                output.append(" from ");
                output.append(((PaymentOperationResponse) payment).getFrom().getAccountId());
                System.out.println(output.toString());
            }

        }
    });
}

} }

I don't understand why it gets terminated. 我不明白为什么它会终止。 If I checked the balance from my account URL, but shows me the sending-receiving result, but it is not showing result in Eclipse. 如果我从帐户URL检查余额,但显示了收发结果,但是在Eclipse中却没有显示结果。

I have also referred below reference link and follow the answer but still it is not working. 我还参考了以下参考链接并按照答案进行操作,但仍然无法正常工作。

Stellar payments query 恒星支付查询

Can anyone tell me how to run this code which continuously receive the payments and maintain the logs on console. 谁能告诉我如何运行此代码以连续接收付款并在控制台上维护日志。 ?

The thing is this is a streaming service, so if you just run the service in main method then it will obviously terminated while running in main method and the scope will go outside and EventListener will not be able to be executed. 关键是这是一个流服务,因此,如果仅在main方法中运行该服务,则它将在main方法中运行时明显终止,作用域将移至外部,并且EventListener将无法执行。 As you said you are using eclips,one thing you can do is instead of run try debug and insert a debugpoint at this Server server = new Server("https://horizon-testnet.stellar.org"); 正如您所说的,您可以使用Eclipse,而不是运行try debug并在此Server server = new Server("https://horizon-testnet.stellar.org");插入调试点Server server = new Server("https://horizon-testnet.stellar.org"); line and press F6 and go one by one line. 并按F6并逐行一行。 While debugging Once it reaches the last line of the program then wait,don't run. 调试时一旦到达程序的最后一行,请等待,不要运行。 You will see the data in the console. 您将在控制台中看到数据。 this way you will understand how the program is working. 这样,您将了解程序的工作方式。 If you want to run it fast then use the code that I have added with the existing code. 如果要快速运行,请使用我在现有代码中添加的代码。 I have added two options. 我添加了两个选项。 you can use any of this. 您可以使用其中任何一个。 This will display the output. 这将显示输出。

public class TestStellar2 {


      public static void main(String args[]) {

        Server server = new Server("https://horizon-testnet.stellar.org");
        KeyPair account = KeyPair.fromAccountId("GC2BKLYOOYPDEFJKLKY6FNNRQMGFLVHJKQRGNSSRRGSMPGF32LHCQVGF");

        PaymentsRequestBuilder paymentsRequest = server.payments().forAccount(account);


        paymentsRequest.stream(new EventListener <OperationResponse>(){
          @Override
          public void onEvent(OperationResponse payment) {

            if (payment instanceof PaymentOperationResponse) {
              if (((PaymentOperationResponse) payment).getTo().equals(account)) {
                return;
              }

              String amount = ((PaymentOperationResponse) payment).getAmount();

              Asset asset = ((PaymentOperationResponse) payment).getAsset();
              String assetName;
              if (asset.equals(new AssetTypeNative())) {
                assetName = "lumens";
              } else {
                StringBuilder assetNameBuilder = new StringBuilder();
                assetNameBuilder.append(((AssetTypeCreditAlphaNum) asset).getCode());
                assetNameBuilder.append(":");
                assetNameBuilder.append(((AssetTypeCreditAlphaNum) asset).getIssuer().getAccountId());
                assetName = assetNameBuilder.toString();
              }

              StringBuilder output = new StringBuilder();
              output.append(amount);
              output.append(" ");
              output.append(assetName);
              output.append(" from ");
              output.append(((PaymentOperationResponse) payment).getFrom().getAccountId());
              System.out.println(output.toString());
            }
          }
        }); 

        /**
         * option 1
         * 
         */
       /*try {
            System.in.read();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }*/

        /**
         * option 2
         */
        try {
            Thread.currentThread().join();
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

      }

    }

The output will look like below : 输出将如下所示:

10.0000000 lumens from GC2BKLYOOYPDEFJKLKY6FNNRQMGFLVHJKQRGNSSRRGSMPGF32LHCQVGF many line like this

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM