I have a request

public void onResponse(Call<Count> call, Response<Count> response) { Log.d("Response", String.valueOf(response.code())); if (response.body().getmCount().equals("2")) { Toast.makeText(getApplicationContext(), "У вас осталось: " + response.body().getmCount(), Toast.LENGTH_LONG); } else if (response.body().getmCount().equals("1")) { Toast.makeText(getApplicationContext(), "У вас осталось: " + response.body().getmCount(), Toast.LENGTH_LONG); } else if (response.body().getmCount().equals("0")){ Toast.makeText(getApplicationContext(),"У вас осталось: "+response.body().getmCount(),Toast.LENGTH_LONG); mCount = 0; } } @Override public void onFailure(Call<Count> call, Throwable t) { } }); 

But I have an error from server 401, with this error I need to send a new request, how to do it?

  • I think you need to do it in onFailure , depending on the responce code - Vladyslav Matviienko
  • @metalurgus can give you a concrete example - Martinez Toni

1 answer 1

  1. Create a separate method in which to place the launch of a retrofit of the desired request.
  2. In case of an error or code 401, like yours, run this method
  3. Call return to not execute the code further.