我从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
}
}
答案 0 :(得分:0)
在您的情况下,响应为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();
}
}