3

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.:)

  • the log is someType 's size is 0. – Song Mar 22 '17 at 3:15
5

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.

  • yes,u are right: The list "outputParameters" should contain all return variables of the method. I've solved it.thx :) – Song Mar 23 '17 at 3:54

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.