Android 安卓登录认证与改进

Android 安卓登录认证与改进,android,authentication,retrofit,retrofit2,okhttp3,Android,Authentication,Retrofit,Retrofit2,Okhttp3,我有一个LoginActivity,它获取用户名和密码。我正在尝试使用改造后2和OkHTTP接收身份验证令牌。我已经创建了这个服务,它构建了改造适配器以及OkHttp客户端。我有一个界面,但这是我的问题开始 我必须在FormEncodedURL中将我的凭证(登录时的用户名和密码)发布到后端以接收令牌,我有一个名为AccessToken的POJO。我真的不确定我是否正确地使用了回调方法等等 URL需要看起来像 请让我知道,如果有任何其他需要帮助我与这篇文章,因为我是相当新的android开发和应

我有一个LoginActivity,它获取用户名和密码。我正在尝试使用改造后2和OkHTTP接收身份验证令牌。我已经创建了这个服务,它构建了改造适配器以及OkHttp客户端。我有一个界面,但这是我的问题开始

我必须在FormEncodedURL中将我的凭证(登录时的用户名和密码)发布到后端以接收令牌,我有一个名为AccessToken的POJO。我真的不确定我是否正确地使用了回调方法等等

URL需要看起来像

请让我知道,如果有任何其他需要帮助我与这篇文章,因为我是相当新的android开发和应用程序

我的发帖电话如下所示:

 @POST("token")
Call<AccessToken> getAuthToken(@Body User user);
AccessToken模型为:

public class AccessToken {

private String accessToken;
private String tokenType;
private Integer expiresIn;
private String name;
private String title;
private String picImageBase64String;
private String Issued;
private String Expires;
private Map<String, Object> additionalProperties = new HashMap<String, Object>();

public String getAccessToken() {
    return accessToken;
}

public void setAccessToken(String accessToken) {
    this.accessToken = accessToken;
}

public String getTokenType() {
    return tokenType;
}

public void setTokenType(String tokenType) {
    this.tokenType = tokenType;
}

public Integer getExpiresIn() {
    return expiresIn;
}

public void setExpiresIn(Integer expiresIn) {
    this.expiresIn = expiresIn;
}

public String getName() {
    return name;
}

public void setName(String name) {
    this.name = name;
}

public String getTitle() {
    return title;
}

public void setTitle(String title) {
    this.title = title;
}

public String getPicImageBase64String() {
    return picImageBase64String;
}

public void setPicImageBase64String(String picImageBase64String) {
    this.picImageBase64String = picImageBase64String;
}

public String getIssued() {
    return Issued;
}

public void setIssued(String Issued) {
    this.Issued = Issued;
}

public String getExpires() {
    return Expires;
}

public void setExpires(String Expires) {
    this.Expires = Expires;
}

public Map<String, Object> getAdditionalProperties() {
    return this.additionalProperties;
}

public void setAdditionalProperty(String name, Object value) {
    this.additionalProperties.put(name, value);
}
公共类AccessToken{
私有字符串访问令牌;
私有字符串类型;
私有整数expiresIn;
私有字符串名称;
私有字符串标题;
私有字符串picImageBase64String;
发行私人字符串;
私有字符串过期;
私有映射additionalProperties=new HashMap();
公共字符串getAccessToken(){
返回accessToken;
}
公共无效setAccessToken(字符串accessToken){
this.accessToken=accessToken;
}
公共字符串getTokenType(){
返回令牌类型;
}
公共void setTokenType(字符串tokenType){
this.tokenType=tokenType;
}
公共整数getExpiresIn(){
返回过期蛋白;
}
public void setExpiresIn(整数expiresIn){
this.expiresIn=expiresIn;
}
公共字符串getName(){
返回名称;
}
公共void集合名(字符串名){
this.name=名称;
}
公共字符串getTitle(){
返回标题;
}
公共无效集合标题(字符串标题){
this.title=标题;
}
公共字符串getPicImageBase64String(){
返回picImageBase64String;
}
public void setPicImageBase64String(字符串picImageBase64String){
this.picImageBase64String=picImageBase64String;
}
公共字符串getIssued(){
发出的报税表;
}
已发布公共无效设置(已发布字符串){
这个.发布的=发布的;
}
公共字符串getExpires(){
返回到期;
}
public void setExpires(字符串过期){
this.Expires=Expires;
}
公共映射getAdditionalProperties(){
返回此。附加属性;
}
public void setAdditionalProperty(字符串名称、对象值){
this.additionalProperties.put(名称、值);
}
这是逻辑活动中的逻辑

public void login() {
    Log.d(TAG, "Login");

    if (!validate()) {
        onLoginFailed();
        return;
    }

    _loginButton.setEnabled(false);

    final ProgressDialog progressDialog = new ProgressDialog(LoginActivity.this,
            R.style.AppTheme);
    progressDialog.setIndeterminate(true);
    progressDialog.setMessage("Authenticating...");
    progressDialog.show();

    String email = _emailText.getText().toString();
    String password = _passwordText.getText().toString();
    String grantType = "grant_type=password";
    User user = new User(email, password, grantType);
    Call<AccessToken> call = figgApiService.getAuthToken(user);
    call.enqueue(new Callback<AccessToken>() {
        @Override
        public void onResponse(Call<AccessToken> call, Response<AccessToken> response) {
            int statusCode = response.code();
            AccessToken accesstoken = response.body();
        }

        @Override
        public void onFailure(Call<AccessToken> call, Throwable t) {

        }
    });
public void login(){
Log.d(标记“登录”);
如果(!validate()){
onLoginFailed();
返回;
}
_loginButton.setEnabled(false);
final ProgressDialog ProgressDialog=新建ProgressDialog(LoginActivity.this,
R.style.AppTheme);
progressDialog.setUndeterminate(true);
progressDialog.setMessage(“身份验证…”);
progressDialog.show();
字符串email=_emailText.getText().toString();
字符串密码=_passwordText.getText().toString();
字符串grantType=“grant\u type=password”;
用户=新用户(电子邮件、密码、grantType);
Call Call=figgApiService.getAuthToken(用户);
call.enqueue(新回调(){
@凌驾
公共void onResponse(调用、响应){
int statusCode=response.code();
AccessToken AccessToken=response.body();
}
@凌驾
失败时公共无效(调用调用,可丢弃的t){
}
});

您需要将界面更改为:

@FormUrlEncoded
@POST("token")
Call<AccessToken> getAuthToken(@Field("username") String username, @Field("password") String password, @Field("grant_type") String grantType);
@FormUrlEncoded
@邮政(“代币”)
调用getAuthToken(@Field(“username”)String username、@Field(“password”)String password、@Field(“grant_type”)String grantType);
你的电话是这样的:

Call<AccessToken> call = figgApiService.getAuthToken(email, password, grantType);
Call Call=figgApiService.getAuthToken(电子邮件、密码、grantType);

试试看,在onResponse中设置一个断点,如果它一直没有到达,那么它就会转到onFailure。为什么你的URL会这样传递密码,你应该把它放在post数据的主体中
Call<AccessToken> call = figgApiService.getAuthToken(email, password, grantType);