-2

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);
tony
  • 466
  • 6
  • 22
  • Possible duplicate of [How do I parse JSON in Android?](https://stackoverflow.com/questions/9605913/how-do-i-parse-json-in-android) – Basi Apr 11 '19 at 14:02
  • 1
    Well, for started you are not using `response`, which contains the server response. Secondly, there are no arrays in the JSON you posted, just objects. – Leonardo Velozo Apr 11 '19 at 14:04
  • Sorry..the `response` was included in the original code. I just had to re-type it because I was trying other different codes. But how do I get the data then? – tony Apr 11 '19 at 14:08

2 Answers2

1

user is JsonObject not JsonArray so you can get name and full like below

       JSONObject mainObject = new JSONObject();

        JSONObject jobjUser =mainObject.getJSONObject("user");

        String username=jobjUser.getString("username");

        JSONObject jobjImages=mainObject.getJSONObject("images");
        JSONObject jobjAvatar=jobjImages.getJSONObject("avatar");
        String full=jobjAvatar.getString("full");
Dhaiyur
  • 498
  • 4
  • 14
  • @tony please check and let me know if you required anything – Dhaiyur Apr 11 '19 at 14:14
  • It doesn't work. Thank you very much for the help though – tony Apr 11 '19 at 14:21
  • it gives me this error `com.android.volley.AuthFailureError` but it doesn't give me this error if I just Log `response.toString`, instead it gives me the JSON results. – tony Apr 11 '19 at 14:25
0

Although I still had errors after using the answer above, I used it as a basis to fix my issue. Here's the code

JSONObject mainObject = new JSONObject(response.toString());
                        JSONObject jsonObject = mainObject.getJSONObject("user");
                        JSONObject object = jsonObject.getJSONObject("images");
                        JSONObject jsonObject1 = object.getJSONObject("avatar");

                        String image = jsonObject1.getString("full");
                        String username = jsonObject.getString("username");
tony
  • 466
  • 6
  • 22