A
A
Alexzatey2020-05-15 01:14:58
Android
Alexzatey, 2020-05-15 01:14:58

Why doesn't the if, else if, else logic work in AsyncTask in the doInBackground method?

There is no "RUB" Currency in Json. Decided to register manually. But when selecting " RUB " in the spinner and passing this value to doInBackground , the logic does not work, a NullPointerException is thrown.

@Override
    protected String doInBackground(String... params) {
        Call<JsonObject> request = callObject.call();
        JsonObject Value2;
        JsonObject Value1;


        try {
            Response<JsonObject> response = request.execute(); 
            JsonObject reader = response.body();
            JsonObject Valute = reader.getAsJsonObject("Valute");
            double a=1.0;
            if (params[0]=="RUB") {
                Value2 = Valute.getAsJsonObject(params[1]);
                out = String.valueOf(Precision.round(a/Value2.get("Value").getAsDouble(),3));
                return out;
            }
            else if (params[1]=="RUB"){
                Value1 = Valute.getAsJsonObject(params[0]);
                out = String.valueOf(Precision.round(Value1.get("Value").getAsDouble()/a,3));
                return out;
            }
            else if (params[0]=="RUB" & params[1]=="RUB") { out = String.valueOf(1.0);
                 return out;
             }
            else {
                Value1 = Valute.getAsJsonObject(params[0]);
                Value2 = Valute.getAsJsonObject(params[1]);
                out = String.valueOf(Precision.round(Value1.get("Value") //Использовал библиотеку
                        .getAsDouble()/Value2.get("Value").getAsDouble(),3));// APACH для округления чисел
                return out;
            }


        } catch (IOException e) {
            e.printStackTrace();
        }
        return out;
    }

Answer the question

In order to leave comments, you need to log in

1 answer(s)
O
one pavel, 2020-05-15
@Alexzatey

1. As far as I remember, in java, you should not compare strings like this
params[0]=="RUB"
for this there is equals,
even if it is an attempt to compare by reference
2. I guess you are not writing in a notepad,
put a breakpoint on the first line of the method,
yes take a look at what comes into the variables

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question