简体   繁体   中英

How to call the smart contract methods from android app using web3j

I want to call the smart contract methods from an Android app. I deployed the smart contract using truffle framework in my private network. I also created a wrapper class for the deployed smart contract using web3j commands. It creates a .java file in my Android project.

I want to call smart contract methods from my Android app. This is my code:

import android.net.Credentials;
import android.renderscript.Type;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.bodaty.deyaPay.Product;

import org.web3j.crypto.CipherException;
import org.web3j.crypto.WalletUtils;
import org.web3j.protocol.Web3j;
import org.web3j.protocol.Web3jFactory;
import org.web3j.protocol.core.RemoteCall;
import org.web3j.protocol.core.methods.response.TransactionReceipt;
import org.web3j.protocol.http.HttpService;
import org.web3j.tx.Contract;

import java.io.IOException;

public class MainActivity extends AppCompatActivity {
    TextView t;
    EditText e;
    Button b1,b2;
    Web3j web3 = Web3jFactory.build(new HttpService());
    org.web3j.crypto.Credentials credentials = WalletUtils.loadCredentials(" ","/usr/local/Cellar/test3/keystore/UTC--2018-03-13T09-19-38.721370320Z--191a1be7c53236f916af5512329a5092f70fab59 ");
    Product p = Product.load("0x3ed0585f956acc406dc0f5e97524b6a2d30bce57",web3,credentials, Contract.GAS_LIMIT,Contract.GAS_PRICE);
    public MainActivity() throws IOException, CipherException {

    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        t = (TextView) findViewById(R.id.textView);
        e = (EditText) findViewById(R.id.editText);
        b1 = (Button) findViewById(R.id.button);
        b2 = (Button) findViewById(R.id.button2);
        b1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String a = e.getText().toString();

                //Product p = Product.load("0x3ed0585f956acc406dc0f5e97524b6a2d30bce57",web3,credentials, Contract.GAS_LIMIT,Contract.GAS_PRICE);
                try {
                    TransactionReceipt result = p.set(a).send();
                } catch (Exception e1) {
                    e1.printStackTrace();
                }

            }
        });
        b2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String a = e.getText().toString();
                try {
                    RemoteCall<String> result = p.get(a);
                } catch (Exception e1) {
                    e1.printStackTrace();
                }
            }
        });

    }
}

I am receiving the error: java.io.FileNotFoundException: /usr/local/Cellar/test3/keystore/UTC--2018-03-13T09-19-38.721370320Z--191a1be7c53236f916af5512329a5092f70fab59 : open failed: ENOENT (No such file or directory)

How do I give the wallet address and how do I call those methods into my Android app?

You are providing a path file on your pc while the app is running on your phone/emulator.

    public static Bip39Wallet createBipWallet(String password) throws Exception {
    String path = Environment.getExternalStoragePublicDirectory(DIRECTORY_DOWNLOADS).getPath();
    Bip39Wallet bip39Wallet = WalletUtils.generateBip39Wallet(password, new File(path));
    return bip39Wallet;
}

This way the wallet file is created and stored in the downloads directory, use this path later to load your wallet

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM