如何使用web3j从android应用程序调用智能合约方法 [英] How to call the smart contract methods from android app using web3j
问题描述
我想从Android应用程序调用智能合约方法.我在我的私有网络中使用松露框架部署了智能合约.我还使用web3j命令为已部署的智能合约创建了包装器类.它在我的Android项目中创建一个.java文件.
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.
我想从我的Android应用程序中调用智能合约方法.这是我的代码:
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();
}
}
});
}
}
我收到错误:
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应用程序中调用这些方法?
How do I give the wallet address and how do I call those methods into my Android app?
推荐答案
当应用程序在手机/模拟器上运行时,您正在PC上提供路径文件.
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
这篇关于如何使用web3j从android应用程序调用智能合约方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!