1

i used web3j to Transacting with a smart contract. this is my Contract function which i want to intertact with.

function usersRegister(string _id, string _password){
    Equipment e = equipments[msg.sender];
    e.users[e.usersNum++] = User({id: _id, password: _password, registerTime: now});
}

and this is my code in Android using web3j

Web3j web3j = Web3jFactory.build(new HttpService(url));
EthGetTransactionCount ethGetTransactionCount = web3j.ethGetTransactionCount(
                        equipmentAddress, DefaultBlockParameterName.LATEST).sendAsync().get();
                BigInteger nonce = ethGetTransactionCount.getTransactionCount();
BigInteger gasPrice = Contract.GAS_PRICE;
BigInteger gasLimit = Contract.GAS_LIMIT;

List<Type> inputParameters = new ArrayList<>();
Type usersId = new Utf8String(_usersId);
Type usersPassword = new Utf8String(_usersPassword);
inputParameters.add(usersId);
inputParameters.add(usersPassword);
Function function = new Function("usersRegister",
                        inputParameters,
                        Collections.<TypeReference<?>>emptyList());
String functionEncoder = FunctionEncoder.encode(function);
Transaction transaction = Transaction.createFunctionCallTransaction(
                        equipmentAddress, nonce, gasPrice,
                        gasLimit, contractAddress, new BigInteger("0"),
                        functionEncoder);
EthSendTransaction transactionResponse =
                 web3j.ethSendTransaction(transaction).sendAsync().get();

but i get "null" in transactionResponse.getTransactionHash()

someone know what's wrong with my code?thx for help :x

  • Transactions take some time to validate. It might take up to a few minutes. Are you doing your transaction in the background? – noev Aug 24 '17 at 10:46
  • I created an example project, how to read and write to smart contracts. You can find the code on github and some explanation here – noev Aug 24 '17 at 10:49
0

please check the credential. because the the key file in keystore folder is wrong file. an account of credential have to be registered in eth.accounts[] list. I solved the problem. ^^

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.