如何使用web3j从Android应用程序调用智能合约方法

时间:2018-06-02 07:07:24

标签: java android smartcontracts web3-java

我想从Android应用程序调用智能合约方法。我在私人网络中使用松露框架部署了智能合约。我还使用web3j命令为部署的智能合约创建了一个包装类。它在我的Android项目中创建一个.java文件。

我想从我的Android应用程序中调用智能合约方法。这是我的代码:

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();
                }
            }
        });

    }
}

我收到错误: java.io.FileNotFoundException: /usr/local/Cellar/test3/keystore/UTC--2018-03-13T09-19-38.721370320Z--191a1be7c53236f916af5512329a5092f70fab59 : open failed: ENOENT (No such file or directory)

如何提供钱包地址以及如何将这些方法调用到我的Android应用程序中?

1 个答案:

答案 0 :(得分:0)

当应用程序在手机/模拟器上运行时,您正在PC上提供路径文件。

    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;
}

通过这种方式创建钱包文件并将其存储在下载目录中,稍后使用此路径加载您的钱包