Java Web3j在智能合约中调用变量

Java Web3j在智能合约中调用变量,java,solidity,smartcontracts,geth,web3-java,Java,Solidity,Smartcontracts,Geth,Web3 Java,我正在尝试使用solidity、geth和web3j获取智能合约中变量的值 合同HelloWorld非常简单: pragma solidity ^0.6.10; contract HelloWorld { uint256 public counter = 5; function add() public { //increases counter by 1 counter++; } function subtract() public {

我正在尝试使用solidity、geth和web3j获取智能合约中变量的值

合同HelloWorld非常简单:

pragma solidity ^0.6.10;
   contract HelloWorld {
   uint256 public counter = 5;
  
   function add() public {  //increases counter by 1
       counter++;
   }
 
   function subtract() public { //decreases counter by 1
       counter--;
   }
   
   function getCounter() public view returns (uint256) {
       return counter;
   }
}
web3j没有call()函数,只有send()函数,这让人惊讶

当我尝试按照web3j说明获取计数器时:

contract.getCounter().send()
我得到的是交易凭证,而不是价值uint256

有人能帮忙吗

多谢各位


您需要修改生成的HelloWorld.java文件中的getCounter()函数

public RemoteCall<Type> getCounter() {
    final Function function = new Function(
            FUNC_GETCOUNTER, 
            Arrays.<Type>asList(), 
            Arrays.<TypeReference<?>>asList(new TypeReference<Uint>() {}));
    return executeRemoteCallSingleValueReturn(function);
}

您需要修改生成的HelloWorld.java文件中的getCounter()函数

public RemoteCall<Type> getCounter() {
    final Function function = new Function(
            FUNC_GETCOUNTER, 
            Arrays.<Type>asList(), 
            Arrays.<TypeReference<?>>asList(new TypeReference<Uint>() {}));
    return executeRemoteCallSingleValueReturn(function);
}
Web3Js确实有一个电话。要调用getCounter()方法,请使用以下语法:
contract.methods.getCounter().call()…
Web3Js确实有一个调用。要调用getCounter()方法,请使用以下语法:
contract.methods.getCounter().call()…