gpt4 book ai didi

java - 从 JSON 格式的服务器响应获取 token

转载 作者:行者123 更新时间:2023-12-01 20:16:20 25 4
gpt4 key购买 nike

我通过 Retrofit 的帮助向服务器发送了一个发布请求,但我想在服务器发回响应时从 token 中获取一个值。这就是我发送帖子请求和 onSuccess 我想接收 token 的方式。我的问题是我不知道从服务器响应中检索 token 。请在此提供帮助。

public void loginUser(String userName, String userPassword, Boolean userRememberMe) {

mAPIService.loginUser(userName, userPassword, userRememberMe).enqueue(new Callback<Login>() {
@Override
public void onResponse(Response<Login> response, Retrofit retrofit) {
if(response.isSuccess()) {
//save token here
Intent intent = new Intent(LoginActivity.this, ProfileActivity.class);
startActivity(intent);
}
}

@Override
public void onFailure(Throwable throwable) {
Log.e(TAG, "Unable to Login");
}
});
}

这是我从服务器得到的响应

{
"total": 0,
"data": {
"id": "348680c7-d46b-4adc-9be0-89a1d1a38566",
"username": "0242770336",
"name": "name",
"phoneNumber": "063546345",
"email": "number@gmail.com",
"dateOfBirth": null,
"image": null,
"gender": "",
"idTypeId": null,
"idType": null,
"idNumber": null,
"idExpiryDate": null,
"idVerified": false,
"cityId": null,
"city": null,
"residentialAddress": null,
"latitude": 0,
"longitude": 0,
"createdAt": "2017-08-14T17:45:16.24Z",
"role": {
"id": 2,
"name": "User",
"privileges": [
""
]
},
"token": "jNK_DGszYOMEpPOFoRGjuyJ5KX9kaJQKCl3cujlHoXklCS8Ij6b-QBmhv0jVwVC54KcNXkzyM62xpswqcjo9Ajf-n-rWzSaIoiYNglaXhtPspziZ0PcTKzTMAvw8si3A7BlcD98M-IjIxYjxieVYAPWVtcvomWi"
},

"message": "Login Successfull",
"success": true
}

这是我的登录模型

public class Login {

@SerializedName("userName")
@Expose
private String userName;
@SerializedName("password")
@Expose
private String password;
@SerializedName("rememberMe")
@Expose
private Boolean rememberMe;

public String getUserName() {
return userName;
}

public void setUserName(String userName) {
this.userName = userName;
}

public String getPassword() {
return password;
}

public void setPassword(String password) {
this.password = password;
}

public Boolean getRememberMe() {
return rememberMe;
}

public void setRememberMe(Boolean rememberMe) {
this.rememberMe = rememberMe;
}

}

最佳答案

有两种方法可以实现这一点。

方法1:使用JsonObject

mAPIService.loginUser(userName, userPassword, userRememberMe).enqueue(new Callback<JsonObject>() {
@Override
public void onResponse(Response<JsonObject> response, Retrofit retrofit) {
if(response.isSuccess()) {
try {

String token=response.body().get("data").get("token");
} catch (JSONException e) {
e.printStackTrace();
}
Intent intent = new Intent(LoginActivity.this,ProfileActivity.class);
startActivity(intent);
}
}
}

方法2:您可以使用http://pojo.sodhanalibrary.com/将您的响应转换为 POJO。现在将其添加到您的项目中,让我们将其命名为 ResponseData.class 并通过执行以下更改来使用 getter 方法获取 token

mAPIService.loginUser(userName, userPassword, userRememberMe).enqueue(new Callback<ResponseData>() {
@Override
public void onResponse(Response<ResponseData> response, Retrofit retrofit) {
if(response.isSuccess()) {
//save token here
String token =response.getData().getToken();

Intent intent = new Intent(LoginActivity.this, ProfileActivity.class);
startActivity(intent);
}
}

@Override
public void onFailure(Throwable throwable) {
Log.e(TAG, "Unable to Login");
}
});

关于java - 从 JSON 格式的服务器响应获取 token ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45731299/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com