簡體   English   中英

如何使用 web3j 從 android 應用程序調用智能合約方法

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

我想從 Android 應用程序調用智能合約方法。 我在我的專用網絡中使用 truffle 框架部署了智能合約。 我還使用 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 應用程序中?

當應用程序在您的手機/模擬器上運行時,您正在 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;
}

這樣錢包文件被創建並存儲在下載目錄中,稍后使用此路徑加載您的錢包

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM