Reputation: 3
I can not understand the problem. Always returns onFailure. Data returns status code 200, OnResponse does not return.
Response D/OkHttp: <-- 200 OK http://192.168.10.10/oauth/token (667ms)
What's wrong with this code?
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;
}
}
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
);
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("[email protected]","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();
}
});
Upvotes: 0
Views: 2898
Reputation: 4323
The problem is that you are failing to understand how to use the Retrofit library.
Lets have a look at method you have declared in your service 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
);
The line Call<List<AuthModel>>
is telling retrofit what the response of the api call will look like. What you are telling it to do here is to expect something that looks like your AuthModel, not only that but a List of AuthModels.
What you actually need to do is to
create a class that represents the response { "token_type": "Bearer", "expires_in": 31536000}
and
declare that as the expected response of your auth method call like so Call<MyFunkyNewLoginResponseClass>
If you are still experiencing problems after that then that should warrant a separate question.
Upvotes: 1