Stellar支付代码在Java中不起作用
我已参考了有关的文件 然后我开始在Java中运行和之后的代码 发送付款代码正在工作,但接收付款代码已终止。我在下面提到了代码:Stellar支付代码在Java中不起作用,java,paypal,Java,Paypal,我已参考了有关的文件 然后我开始在Java中运行和之后的代码 发送付款代码正在工作,但接收付款代码已终止。我在下面提到了代码: public class receivePayment { public static void main(String args[]) { Server server = new Server("https://horizon-testnet.stellar.org"); KeyPair account = KeyPair.fromAccountI
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());
}
}
});
}
公共类收到付款{
公共静态void main(字符串参数[]){
服务器=新服务器(“https://horizon-testnet.stellar.org");
KeyPair account=KeyPair.fromAccountId(“gc2bklyoypdefjklky6fnnrqmgflvhjkqrgnssrrgrgsmpgf32lhcqvgf”);
//创建API调用以查询涉及该帐户的付款。
PaymentsRequestBuilderPaymentsRequest=server.payments()。用于帐户(account);
//如果已经处理了一些付款,则从
//
//最后一次看到的付款。(见下文“handlePayment”中的付款地点。)
//已保存。)
/*
*字符串lastToken=LoadLastPaginToken();如果(lastToken!=null){
*paymentsRequest.cursor(lastToken);}
*/
//'stream'将逐个发送记录的付款,然后保留
//连接打开,并在新付款发生时继续向您发送。
paymentsRequest.stream(新的EventListener(){
@凌驾
公共无效OneEvent(运营响应付款){
//记录分页令牌,以便下次从这里开始。
//savepagintoken(payment.getpagintoken());
//付款流包括已发送和已接收的付款。
//我们只是
//希望在此处处理收到的付款。
if(付款操作响应的付款实例){
if(((PaymentOperationResponse)payment.getTo()等于(account)){
返回;
}
字符串金额=((PaymentOperationResponse)付款).getAmount();
资产=((PaymentOperationResponse)付款).getAsset();
字符串资产名;
if(asset.equals(新的AssetTypeNative())){
assetName=“流明”;
}否则{
StringBuilder assetNameBuilder=新StringBuilder();
append(((AssetTypeCreditAlphaNum)asset.getCode());
assetNameBuilder.append(“:”);
assetNameBuilder.append(((AssetTypeCreditAlphaNum)asset.getIssuer().getAccountId());
assetName=assetNameBuilder.toString();
}
StringBuilder输出=新的StringBuilder();
输出。追加(金额);
输出。追加(“”);
output.append(assetName);
输出。追加(“从”);
append(((PaymentOperationResponse)payment.getFrom().getAccountId());
System.out.println(output.toString());
}
}
});
}
}
我不明白为什么它会被终止。如果我检查了帐户URL中的余额,但显示了发送接收结果,但在Eclipse中没有显示结果
我也参考了下面的参考链接,并遵循答案,但它仍然不起作用
谁能告诉我如何运行这段代码,持续接收付款并在控制台上维护日志 问题是,这是一个流式服务,因此如果您只是在main方法中运行服务,那么在main方法中运行服务时,它显然会终止,并且范围将超出范围,EventListener将无法执行。正如您所说,您正在使用eclips,您可以做的一件事是不运行try debug,而是在
Server Server=new Server(“https://horizon-testnet.stellar.org");代码>行,按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();
}
}
}
提供到目前为止您遇到的代码,并说明您遇到的问题。您可以从下面的链接查看代码:我在eclipse中复制了相同的代码,但实现了他们没有给出的两个方法loadLastPagingToken()和savePagingToken()。所以我在代码中对它们进行了注释。根据文档,此代码必须持续运行,以监听我们在代码中给出的帐户id接收的付款。但它会在“paymentsRequest.stream(neweventlistener()”中终止在这个方法中,它不会进入这个方法内部。我已经用我试图在eclipse中运行的代码更新了这个问题。请再次检查这个问题。是的,谢谢,现在接收付款代码在你建议的两个选项中都起作用。非常感谢你的回答和你提供的代码。
10.0000000 lumens from GC2BKLYOOYPDEFJKLKY6FNNRQMGFLVHJKQRGNSSRRGSMPGF32LHCQVGF many line like this