gpt4 book ai didi

java - Retrofit 2 总是返回 onFailure

转载 作者:行者123 更新时间:2023-12-02 13:11:10 27 4
gpt4 key购买 nike

我无法理解这个问题。总是在失败时返回。数据返回状态码200,OnResponse不返回。

回应D/OkHttp: <-- 200 OK http://192.168.10.10/oauth/token (667 毫秒)

这段代码有什么问题?

应用程序.java

public class App extends Application {

private static ApiBackend api;
private Retrofit retrofit;

@Override
public void onCreate() {
super.onCreate();

HttpLoggingInterceptor interceptor = new HttpLoggingInterceptor();
interceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
OkHttpClient client = new OkHttpClient.Builder().addInterceptor(interceptor).build();


retrofit = new Retrofit.Builder()
.baseUrl("http://192.168.10.10/")
.addConverterFactory(GsonConverterFactory.create())
.client(client)
.build();
api = retrofit.create(ApiBackend.class);
}

public static ApiBackend getApi() {
return api;
}
}

界面

@FormUrlEncoded
@POST("oauth/token")
Call<List<AuthModel>> auth(
@Field("username") String username,
@Field("password") String password,
@Field("grant_type") String grant_type,
@Field("client_id") Integer client_id,
@Field(

"client_secret") String client_secret
);

验证模型

public class AuthModel {
List<AuthModel> TaskModel;
@SerializedName("username")
@Expose
private String username;
@SerializedName("password")
@Expose
private String password;
@SerializedName("grant_type")
@Expose
private String grantType;
@SerializedName("client_id")
@Expose
private Integer clientId;
@SerializedName("client_secret")
@Expose
private String clientSecret;

public String getUsername() {
return username;
}

public void setUsername(String username) {
this.username = username;
}

public String getPassword() {
return password;
}

public void setPassword(String password) {
this.password = password;
}

public String getGrantType() {
return grantType;
}

public void setGrantType(String grantType) {
this.grantType = grantType;
}

public Integer getClientId() {
return clientId;
}

public void setClientId(Integer clientId) {
this.clientId = clientId;
}

public String getClientSecret() {
return clientSecret;
}

public void setClientSecret(String clientSecret) {
this.clientSecret = clientSecret;
}

}

主要 Activity

        App.getApi().auth("admin@admin.com","admin123","password",2,"yl9s3UK74vSR1AxlCMhHqHO1AbTz9DzaXUVZyKpA").enqueue(new Callback<List<AuthModel>>() {
@Override
public void onResponse(Call<List<AuthModel>> call, Response<List<AuthModel>> response) {
if (response.isSuccessful()) {
Log.v(TAG,"ffff");
} else {
// error response, no access to resource?
}
}

@Override
public void onFailure(Call<List<AuthModel>> call, Throwable t) {
Toast.makeText(MainActivity.this, "An error occurred during networking", Toast.LENGTH_SHORT).show();
}
});

最佳答案

问题是您无法理解如何使用 Retrofit 库。

让我们看一下您在服务接口(interface)中声明的方法。

@FormUrlEncoded
@POST("oauth/token")
Call<List<AuthModel>> auth(
@Field("username") String username,
@Field("password") String password,
@Field("grant_type") String grant_type,
@Field("client_id") Integer client_id,
@Field(

"client_secret") String client_secret
);

线路Call<List<AuthModel>>告诉 Retrofit api 调用的响应会是什么样子。您在这里告诉它要做的是期待一些看起来像您的 AuthModel 的东西,不仅如此,而且还有一个 AuthModel 列表。

你真正需要做的是

  1. 创建一个代表响应的类 { "token_type": "Bearer", "expires_in": 31536000}

  2. 将其声明为您的 auth 方法调用的预期响应,如下所示 Call<MyFunkyNewLoginResponseClass>

如果此后您仍然遇到问题,那么应该单独提出一个问题。

关于java - Retrofit 2 总是返回 onFailure,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43951222/

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