我在使用Retrofit Android lib的POSTMAN中得到的结果不一样。也许我做错了什么。
以下是我用来调用API的代码
@Override
protected Response doInBackground(Void... params) {
Response<UserModel> response;
try {
LoginModel loginModel = new LoginModel("password", mEmail, mPassword, "Mobile", "123@abc");
Retrofit retrofit = new Retrofit.Builder()
.baseUrl(Constants.BASE_URL)
.addConverterFactory(GsonConverterFactory.create())
.build();
ServiceAPI serviceAPI = retrofit.create(ServiceAPI.class);
Call<UserModel> call = serviceAPI.getToken(loginModel);
try{
response = call.execute();
if (response.body() == null) {
Log.d(getString(R.string.app_name), response.errorBody().toString());
return response;
} else {
return response;
}
}catch ( IOException e){
Log.e(getString(R.string.app_name), e.getMessage());
}
Thread.sleep(2000);
} catch (InterruptedException e) {
return null;
}
return null;
}
接口代码
下方public interface ServiceAPI {
@POST("Token")
Call<UserModel> getToken(@Body LoginModel loginModel);
}
类LoginModel.java
public class LoginModel {
@SerializedName("grant_type")
private String grantType;
private String username;
private String password;
@SerializedName("client_id")
private String clientId;
@SerializedName("client_secret")
private String clientSecret;
public LoginModel(String grantType, String username, String password, String clientId, String clientSecret) {
this.grantType = grantType;
this.username = username;
this.password = password;
this.clientId = clientId;
this.clientSecret = clientSecret;
}
}
使用PostMan,结果会有所不同,进行令牌修正。
答案 0 :(得分:2)
更新你的api
@FormUrlEncoded
@Post("Token")
Call<UserModel> getToken(@Field("grant_type") String grantType),
@Field("username") String userName,
@Field("password") String password
// etc
);
你的api电话
Call<UserModel> call = serviceAPI.getToken("password", mEmail, mPassword, ...);