gpt4 book ai didi

java - 我如何从服务器获取和设置android中的API(从服务器获取int值)?如何绑定(bind)和实现

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

我收到来自 API 角色 ID 的响应(以 int 值表示)。我如何在我的 android 项目中获取并设置这个角色 id,以便我可以在全局范围内使用它。

TextView  profile_fullName;
TextView profile_roleName;

int inspect=18;
int contract=19;

profile_fullName=(TextView)findViewById(R.id.profile_fullName);
profile_roleName=(TextView)findViewById(R.id.profile_role);

这是 API 的响应

{
"success": true,
"message": {
"UserName": "ABC",
"ComanyName": "ABC XYZ COMPANY Ltd",
"Email": "abc.xyz@gmail.com",
"FullName": "ABC",
"RoleName": "logged in as Engineer",
"RoleId": 19
}
}

最佳答案

在您的例子中,响应是一个JSONObject。因此通过 JSONObject 提取响应。

在你的onResponse()

public void onResponse(JSONObject response) {
try {
if (String.valueOf(response.get("success")).equalsIgnoreCase("true")) { //this is just an additional validation
JSONObject jsonObjectInner = new JSONObject(String.valueOf("message"));
profile_fullName.setText(jsonObjectInner.get("FullName"));
profile__roleName.setText(jsonObjectInner.get("RoleName"));

}else{
//add validation if success column is != true
}
} catch (Throwable e) {
e.printStackTrace();
}
}

关于java - 我如何从服务器获取和设置android中的API(从服务器获取int值)?如何绑定(bind)和实现,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56894391/

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