我无法理解这个问题。始终返回故障。 数据返回状态代码200,OnResponse不返回。
响应 D / OkHttp:< - 200 OK http://192.168.10.10/oauth/token(667ms)
此代码有什么问题?
App.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
);
AuthModel
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;
}
}
MainActivity
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();
}
});
答案 0 :(得分:0)
问题在于您无法理解如何使用Retrofit库。
让我们看一下您在服务接口中声明的方法。
@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>>
正在告诉改造api调用的响应是什么样的。你在这里要做的是期望看起来像你的AuthModel的东西,不仅仅是一个AuthModel列表。
您实际需要做的是
创建一个代表响应{ "token_type": "Bearer", "expires_in": 31536000}
和
声明您的auth方法调用的预期响应,如Call<MyFunkyNewLoginResponseClass>
如果您在此之后仍然遇到问题,则需要另外提出问题。