I am trying to use a function which returns a value of the smart contract (pakoin).
contract Pacoin {
/* This creates an array with all balances */
mapping (address => uint256) public balanceOf;
mapping (address => uint256) public vales;
string public name;
string public symbol;
uint8 public decimals;
address public pakos;
//Here some functions
function nPakoins() public constant returns (uint256 npakoins) {
npakoins = balanceOf[msg.sender];
}
function nVales() public constant returns (uint256 nvales) {
nvales = vales[msg.sender];
}
}
Despite trying different ways, I get plenty of errors and I don't know how to solve them.
BigInteger balance = pakoin.nPakoins(publicKeyUser).send();
However, instead of returning the value, the app suddenly closes and I have the following error on the console:
12-30 10:47:55.215 29089-29105/com.daedasan.sgssi_pakos E/AndroidRuntime: FATAL EXCEPTION: AsyncTask #1 Process: com.daedasan.sgssi_pakos, PID: 29089 java.lang.RuntimeException: An error occurred while executing doInBackground() at android.os.AsyncTask$3.done(AsyncTask.java:318) at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:354) at java.util.concurrent.FutureTask.setException(FutureTask.java:223) at java.util.concurrent.FutureTask.run(FutureTask.java:242) at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:243) at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1133) at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:607) at java.lang.Thread.run(Thread.java:761) Caused by: java.lang.NoSuchMethodError: No interface method getTypeName()Ljava/lang/String; in class Ljava/lang/reflect/Type; or its super classes (declaration of 'java.lang.reflect.Type' appears in /system/framework/core-oj.jar) at org.web3j.abi.TypeReference.getClassType(TypeReference.java:54) at org.web3j.abi.FunctionReturnDecoder.build(FunctionReturnDecoder.java:99) at org.web3j.abi.FunctionReturnDecoder.decode(FunctionReturnDecoder.java:42) at org.web3j.tx.Contract.executeCall(Contract.java:159) at org.web3j.tx.Contract.executeCallSingleValueReturn(Contract.java:165) at org.web3j.tx.Contract.executeCallSingleValueReturn(Contract.java:176) at org.web3j.tx.Contract.lambda$executeRemoteCallSingleValueReturn$1$Contract(Contract.java:232) at org.web3j.tx.Contract$$Lambda$1.call(Unknown Source) at org.web3j.protocol.core.RemoteCall.send(RemoteCall.java:30) at com.daedasan.sgssi_pakos.MainActivity$ReadTask.doInBackground(MainActivity.java:199) at com.daedasan.sgssi_pakos.MainActivity$ReadTask.doInBackground(MainActivity.java:181) at android.os.AsyncTask$2.call(AsyncTask.java:304) at java.util.concurrent.FutureTask.run(FutureTask.java:237) at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:243) at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1133) at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:607) at java.lang.Thread.run(Thread.java:761)
Q: Do you know how to solve this problem or another way to do it? Thank you in advance!
Here is the whole class.
public class MainActivity extends AppCompatActivity {
private final static String publicKeyUser = "0x4B5867c957a0950d4086172cA215Ac432b28b868";
private final static String privateKeyRinkeby = "***MyPrivateKey***";
private final static String greeterContractAddressRinkeby = "0x7a698A3072C01c4a9339ea929Cd2a241bED81937";
private final static String rinkebyUrl = "https://rinkeby.infura.io/hbvHwdGroO3GR4JQJvEl";
//private static final Logger log = LoggerFactory.getLogger(MainActivity.class);
private ProgressBar progressBar;
private ProgressBar progressBar2;
private EditText editText;
private TextView comprarTextView;
private TextView gasPriceTextView;
private TextView gasLimitTextView;
private SeekBar gasPriceSeekBar;
private Button botonNumPakoins;
private EditText nVales;
private Web3j web3j;
private Credentials credentials = Credentials.create(privateKeyRinkeby);
private int minimumGasLimit = 50000;
private BigInteger gasLimit = new BigInteger(String.valueOf(minimumGasLimit));
public MainActivity() throws IOException, CipherException {
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initUi();
setGasPriceText(10);
setGasLimit(minimumGasLimit);
initWeb3j();
}
private void initUi() {
progressBar = (ProgressBar) findViewById(R.id.progressbar);
progressBar2 = (ProgressBar) findViewById(R.id.progressBar2);
comprarTextView = (TextView) findViewById(R.id.ComprarText);
botonNumPakoins = (Button) findViewById(R.id.botonNPakoins);
nVales = (EditText) findViewById(R.id.NValesCanjear);
botonNumPakoins.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
getNumPakoins();
}
});
gasPriceSeekBar = (SeekBar) findViewById(R.id.gas_price_seek_bar);
gasPriceSeekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
@Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
setGasPriceText(progress);
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
}
});
SeekBar gasLimitSeekBar = (SeekBar) findViewById(R.id.gas_limit_seek_bar);
gasLimitSeekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
@Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
setGasLimit(progress + minimumGasLimit);
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
}
});
gasLimitTextView = (TextView) findViewById(R.id.gas_limit_text_view);
gasPriceTextView = (TextView) findViewById(R.id.gas_price_text_view);
}
private void writeGreetingToContract() {
progressBar.setVisibility(View.VISIBLE);
WriteTask writeTask = new WriteTask();
writeTask.execute(editText.getText().toString());
}
private void getNumPakoins(){
try {
progressBar.setVisibility(View.VISIBLE);
numPakoins numeroPakoins = new numPakoins();
numeroPakoins.execute();
} catch (Exception e) {
Log.d("wat", "getDiezBonosexception = " + e.getMessage());
}
}
private void initWeb3j() {
InitWeb3JTask task = new InitWeb3JTask();
task.execute(rinkebyUrl);
}
public void setGasPriceText(int gasPrice) {
String formattedString = getString(R.string.gas_price, String.valueOf(gasPrice));
gasPriceTextView.setText(formattedString);
}
private BigInteger getGasPrice() {
int gasPriceGwei = gasPriceSeekBar.getProgress();
BigInteger gasPriceWei = BigInteger.valueOf(gasPriceGwei + 1000000000L);
Log.d("wat", "getGasPrice: " + String.valueOf(gasPriceGwei));
return gasPriceWei;
}
public void setGasLimit(int gasLimit) {
String gl = String.valueOf(gasLimit);
this.gasLimit = new BigInteger(gl);
gasLimitTextView.setText(getString(R.string.gas_limit, gl));
}
private class numPakoins extends AsyncTask<String, String, String> {
@Override
protected String doInBackground(String... params) {
String result;
try {
Pakoin pakoin = Pakoin.load(greeterContractAddressRinkeby, web3j, credentials, getGasPrice(), gasLimit);
BigInteger balance = pakoin.nPakoins(publicKeyUser).send();
result = balance.toString();
//result = transactionReceipt.getBlockHash();
} catch (Exception e) {
result = "Error reading the smart contract. Error: " + e.getMessage();
}
return result;
}
@Override
protected void onPostExecute(String result) {
super.onPostExecute(result);
progressBar.setVisibility(View.INVISIBLE);
comprarTextView.setText(result);
}
}
private class InitWeb3JTask extends AsyncTask<String, String, String> {
@Override
protected String doInBackground(String... params) {
String url = params[0];
HttpService infuraHttpService;
String result = "Success initializing web3j/infura";
try {
infuraHttpService = new HttpService(url);
web3j = Web3j.build(new HttpService(url));
} catch (Exception wtf) {
String exception = wtf.toString();
Log.d("wat", "Error initializing web3j/infura. Error: " + exception);
}
return result;
}
@Override
protected void onPostExecute(String result) {
super.onPostExecute(result);
Toast.makeText(MainActivity.this, result, Toast.LENGTH_LONG).show();
}
}