1

I am trying to execute some functions of my deployed contract in Android Studio through a Java file. I have generated Java Wrapper files. However, when I run Android Studio, there is no data added to the smart contract. Is there anything wrong with my codes? Any help will be appreciated.

Here is my Java Code :

package smth.ui.home;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProvider;

import org.web3j.crypto.Credentials;
import org.web3j.protocol.Web3j;
import org.web3j.protocol.core.methods.response.TransactionReceipt;
import org.web3j.protocol.http.HttpService;
import org.web3j.tx.ManagedTransaction;

import smth.R;
import smth.pojo.UserAccountFactory;

public class HomeFragment extends Fragment {

    private HomeViewModel homeViewModel;

    public View onCreateView(@NonNull LayoutInflater inflater,
                             ViewGroup container, Bundle savedInstanceState) {
        homeViewModel =
                new ViewModelProvider(this).get(HomeViewModel.class);
        View root = inflater.inflate(R.layout.fragment_home, container, false);
        final TextView textView = root.findViewById(R.id.text_home);
        homeViewModel.getText().observe(getViewLifecycleOwner(), new Observer<String>() {
            @Override
            public void onChanged(@Nullable String s) {
                textView.setText(s);
            }
        });

        Web3j web3 = Web3j.build(new HttpService("https://ropsten.etherscan.io/tx/0x7b8418fc1d6d82e221a3ec4519c2a1c0de04a4a5a4c421e2e45ec63c3109737e"));

        Credentials credentials1 = Credentials.create("privateKey");
        try {
            UserAccountFactory contract = UserAccountFactory.load("0x1db931d46dc846bd2e7ff0f33d9eb43fc62e6981", web3, credentials1, ManagedTransaction.GAS_PRICE, UserAccountFactory.GAS_LIMIT);
            TransactionReceipt transactionReceipt = contract.registerNewUser("[email protected]", "Hi1231","Customer").sendAsync().get();
        } catch (Exception e) {
            e.printStackTrace();
        }
        web3.shutdown();

        return root;
    }
}

And here is my JavaWrapper code :

package smth.pojo;

import java.math.BigInteger;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.concurrent.Callable;
import org.web3j.abi.TypeReference;
import org.web3j.abi.datatypes.Function;
import org.web3j.abi.datatypes.Type;
import org.web3j.abi.datatypes.Utf8String;
import org.web3j.abi.datatypes.generated.Bytes32;
import org.web3j.abi.datatypes.generated.Uint256;
import org.web3j.crypto.Credentials;
import org.web3j.protocol.Web3j;
import org.web3j.protocol.core.RemoteCall;
import org.web3j.protocol.core.methods.response.TransactionReceipt;
import org.web3j.tuples.generated.Tuple4;
import org.web3j.tx.Contract;
import org.web3j.tx.TransactionManager;

/**
 * <p>Auto generated code.
 * <p><strong>Do not modify!</strong>
 * <p>Please use the <a href="https://docs.web3j.io/command_line.html">web3j command line tools</a>,
 * or the org.web3j.codegen.SolidityFunctionWrapperGenerator in the 
 * <a href="https://github.com/web3j/web3j/tree/master/codegen">codegen module</a> to update.
 *
 * <p>Generated with web3j version 3.3.1.
 */
public class UserAccountFactory extends Contract {
    private static final String BINARY = null;

    protected static final HashMap<String, String> _addresses;

    static {
        _addresses = new HashMap<>();
    }

    protected UserAccountFactory(String contractAddress, Web3j web3j, Credentials credentials, BigInteger gasPrice, BigInteger gasLimit) {
        super(BINARY, contractAddress, web3j, credentials, gasPrice, gasLimit);
    }

    protected UserAccountFactory(String contractAddress, Web3j web3j, TransactionManager transactionManager, BigInteger gasPrice, BigInteger gasLimit) {
        super(BINARY, contractAddress, web3j, transactionManager, gasPrice, gasLimit);
    }

    public static RemoteCall<UserAccountFactory> deploy(Web3j web3j, Credentials credentials, BigInteger gasPrice, BigInteger gasLimit) {
        return deployRemoteCall(UserAccountFactory.class, web3j, credentials, gasPrice, gasLimit, BINARY, "");
    }

