Ответ модификации Swagger 2 - 200, но не может получить токен из тела ответа - PullRequest
0 голосов
/ 05 июня 2018

Я пытаюсь соединиться и получить токен с Retrofit 2 от службы swagger restful, я получаю ответ 200, но не могу получить токен из тела ответа

private void login() {
        Call<User> call=userClient.login("user","123","password");

        call.enqueue(new Callback<User>() {
            @Override
            public void onResponse(Call<User> call, Response<User> response) {

                if(response.isSuccessful()){
                    token=response.body().getToken();
                    Toast.makeText(LoginActivity.this,token,Toast.LENGTH_SHORT).show();


                }else{
                    Toast.makeText(LoginActivity.this,"login not correct",Toast.LENGTH_SHORT).show();
                }

            }

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

                Toast.makeText(LoginActivity.this,"error",Toast.LENGTH_SHORT).show();

            }
        });
    }

1 Ответ

0 голосов
/ 06 июня 2018

Создайте класс User как этот

 public class User {

@SerializedName("access_token")
@Expose
private String accessToken;
@SerializedName("token_type")
@Expose
private String tokenType;
@SerializedName("expires_in")
@Expose
private Integer expiresIn;

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

}

, а затем попробуйте вот так

token=response.body().getAccessToken();

Ваш экземпляр модификации должен быть похож на

Retrofit retrofit = new Retrofit.Builder()
            .baseUrl(Constants.your_baseUrl)
            .addConverterFactory(GsonConverterFactory.create())
            .build();
...