我可能做错了什么,我正在尝试登录API并且没有数据返回,并且它返回错误代码400 我在做什么错了?
这是我的API详细信息:Image from postman take look
我的改造API代码:
private static Retrofit retrofit;
private static Retrofit.Builder builder;
public static final String BASE_URL = "http://tajmeelapi.red-uae.com/Tajmeel/Api/";
public static Retrofit getRetrofitInstance() {
if (retrofit == null) {
builder = new Retrofit.Builder()
.baseUrl(BASE_URL)
.addConverterFactory(GsonConverterFactory.create());
}
return retrofit =builder.build();
}
我的登录方法:
@POST("token")
Call<TokenResponse> loginUser(@Body Login login);
我的令牌响应:
public class TokenResponse {
private String access_token;
private String token_type;
private Integer expires_in;
@SerializedName(".issued")
@Expose
private String issued;
@SerializedName(".expires")
@Expose
private String expires;
public String getAccess_token() {
return access_token;
}
public void setAccess_token(String access_token) {
this.access_token = access_token;
}
public String getToken_type() {
return token_type;
}
public void setToken_type(String token_type) {
this.token_type = token_type;
}
public Integer getExpires_in() {
return expires_in;
}
public void setExpires_in(Integer expires_in) {
this.expires_in = expires_in;
}
public String getExpires() {
return expires;
}
public void setExpires(String expires) {
this.expires = expires;
}
public String getIssued() {
return issued;
}
public void setIssued(String issued) {
this.issued = issued;
}
}
我的登录课程:
public class Login {
@SerializedName("UserName")
@Expose
private String UserName;
@SerializedName("Password")
@Expose
private String Password;
@SerializedName("grant_type")
@Expose
private String grant_type;
@SerializedName("Culture")
@Expose
private String Culture;
public String getUserName() {
return UserName;
}
public void setUserName(String userName) {
UserName = userName;
}
public String getPassword() {
return Password;
}
public void setPassword(String password) {
Password = password;
}
public String getGrant_type() {
return grant_type;
}
public void setGrant_type(String grant_type) {
this.grant_type = grant_type;
}
public String getCulture() {
return Culture;
}
public void setCulture(String culture) {
Culture = culture;
}
}
我在活动中按如下方式使用它:
Login login = new Login();
login.setUserName("username");
login.setPassword("ab154211254");
login.setGrant_type("password");
login.setCulture("ar");
apiInstence.loginUser(login).enqueue(new Callback<TokenResponse>() {
@Override
public void onResponse(@NonNull Call<TokenResponse> call, @NonNull Response<TokenResponse> response) {
int req = response.code();
Log.d("req",req+"");
Log.d("onResponse", "onResponse: "+response.body());
}
@Override
public void onFailure(@NonNull Call<TokenResponse> call, @NonNull Throwable t) {
}
});
我也在清单中设置了Internet许可,但是没有任何作用。任何人都可以帮助我...
答案 0 :(得分:0)
请在此行中更正api接口类中的登录方法
@FormUrlEncoded
然后该方法如下所示。
@POST("token")
@FormUrlEncoded
Call<TokenResponse> loginUser(@Body Login login);
答案 1 :(得分:0)
尝试这样。
@FormUrlEncoded
@POST("token")
Call<TokenResponse> loginUser(@Field("UserName") String UserName,
@Field("Password") String Password,
@Field("grant_type") String grant_type,
@Field("Culture") String Culture);