Home Forum

How to return a (number) value from a Json file to use it in further calculation

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:

Code (Java):
  1.  calculate.setOnClickListener(new View.OnClickListener() {
  2.             @Override
  3.             public void onClick(View view) {
  4.  
  5.                 inputAmountValue =  Double.valueOf(inputAmount.getText().toString());
  6.  
  7.  
  8.                 affordedQuantity = inputAmountValue /  /*THIS IS WHERE I NEED TO RETURN THE VALUE FROM THE JSON OBJECT */;
  9.  
  10.                 result.setText(String.valueOf(affordedQuantity)); // Showing the value on the screen
  11.  
  12.             }
  13.         });
  14.  
  15.     }
  16.  
  17.     private void apiRequest(String url) {
  18.  
  19.         AsyncHttpClient client = new AsyncHttpClient();
  20.         client.get(url, new JsonHttpResponseHandler() {
  21.  
  22.             @Override
  23.             public void onSuccess(int statusCode, Header[] headers, JSONObject response) {
  24.  
  25.                 Log.d("Bitcoin", "JSON: " + response.toString());
  26.  
  27.                 try{
  28.                     double price = Double.valueOf(response.getString("last")); // this is the value witch i need to use
  29.                 }catch (JSONException e) {
  30.                     e.printStackTrace();
  31.                 }
  32.             }
  33.  
  34.             @Override
  35.             public void onFailure(int statusCode, Header[] headers, Throwable e, JSONObject response) {
  36.                 // called when response HTTP status is "4XX" (eg. 401, 403, 404)
  37.                 Log.d("Bitcoin", "Request fail! Status code: " + statusCode);
  38.                 Log.d("Bitcoin", "Fail response: " + response);
  39.                 Log.e("ERROR", e.toString());
  40.  
  41.             }
  42.         });
  43.  
  44.     }
 

Related:

#1 Ciortea Razvan, Jun 14, 2018 at 12:21 PM
Use the JSON parser

https://www.tutorialspoint.com/android/android_json_parser.htm
 
#2 LV426, Jun 14, 2018 at 2:21 PM