1. Download our Official Android App: Forums for Android!

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

Discussion in 'Android Development' started by Ciortea Razvan, Jun 14, 2018.

  1. Ciortea Razvan

    Thread Starter
    Rank:
    None
    Points:
    5
    Posts:
    7
    Joined:
    Jun 7, 2018

    Jun 7, 2018
    7
    0
    5
    Male
    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.     }
     

    Advertisement

  2. LV426

    LV426 I say we take off and nuke this place from orbit
    Moderator
    Rank:
     #11
    Points:
    1,988
    Posts:
    7,282
    Joined:
    Oct 16, 2015

    Oct 16, 2015
    7,282
    10,384
    1,988
    Male
    Software developer
    South West of England

Share This Page

Loading...