    public static RemoteCall<UserAccountFactory> deploy(Web3j web3j, TransactionManager transactionManager, BigInteger gasPrice, BigInteger gasLimit) {
        return deployRemoteCall(UserAccountFactory.class, web3j, transactionManager, gasPrice, gasLimit, BINARY, "");
    }

    public RemoteCall<TransactionReceipt> editUserDetails(BigInteger _userNo, String _oldPassword, String _newPassword) {
        final Function function = new Function(
                "editUserDetails", 
                Arrays.<Type>asList(new org.web3j.abi.datatypes.generated.Uint256(_userNo), 
                new org.web3j.abi.datatypes.Utf8String(_oldPassword), 
                new org.web3j.abi.datatypes.Utf8String(_newPassword)), 
                Collections.<TypeReference<?>>emptyList());
        return executeRemoteCallTransaction(function);
    }

    public RemoteCall<TransactionReceipt> loginUser(BigInteger _userNo, String _password) {
        final Function function = new Function(
                "loginUser", 
                Arrays.<Type>asList(new org.web3j.abi.datatypes.generated.Uint256(_userNo), 
                new org.web3j.abi.datatypes.Utf8String(_password)), 
                Collections.<TypeReference<?>>emptyList());
        return executeRemoteCallTransaction(function);
    }

    public RemoteCall<TransactionReceipt> registerNewUser(String _email, String _password, String _accountType) {
        final Function function = new Function(
                "registerNewUser", 
                Arrays.<Type>asList(new org.web3j.abi.datatypes.Utf8String(_email), 
                new org.web3j.abi.datatypes.Utf8String(_password), 
                new org.web3j.abi.datatypes.Utf8String(_accountType)), 
                Collections.<TypeReference<?>>emptyList());
        return executeRemoteCallTransaction(function);
    }

    public RemoteCall<BigInteger> userCount() {
        final Function function = new Function("userCount", 
                Arrays.<Type>asList(), 
                Arrays.<TypeReference<?>>asList(new TypeReference<Uint256>() {}));
        return executeRemoteCallSingleValueReturn(function, BigInteger.class);
    }

    public RemoteCall<Tuple4<BigInteger, String, byte[], String>> userList(BigInteger param0) {
        final Function function = new Function("userList", 
                Arrays.<Type>asList(new org.web3j.abi.datatypes.generated.Uint256(param0)), 
                Arrays.<TypeReference<?>>asList(new TypeReference<Uint256>() {}, new TypeReference<Utf8String>() {}, new TypeReference<Bytes32>() {}, new TypeReference<Utf8String>() {}));
        return new RemoteCall<Tuple4<BigInteger, String, byte[], String>>(
                new Callable<Tuple4<BigInteger, String, byte[], String>>() {
                    @Override
                    public Tuple4<BigInteger, String, byte[], String> call() throws Exception {
                        List<Type> results = executeCallMultipleValueReturn(function);
                        return new Tuple4<BigInteger, String, byte[], String>(
                                (BigInteger) results.get(0).getValue(), 
                                (String) results.get(1).getValue(), 
                                (byte[]) results.get(2).getValue(), 
                                (String) results.get(3).getValue());
                    }
                });
    }

    public static UserAccountFactory load(String contractAddress, Web3j web3j, Credentials credentials, BigInteger gasPrice, BigInteger gasLimit) {
        return new UserAccountFactory(contractAddress, web3j, credentials, gasPrice, gasLimit);
    }

    public static UserAccountFactory load(String contractAddress, Web3j web3j, TransactionManager transactionManager, BigInteger gasPrice, BigInteger gasLimit) {
        return new UserAccountFactory(contractAddress, web3j, transactionManager, gasPrice, gasLimit);
    }

    protected String getStaticDeployedAddress(String networkId) {
        return _addresses.get(networkId);
    }

    public static String getPreviouslyDeployedAddress(String networkId) {
        return _addresses.get(networkId);
    }
}

1 Answer 1

1

I suspect you have the HTTP service endpoint wrong.

If I check that link, it looks like it refers to a certain transaction.

However, when you create a web3j instance, you should specify a node endpoint. You can get one using the infura API, or you need to set up an Ethereum client (Geth for example) connecting to the Ropsten network and put its address when creating the web3j instance.

You can refer to docs for more information.

Also, sendAsync().get() is same as calling send().

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.