I am new to android programming and I'm trying to get data from this JSON
{
"user": {
"username": "justin",
"private": false,
"name": "Justin Nemeth",
"vip": true,
"vip_ep": false,
"ids": {
"slug": "justin"
},
"gender": "male",
"age": 32,
"images": {
"avatar": {
"full":
"https://secure.gravatar.com/avatar/30c2f0dfbc39e48656f40498aa871e33?r=pg&s=256"
}
}
}
All I need to get is the "username"
and "full"
I tried using jsonObject.getString("username")
, but it did not work and I did more research and I found that I should use JSONArray
instead, but I tried it and it still did not work for me. I know I'm doing something wrong but I can't tell what it is.
Here's my code
final JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(
Request.Method.GET,
userDetailsUri,
null,
new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
try {
JSONObject mainObject = new JSONObject(response);
JSONArray resArray = mainObject.getJSONArray("user");
for (int i = 0; i < resArray.length(); i++) {
JSONObject jsonObject = resArray.getJSONObject(i);
UserDetails userDetails = new UserDetails();
userDetails.setUsername(jsonObject.getString("username"));
userDetails.setProfile_image(jsonObject.getString("full"));
}
} catch (Exception e) {
e.printStackTrace();
}
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Log.i("vkv", "getUserDetails() " + error.toString());
}
})
{
@Override
public Map<String, String> getHeaders() throws AuthFailureError {
Map<String, String> headers = new HashMap<String, String>();
headers.put("Content-Type", "application/json");
headers.put("Authorization", "Bearer " + access_token);
headers.put("trakt-api-version", "2");
headers.put("trakt-api-key", client_id);
return headers;
}
};
requestQueue.add(jsonObjectRequest);