gpt4 book ai didi

java - Retrofit 不调用 failure 并且 response.code() 是 400

转载 作者:行者123 更新时间:2023-11-29 08:35:31 24 4
gpt4 key购买 nike

我确定我在这里做的事情有问题。即使我得到的代码是 400,Retrofit 也不会失败。

        AuthenticationService authService = retrofit.create(AuthenticationService.class);
Call<ValidateTokenResponseMessage> request = authService.validateToken(token);
request.enqueue(new Callback<ValidateTokenResponseMessage>() {
@Override
public void onResponse(Call<ValidateTokenResponseMessage> call, retrofit2.Response<ValidateTokenResponseMessage> response) {
// When I try to put a breakpoint here I can see that response.code() is 400.
ValidateTokenResponseMessage body = response.body();
if (!body.getValidToken()) {
// do success
}
}

@Override
public void onFailure(Call<ValidateTokenResponseMessage> call, Throwable t) {
// do failure
}
});

最佳答案

Callback.onFailure仅在缺少互联网连接或无效 https 等异常情况下被调用。

Invoked when a network exception occurred talking to the server or when an unexpected exception occurred creating the request or processing the response.

要检查错误,您必须使用 Response.isSuccessful()

Returns true if code() is in the range [200..300).

关于java - Retrofit 不调用 failure 并且 response.code() 是 400,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44399865/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com