• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

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:

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());

            }
        });

    }
 

BEST TECH IN 2023

We've been tracking upcoming products and ranking the best tech since 2007. Thanks for trusting our opinion: we get rewarded through affiliate links that earn us a commission and we invite you to learn more about us.

Smartphones