我不明白这个问题。总是返回onFailure。数据返回状态代码200,OnResponse不返回。
响应D/OkHttp:<- 200 OK http://192.168.10.10/oauth/token (667 OK)
这个密码怎么了?
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();
}
});发布于 2017-05-13 10:52:10
问题是,您无法理解如何使用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的东西,不仅是这样,而且还有一个AuthModels列表。
你真正需要做的是
{ "token_type": "Bearer", "expires_in": 31536000}和Call<MyFunkyNewLoginResponseClass>如果你在那之后还在经历问题,那就应该有一个单独的问题。
https://stackoverflow.com/questions/43951222
复制相似问题