7

I found this documentationQuerying the state of a smart contract. but i can't get resault,this is my code.

                Web3j web3j = Web3jFactory.build(new HttpService(url));
                List<Type> inputParameters = new ArrayList<>();
                List<TypeReference<?>> outputParameters = new ArrayList<>();
                Function function = new Function("getManufacturer",
                        inputParameters,
                        outputParameters);
                String functionEncoder = FunctionEncoder.encode(function);
                EthCall response = web3j.ethCall(
                        Transaction.createEthCallTransaction(contractAddress,
                        DefaultBlockParameterName.LATEST
                ).sendAsync().get();
                List<Type> someType = FunctionReturnDecoder.decode(response.getValue(),function.getOutputParameters());
                Iterator<Type> it = someType.iterator();
                Type resault = someType.get(0);
                String a = resault.toString();
                Log.d("MainActitity", a + "111");

someone know how to fix it.:)

1
  • the log is someType 's size is 0.
    – Song
    Commented Mar 22, 2017 at 3:15

1 Answer 1

8

Love that your using Web3J, I think with a little practice you will find that it's a great API for dealing with Ethereum :-) So regarding your question there are two things:

  1. I think your problem is the third line in your code. The list "outputParameters" should contain all return variables of the method. So if you are expecting a String you should do something like this:

    Arrays.asList(new TypeReference< Utf8String>() {})

  2. But you will probably find even more interesting that you don't even need to worry about writing Java code for your solidity functions because Web3J can do that for you. The SolidityFunctionWrapperGenerator (https://docs.web3j.io/smart_contracts.html#smart-contract-wrappers) generate these for you. All you need is the .bin and .abi of your smart contract and it gives you a Java-class. You can instantiate that class with the address, a Web3J instance, and your Credentials.java. With that you can call methods on your smart contract in the blockchain like on any other Java-class. I strongly suggest looking into that.

2
  • 1
    yes,u are right: The list "outputParameters" should contain all return variables of the method. I've solved it.thx :)
    – Song
    Commented Mar 23, 2017 at 3:54
  • 1
    hi @max are you there? please have a look at this questions: ethereum.stackexchange.com/questions/133944/… Commented Aug 19, 2022 at 18:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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