Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Enqueue callback not called

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.

like image 338
Lucas Charles Avatar asked Mar 21 '26 00:03

Lucas Charles


1 Answers

You have empty onFailure implementation so callback may be called but nothing happens. Try to log some information in onFailure method.

@Override
public void onFailure(Call<String> call, Throwable t) {
   Log.w("MyTag", "requestFailed", t); 
}

And you'll see if request failed and why.

like image 123
Josef Adamcik Avatar answered Mar 23 '26 14:03

Josef Adamcik