I need to return a value from onSuccess() method to use it in some calculation in the onCreate() method, Actually i can use any advice and any way to make the calculation with the "price" variable and show a result on the screen . Thank's.
Here is the code:
Here is the code:
Java:
calculate.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
inputAmountValue = Double.valueOf(inputAmount.getText().toString());
affordedQuantity = inputAmountValue / /*THIS IS WHERE I NEED TO RETURN THE VALUE FROM THE JSON OBJECT */;
result.setText(String.valueOf(affordedQuantity)); // Showing the value on the screen
}
});
}
private void apiRequest(String url) {
AsyncHttpClient client = new AsyncHttpClient();
client.get(url, new JsonHttpResponseHandler() {
@Override
public void onSuccess(int statusCode, Header[] headers, JSONObject response) {
Log.d("Bitcoin", "JSON: " + response.toString());
try{
double price = Double.valueOf(response.getString("last")); // this is the value witch i need to use
}catch (JSONException e) {
e.printStackTrace();
}
}
@Override
public void onFailure(int statusCode, Header[] headers, Throwable e, JSONObject response) {
// called when response HTTP status is "4XX" (eg. 401, 403, 404)
Log.d("Bitcoin", "Request fail! Status code: " + statusCode);
Log.d("Bitcoin", "Fail response: " + response);
Log.e("ERROR", e.toString());
}
});
}