Enqueue callback not called

Viewed 3023

I am using retrofit2 and recognized that sometimes the callback for Call.enqueue is not called.

Retrofit retrofit = new Retrofit.Builder().baseUrl(usuarioService.BASE_URL).addConverterFactory(GsonConverterFactory.create(usuarioService.g)).build();
usuarioService service = retrofit.create(usuarioService.class);
Call<String> user = service.verificarUsuario(login.getText().toString(), senha.getText().toString());
user.enqueue(new retrofit2.Callback<String>() {
    @Override
    public void onResponse(Call<String> call, Response<String> response) {
        String resultado = response.body();

        if (resultado.equals("false")) {
            Toast toast = Toast.makeText(MainActivity.this, "Senha ou usuário não existente", Toast.LENGTH_SHORT);
            toast.setGravity(Gravity.TOP | Gravity.CENTER_VERTICAL, 0, 0);
            toast.show();
        } else {
            if (resultado.equals("true")) {
                Toast toast = Toast.makeText(MainActivity.this, "ACESSO PERMITIDO", Toast.LENGTH_SHORT);
                toast.setGravity(Gravity.CENTER_HORIZONTAL | Gravity.CENTER_VERTICAL, 0, 0);
                toast.show();

                Intent intent = new Intent(MainActivity.this, MenuDrawer.class);
                intent.putExtra("chave1", login.getText().toString());

                startActivity(intent);

                finish();
            }
        }
    }

    @Override
    public void onFailure(Call<String> call, Throwable t) {
    }
});

Suddenly that problem start to happen, does anybody knows how to fix it? These issue do not happen until some moments ago, I do not know what happened.

These happened just when i'm using mobile data, but when i'm used WIFI these never happened yet.

3 Answers

The first thing to look for is as Josef Adamcik said in onFailiure.

The second thing that you might want to check calling the api directly, like in onCreate or onViewCreated.

When I called the api inside another function, i did not get both the callbacks, both success and failure, because it was under a try catch block and inside the catch block there was nothing, no logs or you are searching for the wrong tag in logcat. So try removing try catch if your code is surrounded by it or put Log.e inside the catch block and try again. Some errors like @FormUrlEncoded not present can hit the catch block and if you don't have a Log.e inside the catch block, you will never know why your api is not getting called.

Related