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

余生长醉 提交于 2019-12-24 18:18:26

问题


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?

来源:https://stackoverflow.com/questions/50654417/how-to-call-the-smart-contract-methods-from-android-app-using-web3j

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!