gpt4 book ai didi

android - 改造总是调用 onFailure 即使响应是 200

转载 作者:塔克拉玛干 更新时间:2023-11-03 00:57:24 25 4
gpt4 key购买 nike

我正在使用 Retrofit 调用我的 API。我是 Retrofit 的新手,这就是我找不到此错误的原因。我向 API 发送了一个请求,API 响应是 200 OK 但改造调用了 onFailure 方法。我的API接口(interface)如下:

public interface API {

@FormUrlEncoded
@POST("login")
Call<Login> login(@Query("user") String email,@Query("pass") String password, @Field("device key") String deviceKey);

}

我的电话是:

loginCall = MyApplication.getInstance().getAPI().login(username,password,deviceKey);
loginCall.enqueue(new Callback<Login>() {
@Override
public void onResponse(Call<Login> call, Response<Login> response) {

if(response.body() != null)
{
Login login = response.body();
Toast.makeText(LoginActivity.this, response.body().toString(), Toast.LENGTH_LONG).show();
}
}

@Override
public void onFailure(Call<Login> call, Throwable t) {
Toast.makeText(LoginActivity.this, "Failsssssssss", Toast.LENGTH_LONG).show();
}
});

我的模型类如下:

public class Login {
String status;
String Auth_Token;

public Login(String status, String auth_Token) {
this.status = status;
Auth_Token = auth_Token;
}
}

据我了解这个问题,我的模型类有问题。我的 API 返回两个响应:

"{"status":"200","Auth_Token":"jhjsfbah71yb121t312hv"}"
"{"status":"500","Response":"Invalid Cardentials"}"

如有任何帮助,我们将不胜感激。提前致谢。

最佳答案

您的服务器响应有问题。Retrofit 读取 HTTP 状态,所以你必须确保它是从服务器发送的。 Retrofit 正在尝试读取可能不可用的响应代码 200,因此它正在调用失败方法。

关于android - 改造总是调用 onFailure 即使响应是 200,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43809112/

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