简体   繁体   中英

Android display boolean from Retrofit as false ALWAYS

In my Main Activity i execute SincronizacionDb to compare the data between SQLite and the MYSQL Server, however the json_array retrieved works fine, it's show true or false if the data it's different from the server, but in the APP, this value seems to be always false.

SincronizacionDb

private void sincronizacionDB() {
    if (dbCheck(MainActivity.this, "beneficiosColaborador.db")) {
        int size = sqLiteHandler.getLastIdBeneficio();
        Call<VersionDb> versionDb = restManager.getApiService().checkServerDatabaseVersion(size);
        versionDb.enqueue(new Callback<VersionDb>() {
            @Override
            public void onResponse(Call<VersionDb> call, Response<VersionDb> response) {
                Log.e("RESPONSE", "CALLING ON RESPONSE");
                VersionDb version = response.body();
                isOutdate = version.getEstado();
                Log.i("VERSION", "RESPUESTA" + version.getEstado()); // Get the value from GSon(TRUE, FALSE) WORKS PERFECT
            }

            @Override
            public void onFailure(Call<VersionDb> call, Throwable t) {
                Log.e("error", t.getMessage());
            }
        });
        Log.e("outdate","" + isOutdate); // ALWAYS FALSE
        if (isOutdate) {
            sqLiteHandler.deleteBeneficio();
            Log.e("Beneficios", "Se han eliminado los beneficios viejos");
            Call<List<Beneficios>> callBeneficios = restManager.getApiService().getListadoBeneficios();
            callBeneficios.enqueue(new Callback<List<Beneficios>>() {
                @Override
                public void onResponse(Call<List<Beneficios>> call, Response<List<Beneficios>> response) {
                    List<Beneficios> beneficioData = response.body();
                    sqLiteHandler.addListBeneficios(beneficioData);
                }

                @Override
                public void onFailure(Call<List<Beneficios>> call, Throwable t) {
                    Log.e("error", t.getMessage());
                    //Toast.makeText(MainActivity.this, t.getMessage(), Toast.LENGTH_LONG).show();
                }
            });
        }
    }else{
        Call<List<Beneficios>> callBeneficios = restManager.getApiService().getListadoBeneficios();
        callBeneficios.enqueue(new Callback<List<Beneficios>>() {
            @Override
            public void onResponse(Call<List<Beneficios>> call, Response<List<Beneficios>> response) {
                List<Beneficios> beneficioData = response.body();
                sqLiteHandler.addListBeneficios(beneficioData);
            }

            @Override
            public void onFailure(Call<List<Beneficios>> call, Throwable t) {
                Log.e("error", t.getMessage());
                //Toast.makeText(MainActivity.this, t.getMessage(), Toast.LENGTH_LONG).show();
            }
        });
    }
}

JSON TRUE示例

JSON FALSE示例

and the Results i got in the ANDROID MONITOR

    08-11 08:39:31.417 13618-13618/com.freelance.crdzbird_dev.clarobadge D/SQLiteHandler: Fetching LAST ROW from Sqlite: 213
08-11 08:39:31.617 13618-13618/com.freelance.crdzbird_dev.clarobadge E/outdate: false
08-11 08:39:31.867 13618-13618/com.freelance.crdzbird_dev.clarobadge E/RESPONSE: CALLING ON RESPONSE
08-11 08:39:31.867 13618-13618/com.freelance.crdzbird_dev.clarobadge I/VERSION: RESPUESTA true

Please someone explain me how to avoid this error :(

It seems that you are doing an asynchrounous call in here :

versionDb.enqueue(new Callback<VersionDb>() {
            @Override
            public void onResponse(Call<VersionDb> call, Response<VersionDb> response) {
                Log.e("RESPONSE", "CALLING ON RESPONSE");
                VersionDb version = response.body();
                isOutdate = version.getEstado();
                Log.i("VERSION", "RESPUESTA" + version.getEstado()); // Get the value from GSon(TRUE, FALSE) WORKS PERFECT
            }

            @Override
            public void onFailure(Call<VersionDb> call, Throwable t) {
                Log.e("error", t.getMessage());
            }
        });

And your value of isOutdate is initialized at false, so it will be always false since it executes the check before returning from the service.

So either wait for your callback or use the synchronized method execute in retrofit.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM