如何在android中获取reddit URL JSON数据?

时间:2014-06-17 11:38:17

标签: android json logcat arrays jsonobject

我试图从subreddit获取一个URL列表,以便将它们加载到通用图像加载器中以便查看乐趣。但是,我无法弄清楚我的查询出错的地方。另外,与eclipses logcat相比,我对android studio的logcat并不熟悉,所以我不确定在哪里查找我的调试响应。

这是查询方法:

        public void queryReddit()
    {
        // Prepare your search string to be put in a URL
        // It might have reserved characters or something
//        String urlString = "";
//        try {
//            urlString = URLEncoder.encode(searchString, "UTF-8");
//        } catch (UnsupportedEncodingException e) {
//
//            // if this fails for some reason, let the user know why
//            e.printStackTrace();
//            Toast.makeText(this, "Error: " + e.getMessage(), Toast.LENGTH_LONG).show();
//        }

        // Create a client to perform networking
        AsyncHttpClient client = new AsyncHttpClient();

        // 11. start progress bar
        setProgressBarIndeterminateVisibility(true);


        // Have the client get a JSONArray of data
        // and define how to respond
        client.get("http://www.reddit.com/r/pics/.json",
                new JsonHttpResponseHandler() {

                    @Override
                    public void onSuccess(JSONObject jsonObject) {
                        // 11. stop progress bar
                        setProgressBarIndeterminateVisibility(false);

                        // Display a "Toast" message
                        // to announce your success
                        Toast.makeText(getApplicationContext(), "Success!", Toast.LENGTH_LONG).show();

//                        // 8. For now, just log results
//                        Log.d("omg android", jsonObject.toString());


                        try
                        {
                            Log.d("go reddit yay", jsonObject.toString());
                            JSONObject testingData = (JSONObject) jsonObject.get("data");
                            JSONArray testingChildren = (JSONArray) testingData.get("children");
                            JSONObject testingLogData = (JSONObject) testingChildren.get(0);
                            JSONArray children = (JSONArray) jsonObject.get("children");
                            JSONObject logData = (JSONObject) children.get(0);
                            Log.d("go reddit yay", logData.getString("url"));
                            Log.d("go reddit yay", testingLogData.getString("url"));
                            for(int i = 0; i < 10; i++)
                            {
                                JSONObject data = (JSONObject) children.get(i);



                                if(data.getString("url") != null)
                                {
                                    System.out.println(data.getString("url"));
                                }
                                //if the url field exists and it's a picture that univ image loader understands then add it
                                if(data.getString("url") != null && data.getString("url").substring(data.getString("url").length()-3).equals("png") ||
                                        data.getString("url").substring(data.getString("url").length()-3).equals("jpg"))
                                {
                                    imageUrls.add(data.getString("url"));
                                    System.out.println(data.getString("url"));
                                }

                                //TODO I found this error: this requires android.permission.INTERACT_ACROSS_USERS_FULL
                            }
                            mPagerAdapter.notifyDataSetChanged();
                        } catch (JSONException e)
                        {
                            e.printStackTrace();
                        }
                        //TODO Might want to put all this data in a try catch block and do it right here.
                        // update the data in your custom method.
                        //updateData()
                    }

                    @Override
                    public void onFailure(int statusCode, Throwable throwable, JSONObject error)
                    {
                        // 11. stop progress bar
                        setProgressBarIndeterminateVisibility(false);

                        // Display a "Toast" message
                        // to announce the failure
                        Toast.makeText(getApplicationContext(), "Error: " + statusCode + " " + throwable.getMessage(), Toast.LENGTH_LONG).show();

                        // Log error message
                        // to help solve any problems
                        Log.e("omg android", statusCode + " " + throwable.getMessage());
                    }
                });
    }

