如何使用java从solidity合同中获取价值

如何使用java从solidity合同中获取价值,java,ethereum,solidity,Java,Ethereum,Solidity,我的合同如下: contract SimpleStorage { uint storedData; function set(uint x) { storedData = x; } function get() constant returns (uint retVal) { return storedData; }} [ { "constant": false, "inputs": [ { "name": "x", "type": "uint256" } ], "name

我的合同如下:

contract SimpleStorage {
uint storedData;

function set(uint x) {
    storedData = x;
}

function get() constant returns (uint retVal) {
    return storedData;
}}
[ { "constant": false, "inputs": [ { "name": "x", "type": "uint256" } ], "name": "set", "outputs": [], "type": "function" }, { "constant": true, "inputs": [], "name": "get", "outputs": [ { "name": "retVal", "type": "uint256", "value": "0" } ], "type": "function" } ]
并生成以下abi:

contract SimpleStorage {
uint storedData;

function set(uint x) {
    storedData = x;
}

function get() constant returns (uint retVal) {
    return storedData;
}}
[ { "constant": false, "inputs": [ { "name": "x", "type": "uint256" } ], "name": "set", "outputs": [], "type": "function" }, { "constant": true, "inputs": [], "name": "get", "outputs": [ { "name": "retVal", "type": "uint256", "value": "0" } ], "type": "function" } ]
并由


如何使用java(非js)调用get函数并获取值

以下是java中的一个示例(在Spring Boot下) 祝你好运

迎合了这个用例。它从Solidity编译的二进制文件和ABI文件生成Java智能合约包装器

使用web3j生成包装器代码后,您将能够部署,然后调用上述契约示例中的方法,如下所示:

SimpleStorage simpleStorage = SimpleStorage.deploy(
    <web3j>, <credentials>, GAS_PRICE, GAS_LIMIT,
    BigInteger.ZERO);  // ether value of contract

TransactionReceipt transactionReceipt = simpleStorage.set(
        new Uint256(BigInteger.valueOf(1000))),
        .get();

Uint256 result = simpleStorage.get()
        .get();
simplestreage simplestreage=simplestreage.deploy(
,燃气价格,燃气限值,
BigInteger.ZERO);//合同价值
TransactionReceipt TransactionReceipt=simpleStorage.set(
新的Uint256(BigInteger.valueOf(1000)),
.get();
Uint256结果=simpleStorage.get()
.get();
注意:额外的
get()
是因为web3j在与以太坊客户端交互时返回Java Futures


有关更多信息,请参阅。

#Conor我已经了解了您的答案,很高兴能够理解,但我面临一些问题。请您看看这个并帮助我#Conor我想从android应用程序调用智能合约方法。因此,我使用truffle框架部署了智能合约,并为智能合约生成了包装器类,它在android项目中创建了.java文件,因此我将set和get方法调用到android中,但通过提供waller path的凭据详细信息,它显示了一些错误。这一行是org.web3j.crypto.Credentials Credentials=WalletUtils.loadCredentials(“,”/usr/local/ceral/test3/keystore/UTC-2018-03-13T09-19-38.721370320Z-191A1BE7C53236F916AF551239A5092F7059”)。我的钱包地址不正确,请看这个