我引用了有关 Stellar 的文档.
然后我开始运行 send payment和 receiving payment creating an account 之后的代码在 java 。
发送付款代码有效,但接收付款代码被终止。我在下面提到了代码:
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());
}
}
});
}
我不明白为什么它会被终止。如果我从我的帐户 URL 检查余额,但显示发送接收结果,但它没有在 Eclipse 中显示结果。
我也引用了下面的引用链接并按照答案进行操作,但仍然无法正常工作。
任何人都可以告诉我如何运行此代码以持续接收付款并在控制台上维护日志。 ?
最佳答案
问题是这是一个流式服务,所以如果您只是在 main 方法中运行该服务,那么它显然会在 main 方法中运行时终止,范围将超出范围并且 EventListener 将无法执行。正如您所说,您正在使用 eclips,您可以做的一件事是不要运行尝试调试并在此 Server server = new Server("https://horizon-testnet.stellar.org");
line 然后按 F6 一行一行。调试时一旦到达程序的最后一行然后等待,不要运行。您将在控制台中看到数据。这样你就会明白程序是如何工作的。
如果您想快速运行它,请使用我在现有代码中添加的代码。我添加了两个选项。你可以使用其中的任何一个。这将显示输出。
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();
}
}
}
输出如下所示:
10.0000000 lumens from GC2BKLYOOYPDEFJKLKY6FNNRQMGFLVHJKQRGNSSRRGSMPGF32LHCQVGF many line like this
关于java - Stellar 支付代码在 Java 中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48859382/