在try catch块中,最后发送到logcat的唯一记录信息是第一行,Log.d(&#34; go reddit yay&#34;,jsonObject.toString());

我无法找到其他日志调用的响应,这对我来说很奇怪。

这是第一次日志调用的响应:

06-17 06:35:29.324  17133-17133/.wallpaper D/absfr﹕ {"data":{"after":"t3_2823ou","children":[{"data":{"media_embed":{},"author_flair_css_class":null,"score":503,"created_utc":1402931529,"clicked":false,"visited":false,"id":"28a94k","author":"JamesBDW","title":"Any Bronson fans? [1920x1080]","over_18":false,"created":1402960329,"name":"t3_28a94k","selftext_html":null,"domain":"i.imgur.com","author_flair_text":null,"secure_media":null,"num_reports":null,"edited":false,"stickied":false,"link_flair_text":null,"link_flair_css_class":null,"saved":false,"secure_media_embed":{},"subreddit_id":"t5_2qmjl","distinguished":null,"gilded":0,"url":"https:\/\/i.imgur.com\/Hq1fcSm.jpg","banned_by":null,"subreddit":"wallpaper","is_self":false,"num_comments":31,"approved_by":null,"thumbnail":"http:\/\/a.thumbs.redditmedia.com\/Dh2iU7Q0rpFogkWt.jpg","permalink":"\/r\/wallpaper\/comments\/28a94k\/any_bronson_fans_1920x1080\/","hidden":false,"likes":null,"downs":188,"ups":691,"selftext":"","media":null},"kind":"t3"},{"data":{"media_embed":{},"author_flair_css_class":null,"score":8,"created_utc":1402989714,"clicked":false,"visited":false,"id":"28cnyn","author":"ZadocPaet","title":"Active Dunes on Mars (OS) [1024x768]","over_18":false,"created":1403018514,"name":"t3_28cnyn","selftext_html":null,"domain":"nasa.gov","author_flair_text":null,"secure_media":null,"num_reports":null,"edited":false,"stickied":false,"link_flair_text":null,"link_flair_css_class":null,"saved":false,"secure_media_embed":{},"subreddit_id":"t5_2qmjl","distinguished":null,"gilded":0,"url":"http:\/\/www.nasa.gov\/sites\/default\/files\/styles\/1024x768_autoletterbox\/public\/pia18244.jpg","banned_by":null,"subreddit":"wallpaper","is_self":false,"num_comments":1,"approved_by":null,"thumbnail":"http:\/\/a.thumbs.redditmedia.com\/dFTBquSWiMSjK0aZ.jpg","permalink":"\/r\/wallpaper\/comments\/28cnyn\/active_dunes_on_mars_os_1024x768\/","hidden":false,"likes":null,"downs":3,"ups":11,"selftext":"","media":null},"kind":"t3"},{"data":{"media_embed":{"content":"&lt;iframe class=\"embedly-embed\" src=\"\/\/cdn.embedly.com\/widgets\/media.html?src=http%3A%2F%2Fimgur.com%2Fa%2F0jeZf%2Fembed&amp;url=http%3A%2F%2Fimgur.com%2Fa%2F0jeZf&amp;image=http%3A%2F%2Fi.imgur.com%2F2PdUiuE.jpg&amp;key=2aa3c4d5f3de4f5b9120b660ad850dc9&amp;type=text%2Fhtml&amp;schema=imgur\" width=\"550\" height=\"550\" scrolling=\"no\" frameborder=\"0\" allowfullscreen&gt;&lt;\/iframe&gt;","scrolling":false,"height":550,"width":550},"author_flair_css_class":null,"score":1,"created_utc":1403004127,"clicked":false,"visited":false,"id":"28czid","author":"smessies","title":"I found a wallpaper album full of iconic design chairs. I tought there were some important ones missing so i started adding some myself. work in progress! [1920x1200]","over_18":false,"created":1403032927,"name":"t3_28czid","selftext_html":null,"domain":"imgur.com","author_flair_text":null,"secure_media":null,"num_reports":null,"edited":false,"stickied":false,"link_flair_text":null,"link_flair_css_class":null,"saved":false,"secure_media_embed":{},"subreddit_id":"t5_2qmjl","distinguished":null,"gilded":0,"url":"http:\/\/imgur.com\/a\/0jeZf","banned_by":null,"subreddit":"wallpaper","is_self":false,"num_comments":0,"approved_by":null,"thumbnail":"http:\/\/b.thumbs.redditmedia.com\/GYaN5fyJfY8fI8xE.jpg","permalink":"\/r\/wallpaper\/comments\/28czid\/i_found_a_wallpaper_album_full_of_iconic_design\/","hidden":false,"likes":null,"downs":0,"ups":1,"selftext":"","media":{"type":"imgur.com","oembed":{"thumbnail_height":1200,"author_url":"http:\/\/imgur.com\/user\/smessies","width":550,"type":"rich","version":"1.0","thumbnail_url":"http:\/\/i.imgur.com\/2PdUiuE.jpg","thumbnail_width":1920,"title":"imgur: the simple image sharer","height":550,"description":"Imgur is home to the web's most popular image content, curated in real time by a dedicated community through commenting, voting and sharing.","author_name":"smessies","html":"&lt;iframe class=\"embedly-embed\" src=\"\/\/cdn.embedly.com\/widgets\/media.html?src=http%3A%2F%2Fimgur.com%2Fa%2F0jeZf%2Fembed&amp;url=http%3A%2F%2Fimgur.com%2F

不知道该怎么做。

最后一个调试信息是onSuccess方法的toast确实出现了,所以它在查询中显然是成功的,我在解释数据方面做错了。

1 个答案:

答案 0 :(得分:0)

根据您的说明,在第一次Log.d("go reddit yay", ...);电话会引发您无法看到的异常后,它似乎就是一个声明。

尝试替换您的异常处理代码:

} catch (JSONException e)
{
    e.printStackTrace();
}

用这个:

} catch (Throwable t)
{
    Log.e("omg android", "Exception in onSuccess()", t);
}

并检查是否记录了任何异常。