在Khan Academy

时间:2017-05-10 08:05:41

标签: android json

我正在开发一个Android入学应用,我正尝试通过API与我的应用建立一个Khan连接,其基本搜索功能可以让用户输入关键字,应用会显示主题/视频列表/练习符合搜索条件,但我发现它几乎不可能,因为API没有提供官方的“按关键字搜索”方法,并且任何与Khan合作的人都知道无法下载完整的主题树,因为它结束于一个70mb的文件。

经过了多天艰苦的研究并苦苦挣扎,但仍无法找到任何关于如何处理此问题的文件(如果有的话),所以我想求助。有没有人知道如何处理这个或成功完成它的任何人可以给我一些关于哪个方向的暗示?

我知道你可以做http://www.khanacademy.org/api/v1/topictree?kind=topic(例如)但是即使使用这个过滤器,json结果也很大(超过10mb的数据流)。

这是我流式传输json的方式:

private static String connect2Url(String urlString){

        URL url;
        HttpURLConnection urlConnection = null;
        InputStream inStream = null;
        StringBuilder response = new StringBuilder();

        try {
            url = new URL(urlString);
            urlConnection = (HttpURLConnection) url.openConnection();
            urlConnection.setRequestMethod("GET");
            urlConnection.setDoInput(true);
            urlConnection.connect();
            inStream = urlConnection.getInputStream();
            BufferedReader bReader = new BufferedReader(new InputStreamReader(inStream));
            String temp = "";
            while ((temp = bReader.readLine()) != null) {
                response.append(temp);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (inStream != null) {
                try {
                    inStream.close();
                } catch (IOException ignored) {
                }
            }
            if (urlConnection != null) {
                urlConnection.disconnect();
            }
        }

        return response.toString();
    }

然后我用这个结果创建了JSONObject。

目前我正在传递“http://www.khanacademy.org/api/v1/topic/%s”作为网址,但有了这个,您只能按ID(主题slug)找到主题,但无法进行免费搜索。

好的,我希望有人可以提供帮助,并提前感谢。

编辑:

根据AlphaQ的建议,我已经在调查有关GSON和Jackson的事情,当然我同意他的解析功能对于大型json流无效,但无论如何我遇到了两个问题,第一个是json结构返回Khan Academy创建它的类是非常复杂的(我认为),第二个是即使我可以进行更有效的解析,我能够过滤Khan Academy json从服务器响应的结果是14mb的数据(完整的树是70mb)我想这对于移动应用程序来说仍然太多,无论我的功能如何优化,因为它无论如何都必须下载14mb并且Khan Academy API不提供“按关键字搜索”方法。

给我你的意见,难道你不认为14mb仍然有太多的json数据要流式传输给移动应用吗?

这是json响应的一个非常小的摘录作为一个例子:

{
    "icon_src": "",
    "twitter_url": "",
    "domain_slug": null,
    "relative_url": "/",
    "creation_date": "2016-12-14T00:46:38Z",
    "web_url": "",
    "ka_url": "https://www.khanacademy.org/",
    "translated_standalone_title": "Khan Academy",
    "has_user_authored_content_types": true,
    "translated_title": "Khan Academy",
    "gplus_url": "",
    "children": [{
            "icon_src": "",
            "twitter_url": "",
            "domain_slug": "new-and-noteworthy",
            "relative_url": "/new-and-noteworthy",
            "creation_date": "2016-09-13T20:35:19Z",
            "web_url": "",
            "ka_url": "https://www.khanacademy.org/new-and-noteworthy",
            "translated_standalone_title": "New and noteworthy",
            "has_user_authored_content_types": false,
            "translated_title": "New and noteworthy",
            "gplus_url": "",
            "children": [],
            "hide": false,
            "node_slug": "new-and-noteworthy",
            "title": "New and noteworthy",
            "child_data": [{
                    "kind": "Video",
                    "id": "1101921746"
                }, {
                    "kind": "Video",
                    "id": "796509722"
                }, {
                    "kind": "Video",
                    "id": "796388970"
                }, {
                    "kind": "Video",
                    "id": "753767269"
                }, {
                    "kind": "Video",
                    "id": "225139731"
                }, {
                    "kind": "Video",
                    "id": "225139729"
                }, {
                    "kind": "Video",
                    "id": "988721865"
                }, {
                    "kind": "Video",
                    "id": "884313452"
                }, {
                    "kind": "Video",
                    "id": "671536496"
                }, {
                    "kind": "Video",
                    "id": "887553535"
                }, {
                    "kind": "Video",
                    "id": "1049279550"
                }, {
                    "kind": "Video",
                    "id": "796535894"
                }, {
                    "kind": "Video",
                    "id": "796307891"
                }, {
                    "kind": "Video",
                    "id": "1094139389"
                }, {
                    "kind": "Video",
                    "id": "796502981"
                }, {
                    "kind": "Video",
                    "id": "796434657"
                }, {
                    "kind": "Video",
                    "id": "871510490"
                }, {
                    "kind": "Video",
                    "id": "884392300"
                }, {
                    "kind": "Video",
                    "id": "x77e83a17"
                }, {
                    "kind": "Video",
                    "id": "884395195"
                }, {
                    "kind": "Video",
                    "id": "1077533640"
                }, {
                    "kind": "Video",
                    "id": "1094227108"
                }, {
                    "kind": "Video",
                    "id": "1094119956"
                }, {
                    "kind": "Video",
                    "id": "1112847811"
                }, {
                    "kind": "Video",
                    "id": "1114711148"
                }, {
                    "kind": "Video",
                    "id": "1081957510"
                }, {
                    "kind": "Video",
                    "id": "1095856196"
                }, {
                    "kind": "Video",
                    "id": "1090948774"
                }, {
                    "kind": "Video",
                    "id": "1013985489"
                }, {
                    "kind": "Video",
                    "id": "xca214ba1"
                }
            ],
            "user_authored_content_types_info": [],
            "id": "x29232c6b",
            "user_authored_content_types": [],
            "translated_description": "",
            "alternate_slugs": [],
            "standalone_title": "New and noteworthy",
            "logo_image_url": "",
            "in_knowledge_map": false,
            "description": "",
            "tags": [],
            "deleted": false,
            "listed_locales": [],
            "facebook_url": "",
            "render_type": "UncuratedTutorial",
            "background_image_url": "",
            "background_image_caption": "",
            "has_peer_reviewed_projects": false,
            "topic_page_url": "/new-and-noteworthy",
            "extended_slug": "new-and-noteworthy",
            "deleted_mod_time": "2013-07-13T00:03:09Z",
            "kind": "Topic",
            "curation": {},
            "slug": "new-and-noteworthy",
            "do_not_publish": false,
            "sha": "a6c251bd225b9d23e1a98f1a7fce3ccd0c8cb4fa",
            "branding_image_url": "",
            "current_revision_key": "a6c251bd225b9d23e1a98f1a7fce3ccd0c8cb4fa",
            "content_id": "x29232c6b",
            "content_kind": "Topic",
            "curriculum_key": ""
        }, {
            "icon_src": "",
            "twitter_url": "",
            "domain_slug": "math",
            "relative_url": "/math",
            "creation_date": "2017-05-08T20:15:34Z",
            "web_url": "",
            "ka_url": "https://www.khanacademy.org/math",
            "translated_standalone_title": "Math",
            "has_user_authored_content_types": false,
            "translated_title": "Math",
            "gplus_url": "",
            "children": [{
                    "icon_src": "",
                    "twitter_url": "",
                    "domain_slug": "math",
                    "relative_url": "/math/k-8-grades",
                    "creation_date": "2017-03-17T23:26:26Z",
                    "web_url": "",
                    "ka_url": "https://www.khanacademy.org/math/k-8-grades",
                    "translated_standalone_title": "K-8th grades",
                    "has_user_authored_content_types": false,
                    "translated_title": "K-8th grades",
                    "gplus_url": "",
                    "children": [],
                    "hide": false,
                    "node_slug": "k-8-grades",
                    "title": "K-8th grades",
                    "child_data": [],
                    "user_authored_content_types_info": [],
                    "id": "xf3cb93f0",
                    "user_authored_content_types": [],
                    "translated_description": "",
                    "alternate_slugs": [],
                    "standalone_title": "K-8th grades",
                    "logo_image_url": "",
                    "in_knowledge_map": false,
                    "description": "",
                    "tags": [],
                    "deleted": false,
                    "listed_locales": ["fr", "en", "pt", "de", "tr", "pl", "nb", "es"],
                    "facebook_url": "",
                    "render_type": "Subject",
                    "background_image_url": "",
                    "background_image_caption": "",
                    "has_peer_reviewed_projects": false,
                    "topic_page_url": "/math/k-8-grades",
                    "extended_slug": "math/k-8-grades",
                    "deleted_mod_time": null,
                    "kind": "Topic",
                    "curation": {
                        "modules": [{
                                "content": ["Video:xd2b0fcb4", "Article:x175a9d66", "Video:x102d9798", "Exercise:2015", "Exercise:3022"],
                                "referrer": "k_8_grades_staff_picks",
                                "kind": "ContentCarousel",
                                "title": "Staff picks"
                            }, {
                                "topic": "Topic:xd0ae8a03",
                                "kind": "SubjectIntro"
                            }
                        ],
                        "whitelist": ["Topic:xfe881476a971a3cb", "Topic:xb5feb28c", "Topic:xcef32ab6", "Topic:xef8f4cb4", "Topic:x3184e0ec", "Topic:xc7f617f2", "Topic:x5ec3eb59", "Topic:xb830458a", "Topic:x0267d782", "Topic:x6b17ba59", "Topic:x7c7044d7", "Topic:xa617314f", "Topic:x8708676b", "Topic:x7ed4701d", "Topic:x6ee1f3c2", "Topic:x0f2eb71b", "Topic:xa18e5391"],
                        "hide_community_questions": true
                    },

编辑2:

我正在阅读AlphaQ建议的所有GSON文档,并查看了一些示例,但我的json响应有点复杂,我无法弄清楚如何定义映射类。(/ p>

以下是我收到json的网址。

非常感谢有关如何正确定义映射类的任何帮助。

http://www.khanacademy.org/api/v1/topictree

(这是没有任何过滤但我可以添加?kind =主题以查看完整的主题列表,但不允许更多过滤器)

以上是一个70mb的流,所以我认为即使使用GSON也不可能,但对我来说,如果我能做到这一点就足够了

http://www.khanacademy.org/api/v1/topic/math

从这个巨大的文件中检索所有数学主题。

如你所见,json太大而且太复杂,所以我无法弄清楚如何为此创建映射类。我只需要位于“子节点”下的主题的网址,标题和描述。

2 个答案:

答案 0 :(得分:0)

我建议你像我在评论中所说的那样使用GSONJackson

您可以使用Jackson中的hybrid streaming approach来有效地解析流中所需的数据。

这是使用GSON的example

以下是谷歌人员所说的信息:

  

反序列化超过25MB的字符串没有任何问题

更多信息here

答案 1 :(得分:0)

您可以使用http://json2csharp.com/为json创建映射类。如果您拥有Visual Studio,还可以复制json和“选择性粘贴”以在Visual Studio中自动创建类。