问题描述
嗨,我正在尝试将Retrofit实施到我的Android应用程序中,并且试图从我的身份验证服务器获取访问令牌,但它不会返回任何内容,而是返回一个空对象,我不确定为什么。我遵循了一些使用旧的Retrofit 1 API的代码,并尝试将其自己迁移到Retrofit 2,因此可能遗漏了一些东西。
下面是我编写的代码,任何人都看不到任何可能导致此功能不起作用的东西吗?我只是在一周初才开始使用Retrofit,所以真的很新。
OauthService.java
package com.example.helper.retrofit.oauth2.service;
import com.example.helper.retrofit.oauth2.request.Accesstoken;
import com.example.helper.retrofit.oauth2.response.AccesstokenResponse;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.http.Body;
import retrofit2.http.POST;
public interface OauthService {
@POST("/")
Call<AccesstokenResponse> fetchAccesstoken(@Body Accesstoken accesstoken);
}
AccesstokenResponse.java
package com.example.helper.retrofit.oauth2.response;
public class AccesstokenResponse extends BaseResponse {
private String access_token;
private String refresh_token;
private String expires_in;
private String token_type;
public String getAccess_token() {
return access_token;
}
public String getRefresh_token() {
return refresh_token;
}
public String getExpires_in() {
return expires_in;
}
public String getToken_type() {
return token_type;
}
@Override
public String toString() {
if (super.getError() != null && super.getError_desc() != null) {
return super.getError() + super.getError_desc();
}
return "Accesstoken{" +
"accesstoken='" + access_token + '\'' +
",tokenType='" + token_type + '\'' +
",expiresIn=" + expires_in +
",refreshToken='" + refresh_token + '\'' +
'}';
}
}
Accesstoken.java
package com.example.helper.retrofit.oauth2.request;
public class Accesstoken {
private String username;
private String password;
private String client_id;
private String client_secret;
private String grant_type;
public void setUsername(String username) {
this.username = username;
}
public void setPassword(String password) {
this.password = password;
}
public void setClient_id(String client_id) {
this.client_id = client_id;
}
public void setClient_secret(String client_secret) {
this.client_secret = client_secret;
}
public void setGrant_type(String grant_type) {
this.grant_type = grant_type;
}
}
public void getAccesstoken() {
Accesstoken accesstoken = new Accesstoken();
accesstoken.setClient_id("client");
accesstoken.setClient_secret("client");
accesstoken.setGrant_type("client_credentials");
accesstoken.setUsername(inputEmail.getText().toString());
accesstoken.setPassword(inputPassword.getText().toString());
OauthService oauthService = cOauthService.getClient().create(OauthService.class);
Call<AccesstokenResponse> call = oauthService.fetchAccesstoken(accesstoken);
call.enqueue(new Callback<AccesstokenResponse>() {
@Override
public void onResponse(Call<AccesstokenResponse> call,retrofit2.Response<AccesstokenResponse> response) {
try {
assert response.body() != null;
Log.e("Response: ",response.body().toString());
}catch (Exception e){
Log.e("Response: ","Failed!");
}
}
@Override
public void onFailure(Call<AccesstokenResponse> call,Throwable t) {
Log.e("Error: ","Error!");
}
});
}
任何帮助将不胜感激。
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)