无法使用 POJO class 获取令牌并保存到字符串中

Unable to use POJO class to get the token and save into a string

我从 API 获取数据,然后将其转换为字符串,以便我可以使用我的 POJO classes(用户和令牌)将数据保存到 sharedPref .我可以使用 User class 的方法,但每当我尝试访问 class Token 的方法时,应用程序就会崩溃。 这是我收到的回复:

{
"username": "string",
"email": "string",
"firstName": "string",
"lastName": "string",
"avatarURL": "string",
"token": {
  "token": "string",
  "expiresOn": "2019-06-29T21:07:07.891Z"
}}

这是我的用户 Class:

public class User {
public User() {
}

@SerializedName("username")
@Expose
private String username;
@SerializedName("email")
@Expose
private String email;
@SerializedName("firstName")
@Expose
private String firstName;
@SerializedName("lastName")
@Expose
private String lastName;
@SerializedName("avatarURL")
@Expose
private String avatarURL;
@SerializedName("token")
@Expose
private Token token;

public String getUsername() {
    return username;
}

public void setUsername(String username) {
    this.username = username;
}

public String getEmail() {
    return email;
}

public void setEmail(String email) {
    this.email = email;
}

public String getFirstName() {
    return firstName;
}

public void setFirstName(String firstName) {
    this.firstName = firstName;
}

public String getLastName() {
    return lastName;
}

public void setLastName(String lastName) {
    this.lastName = lastName;
}

public String getAvatarURL() {
    return avatarURL;
}

public void setAvatarURL(String avatarURL) {
    this.avatarURL = avatarURL;
}

public Token getToken() {
    return token;
}

public void setToken(Token token) {
    this.token = token;
}}

这是我的令牌Class:

public class Token {
public Token() {
}

@SerializedName("token")
@Expose
private String token;
@SerializedName("expiresOn")
@Expose
private String expiresOn;

public String getToken() {
    return token;
}

public void setToken(String token) {
    this.token = token;
}

public String getExpiresOn() {
    return expiresOn;
}

public void setExpiresOn(String expiresOn) {
    this.expiresOn = expiresOn;
}}

我请求的函数:

public void logInRequest(final String userName, String userPassword) {
    final JSONObject jsonObject = new JSONObject();
    try {

        jsonObject.put("identifier", userName);
        jsonObject.put("password", userPassword);

    } catch (JSONException e) {
        e.printStackTrace();
    }
    APIService apiService = RetrofitClient.getAPIService();
    Call<String> logInResponse = apiService.logIn(jsonObject.toString());
    logInResponse.enqueue(new Callback<String>() {
        @Override
        public void onResponse(Call<String> call, Response<String> response) {
            if (response.message().equals("OK")) {
                String data = response.body();

                Gson g = new Gson();
                Gson g1 = new Gson();
                User user = g.fromJson(data, User.class);
                Token token=g1.fromJson(data,Token.class);

                String tokenNo=token.getToken();
                String username = user.getUsername();
                String email = user.getEmail();
                String firstName = user.getFirstName();
                String lastName = user.getLastName();

                sharedPrefs.saveUserName(username);
                sharedPrefs.saveEmail(email);
                sharedPrefs.saveFullName(firstName, lastName);

                context.startActivity(new Intent(context, HomeActivity.class));
            } else {
                Toast.makeText(getApplication(), "Wrong User Name or Password", Toast.LENGTH_SHORT).show();
            }


        }

        @Override
        public void onFailure(Call<String> call, Throwable t) {
            Toast.makeText(getApplication(), "Something went wrong please try again", Toast.LENGTH_SHORT).show();

        }
    });

}

我使用了内置调试器,应用程序在这一行崩溃了

Token token=g1.fromJson(data,Token.class);

您正在尝试使用 2 个不同的对象来解析响应,但响应已被解析。如果您调试 User 对象,它内部已经包含了 Token 对象。这行就够了:

User user = g.fromJson(data, User.class);

发生崩溃是因为您试图将 User JSON 对象(您的 data 对象)解析为 Token 对象。所以只需删除这一行:

Token token=g1.fromJson(data,Token.class);

User user = g.fromJson(data, User.class);

// Try this to get the token of a particular user.
// I think this may be the cause of the error.
Token token = user.getToken();

String tokenNo = token.getToken();