Search code examples
androidandroid-ion

Send double value as body parameter to url using ion library?


I am trying to send a double value via Ion Library call. I can't seem to send a double value and this messes up my code. I am trying to convert INR to USD and vice versa via an api call. My double value is 'amount'.

Ion.with(context)
                .load("POST",url)
                .setBodyParameter("amount",amount)
                .asString()
                .setCallback(new FutureCallback<String>() {
                    @RequiresApi(api = Build.VERSION_CODES.N)
                    @Override
                    public void onCompleted(Exception e, String result) {
                        try {
                            JSONObject o = new JSONObject(result);
                            if (o.getString("code").equals("200")) {
                                PriceModel.Instance().setUSDEquivalent(o.getDouble("result"));
                            }

                        } catch (JSONException e1) {
                            e1.printStackTrace();
                        }
                    }
                });

Solution

  • You need to convert double to String as ION only allows String as body parameter. So you can do as follow:

    Ion.with(context)
    .load("POST",url)
    .setBodyParameter("amount",String.valueOf(amount))..
    

    Reference: IonRequestBuilder.setBodyParameter(String name, String value)