Read contract value from Android

by Seraphin   Last Updated May 15, 2019 17:28 PM

I have deployed successfully a smartcontract onto Rinkeby at the address : 0x69cfa9009Bf2c7a9945a4f8b381db21b26b662d7

It is a tutorial smartcontract that enables an election between 2 candidate.

I have generate the Contracts_Election_sol_Election.java file using solc. It seems to have worked properly. Then I have imported that java file into my Android Studio project.

In the MainActivity, I do the following steps

 //Construct Ethereum client --> it seems to compile fine
String someInfuraProjectURL = "https://"+"rinkeby.infura.io/v3/1e514798ab3948509f201baab9c9e611";
Web3j web3 = Web3j.build(new HttpService(someInfuraProjectURL));

//Create credential --> you can use my private key, it is just for test. It seems to work fine
Credentials someCredential = Credentials.create("0x59c451f4813b235631d7359a17af2d436931e1b02ea46cb9b580ea68f5714c58");

//Load the contract --> it seems to compile fine
Contracts_Election_sol_Election contract = Contracts_Election_sol_Election.load(
              "0x69cfa9009Bf2c7a9945a4f8b381db21b26b662d7", web3, someCredential, GAS_PRICE, GAS_LIMIT);

Then, it is where everything seems to become complicated. I want to call a function from the contract called CandidatesCount(). I tried many ways like that one :

Callable<BigInteger> task = () -> {
            RemoteCall<BigInteger> rc = contract.CandidatesCount();
            return rc.send();
        };
        Future<BigInteger> future = Async.run(task);
        BigInteger big = future.get();

or that one

RemoteCall<BigInteger> someRemoteCall  = contract.CandidatesCount();
        CompletableFuture<BigInteger> i = someRemoteCall.sendAsync();
        BigInteger someInt = BigInteger.valueOf(-1);
            someInt = i.get();

But I always get the same issue everytime I call the .get() I get this exception :

error: unreported exception ExecutionException; must be caught or declared to be thrown

Can you please advise what I might be doing wrong here ? Please be indulgent if I am just missing something basic. I am a new to smartcontract and far aways to be expert in Android development.

Best regards



Related Questions


Updated April 07, 2018 04:28 AM

Updated January 02, 2018 17:28 PM

Updated August 22, 2017 23:28 PM

Updated November 02, 2018 16:28 PM