【发布时间】:2016-03-23 12:55:03
【问题描述】:
我需要从 Retrofit 解析 JSON 数组。我需要得到以下密钥:
{
"rc":0,
"message":"success",
"he":[
{
"name":"\u05de\u05e4\u05e7\u05d7",
"type":0
}
]
}
我可以轻松获取消息,但无法从响应中获取“he”数组。
这是我的数据模型类
public class GetRoleData implements Serializable {
@SerializedName("he")
private ArrayList<Roles> he;
@SerializedName("message")
private String message;
public GetRoleData() {
this.he = new ArrayList<>();
this.message = "";
}
public ArrayList<Roles> getUserRoles() {
return he;
}
public String getMessage() {
return message;
}
public class Roles {
public Roles() {
name = "";
type = -1;
}
@SerializedName("name")
private String name;
@SerializedName("type")
private int type;
public int getType() {
return type;
}
public String getName() {
return name;
}
}
}
这就是我向服务器发送请求的方式:
@POST("index.php/")
Call<GetRoleData> getUserRoles(@Body SetParams body);
这是我发送请求和处理响应的方式
APIService apiService = retrofit.create(APIService.class);
Call<GetRoleData > apiCall = apiService.getUserRoles(params);
apiCall.enqueue(new Callback<GetRoleData >() {
@Override
public void onResponse(retrofit.Response<GetRoleData > mUserProfileData, Retrofit retrofit) {
Log.e("locale info", "mUserProfileData = " + mUserProfileData.body().toString());
if (pDialog != null) {
pDialog.dismiss();
}
if (mUserProfileData.body().getMessage().equals("success")) {
Log.e("locale info", "user roles = " + mUserProfileData.body().getUserRoles().size());
} else {
Toast.makeText(RegisterActivity.this, getResources().getString(R.string.get_role_error), Toast.LENGTH_SHORT).show();
}
}
@Override
public void onFailure(Throwable t) {
if (pDialog != null) {
pDialog.dismiss();
}
t.printStackTrace();
}
});
我想要什么
我需要从上面的响应中获取“he”数组。请帮忙谢谢。
这是我得到的回应..
【问题讨论】:
-
请将代码粘贴到您发出请求并处理响应的位置
-
@vipinagrahari 请检查。
-
I am not able to get "he" array from response到底是什么意思? -
你检查了我需要解析的响应吗?我需要从响应中获取 jsonArray,即“他”@Yazan
-
@Yazan 请检查更新的问题。我想获取“他”中的数据