通过改造进行Android登录身份验证

时间:2016-03-24 16:33:44

标签: android authentication retrofit retrofit2 okhttp3

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

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

网址需要看起来像 http://xxxxxxxxxxxxxxxx/token/username=smcnary%40metrostudy.com&password=xxxxxx&grant_type=password

请告诉我是否有任何其他需要帮助我完成这篇文章,因为我对Android开发和堆叠很新。

我的POST调用如下:

 @POST("token")
Call<AccessToken> getAuthToken(@Body User user);

用户模型的位置:

public class User {
@SerializedName("password")
String password;
@SerializedName("username")
String email;
String grantType;
public User(String email, String password, String grantType ) {
    this.email = email;
    this.password = password;
    this.grantType = grantType;
}}

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);
}

以下是LoginActivity中的逻辑

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) {

        }
    });

1 个答案:

答案 0 :(得分:0)

您需要将界面更改为:

@FormUrlEncoded
@POST("token")
Call<AccessToken> getAuthToken(@Field("username") String username, @Field("password") String password, @Field("grant_type") String grantType);

你的电话会是这样的:

Call<AccessToken> call = figgApiService.getAuthToken(email, password, grantType);