2

I'm trying to read this json string on Android but the field "thumbnail" always returns empty even though it never actually is empty, it should return either a url or "self".

{
    "kind": "t3",
    "data": {
      "domain": "twitter.com",
      "banned_by": null,
      "media_embed": {},
      "subreddit": "nba",
      "id": "3y6vzw",
      "author": "Higgnkfe",
      "media": null,
      "score": 883,
      "approved_by": null,
      "over_18": false,
      "hidden": false,
      "num_comments": 811,
      "thumbnail": "http://b.thumbs.redditmedia.com/rfBhblr-DJIfsCEwQ9jq-wy_mHcmmedx3_Xy60STumc.jpg",
      "subreddit_id": "t5_2qo4s",
      "stickied": false,
      "from": null,
      "is_self": false,
      "from_id": null,
      "permalink": "/r/nba/comments/3y6vzw/first_all_star_results_are_in_kobe_steph_lead_the/",
      "created": 1451088664.0,
      "url": "https://twitter.com/nbaallstar/status/680417825463365632",
      "title": "First All Star Results are in; Kobe, Steph lead the vote",
      "created_utc": 1451059864.0,
      "ups": 883
    }  

Here's a piece of code i'm using to read it:

        String title, author, subreddit, id, thumbnail = "test", url;
        int score, numOfComments;
        double created;
        boolean isSelf;

        JSONArray arr = new JSONObject(raw).getJSONObject("data").getJSONArray("children");

        for (int i = 0; i < arr.length(); i++) {
            JSONObject data = arr.getJSONObject(i).getJSONObject("data");

            Log.d("LOADER", "JSON: " + data.toString());

            title = data.getString("title");
            author = data.getString("author");
            subreddit = data.getString("subreddit");
            id = data.getString("id");
            thumbnail = data.getString("thumbnail");
            score = data.getInt("score");
            Log.d("LOADER", " thumb: " + thumbnail); // log shows "thumb:" only
            url = data.getString("url");
            created = data.getDouble("created");
            isSelf = data.getBoolean("is_self");
            numOfComments = data.getInt("num_comments");

            Post post = new Post(subreddit, title, author, url, id, score, numOfComments,
                     thumbnail, created, isSelf);

            posts.add(post);
        }  

I have no problem reading any other field, even the "url" field which also contains a url, so it can't be that...

This is what the log shows for data.toString():

JSON: {"domain":"self.nba","banned_by":null,.....(etc etc etc).......,"id":"3y46dt","num_comments":787,"thumbnail":"","subreddit_id":"t5_2qo4s","is_self":true,"from_id":null,"url":"https:\/\/www.reddit.com\/r\/nba\/comments\/3y46dt\/trash_talk_thread_no_games_today_because_of\/","author_flair_text":"[TOR] Luis Scola"....etc  

As you can see, "thumbnail" has a value of "", but "url" does have a url.

What's going on here?

Here's the url where i'm getting the json string from in case it's useful http://www.reddit.com/r/nba/.json

Jorge Gil
  • 2,805
  • 1
  • 15
  • 22

1 Answers1

2

enter image description hereI checked it myself, nothing is wrong with your code. The fact is the json which you are getting contains empty value for thumbnail. I had gone to the link which you provided and starting 3 arrays contains empty value for thumbnail. So your code is correct it has nothing to do with the value of thumbnail whether it's a url or some other value.

Abhishek Singh
  • 275
  • 1
  • 2
  • 18
  • What browser are you using? When i open it with Chrome it's either "self" or a url. I checked with IE and the downloaded file shows an empty value though. I guess the same is happening with Android. – Jorge Gil Dec 25 '15 at 23:34
  • @JorgeGil I used chrome and then https://jsonformatter.curiousconcept.com/ to format the json to see it clearly. – Abhishek Singh Dec 25 '15 at 23:38
  • If i use Chrome and copy&paste the output to the website you posted i get a value for "thumbail", but if i just paste the url directly to the website i get no value, i have no idea of what's happening... – Jorge Gil Dec 25 '15 at 23:46
  • @JorgeGil I just checked again, it is empty but there are some keys such as **"thumbnail_url"** which have value "https://i.embed.ly/1/image?url=https%3A%2F%2Fi.ytimg.com%2Fvi%2FzrIMXZST3go%2Fhqdefault.jpg&key=b1e305db91cf4aa5a86b732cc9fffce". Are you sure you are not confusing thumbnail with **"thumbnail_url".** – Abhishek Singh Dec 25 '15 at 23:57
  • No, thumbnail_url is used only on certain cases (such as youtube links) and is independent of "thumbnail", "thumbnail" is always used and it is ALWAYS either "self" (to indicate there's no thumbnail) or the thumbnail url. I also checked with mozilla and i'm getting empty strings like you are, but with chrome i'm still getting the correct values. – Jorge Gil Dec 26 '15 at 00:01
  • @JorgeGil Have you tried to meddle with your HTTP request headers, e.g. `Accept` or `User-Agent`, and determine whether reddit responses differently on how it fills the `thumbnail` value? Reference for changing user agent in Android app: http://stackoverflow.com/a/5669214/1519522 – aff Dec 26 '15 at 01:13
  • 1
    Turns out we were both right...sort of. Apparently, subreddit moderators (/r/NBA in this case) have the ability to disable thumbnails when a user is not logged in. Which is the reason i was getting values (i was logged in) and you weren't (you weren't logged in). – Jorge Gil Dec 26 '15 at 16:29