How to parse the Reddit API in Android

So, I'm trying to parse the Reddits r / hot / .json API to get a list view of the themes, but I can't get the right for JSON. I have looked everywhere and I cannot find a good example of how to do this for reddit. Here is what I still have.

JSONObject response = new JSONObject(result);
        JSONObject data = response.getJSONObject("data");
        JSONArray hotTopics = data.getJSONArray("children");

        for(int i=0; i<hotTopics.length(); i++) {
            JSONObject topic = hotTopics.getJSONObject(i);

            String author = topic.getString("author");
            String imageUrl = topic.getString("thumbnail");
            String postTime = topic.getString("created_utc");
            String rScore = topic.getString("score");
            String title = topic.getString("title");

            topicdata.add(new ListData(title, author, imageUrl, postTime, rScore));
            Log.v(DEBUG_TAG,topicdata.toString());
        }

------- Edit Ok to give more details I made an HttpGet request at " http://www.reddit.com/r/hot/.json?sort=new&count=25 " When I run my code in its current form, I get the following JSONException

07-06 22:23:11.628 2580-2580/com.google.android.gms.redditviewr.app W/System.err﹕ org.json.JSONException: No value for author 07-06 22:23:11.632 2580-2580/com.google.android.gms.redditviewr.app W/System.err﹕ at org.json.JSONObject.get(JSONObject.java:354) 07-06 22:23:11.632 2580-2580/com.google.android.gms.redditviewr.app W/System.err﹕ at org.json.JSONObject.getString(JSONObject.java:514) 07-06 22:23:11.636 2580-2580/com.google.android.gms.redditviewr.app W/System.err﹕ at Tasks.RedditApiTask.onPostExecute(RedditApiTask.java:78) 07-06 22:23:11.636 2580-2580/com.google.android.gms.redditviewr.app W/System.err﹕ at Tasks.RedditApiTask.onPostExecute(RedditApiTask.java:22) 07

Which points to the first element in my JSON parsing logic. But that makes no sense, because there really are all of these elements in the children array.

+4
2

,

result
-- data
---- children
------ data
-------- author
-------- thumbnail
-------- created_utc
-------- score
-------- title

-

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

    String author = topic.getString("author");
    String imageUrl = topic.getString("thumbnail");
    String postTime = topic.getString("created_utc");
    String rScore = topic.getString("score");
    String title = topic.getString("title");

    topicdata.add(new ListData(title, author, imageUrl, postTime, rScore));
    Log.v(DEBUG_TAG, topicdata.toString());
}
+9

, retrofit + jsonschema2pojo

rest2mobile plug-in/library Android-, . . reddit rest2mobile .

+4

Source: https://habr.com/ru/post/1547356/


All Articles