使用Instagram API响应创建JSONObject的问题

时间:2014-12-02 02:09:13

标签: android json android-asynctask instagram

我已经在Instagram端点上调用了包含标签#selfie的图片。我遇到的问题是处理响应。我得到了一个巨大的JSON响应,我真正需要的只是“缩略图”图像“url”。

如何从JSONObject中的数据构建对象?

public static class FetchTaggedImages extends AsyncTask<String, Void, JSONArray> {


    @Override
    protected JSONArray doInBackground(String... params) {
        String mAccessToken = params[0];
        String url = (API_URL + "?access_token=" + mAccessToken);
        HttpGet httpGet = new HttpGet(url);
        HttpClient httpclient = new DefaultHttpClient();
        String instagramJSONResponse = null;
        JSONArray responseArray = null;
        HttpResponse postResponse;

        try {
            postResponse = httpclient.execute(httpGet);
            instagramJSONResponse = EntityUtils.toString(postResponse
                    .getEntity());
            JSONObject jsonObject = new JSONObject(instagramJSONResponse);
            String thumbnail = jsonObject.getString("url");

            responseArray = new JSONArray(instagramJSONResponse);
            Log.e("RESPONSE", instagramJSONResponse);
        } catch (Exception ex) {
            ex.printStackTrace();
        }

        return responseArray;
    }

    @Override
    protected void onPostExecute(JSONArray response) {
        // TODO Auto-generated method stub
        super.onPostExecute(response);
        Log.e("ONPOSTEXECUTE", "ONPOSTEXECUTE");

        JSONObject jsonObject;


    }
}

这里是JSON STRING的样本

    {  
   "data":[  
      {  
         "tags":[  
            "summer",
            "love",
            "tagstagram",
            "colorful",
            "instalike",
            "selfie",
            "swag",
            "instago",
            "igers",
            "follow4follow",
            "follow",
            "instadaily",
            "friends",
            "style",
            "webstagram",
            "look",
            "instafollow",
            "iphoneonly",
            "instagood",
            "amazing",
            "instacool",
            "bestoftheday",
            "fun",
            "followme",
            "like4like",
            "picoftheday",
            "photooftheday"
         ],
         "location":null,
         "link":"http:\/\/instagram.com\/p\/wFlirqPxAW\/",
         "user_has_liked":false,
         "caption":{  
            "id":"866263615190011927",
            "created_time":"1417486691",
            "text":"First contest of the season ",
            "from":{  
               "id":"507279035",
               "profile_picture":"https:\/\/instagramimages-a.akamaihd.net\/profiles\/profile_507279035_75sq_1397511894.jpg",
               "username":"jdbv45",
               "full_name":"Juan Basave"
            }
         },
         "type":"image",
         "id":"866263614401482774_507279035",
         "likes":{  
            "data":[  
               {  
                  "id":"956576929",
                  "profile_picture":"https:\/\/igcdn-photos-e-a.akamaihd.net\/hphotos-ak-xfa1\/10802992_1561400387426348_1634151041_a.jpg",
                  "username":"don_assi2014",
                  "full_name":"-Ali Assi-"
               },
               {  
                  "id":"520771011",
                  "profile_picture":"https:\/\/igcdn-photos-b-a.akamaihd.net\/hphotos-ak-xfa1\/1889350_1565133803717465_1353157813_a.jpg",
                  "username":"michelle.y6",
                  "full_name":"Michelle Y"
               },
               {  
                  "id":"873300277",
                  "profile_picture":"https:\/\/igcdn-photos-g-a.akamaihd.net\/hphotos-ak-xfa1\/10817804_1543101285933854_123980756_a.jpg",
                  "username":"ayah_shahbanderr",
                  "full_name":"Ayah"
               },
               {  
                  "id":"199966711",
                  "profile_picture":"https:\/\/igcdn-photos-a-a.akamaihd.net\/hphotos-ak-xfa1\/10617174_587781691338392_1164910966_a.jpg",
                  "username":"elijah_reissman_",
                  "full_name":"Elijah Reissman"
               }
            ],
            "count":13
         },
         "images":{  
            "low_resolution":{  
               "url":"http:\/\/scontent-b.cdninstagram.com\/hphotos-xap1\/t51.2885-15\/1739913_754468237961901_1189124740_a.jpg",
               "height":306,
               "width":306
            },
            "standard_resolution":{  
               "url":"http:\/\/scontent-b.cdninstagram.com\/hphotos-xap1\/t51.2885-15\/1739913_754468237961901_1189124740_n.jpg",
               "height":640,
               "width":640
            },
            "thumbnail":{  
               "url":"http:\/\/scontent-b.cdninstagram.com\/hphotos-xap1\/t51.2885-15\/1739913_754468237961901_1189124740_s.jpg",
               "height":150,
               "width":150
            }
         },
         "users_in_photo":[  

         ],
         "created_time":"1417486691",
         "user":{  
            "id":"507279035",
            "profile_picture":"https:\/\/instagramimages-a.akamaihd.net\/profiles\/profile_507279035_75sq_1397511894.jpg",
            "username":"jdbv45",
            "bio":"",
            "website":"",
            "full_name":"Juan Basave"
         },
         "filter":"Mayfair",
         "comments":{  
            "data":[  
               {  
                  "id":"866282245801841449",
                  "created_time":"1417488912",
                  "text":"#TagStaGram #love #friends @tagstagram #photooftheday #selfie #amazing #followme #follow4follow #like4like #look #instalike #igers #picoftheday #instadaily #instafollow #fun #iphoneonly #instagood #bestoftheday #instacool #instago #summer #follow #webstagram #colorful #style #swag",
                  "from":{  
                     "id":"507279035",
                     "profile_picture":"https:\/\/instagramimages-a.akamaihd.net\/profiles\/profile_507279035_75sq_1397511894.jpg",
                     "username":"jdbv45",
                     "full_name":"Juan Basave"
                  }
               }
            ],
            "count":1
         },
         "attribution":null
      },

1 个答案:

答案 0 :(得分:0)

“thumbnail”位于“images”对象中,“images”对象位于“data”数组的第一个对象中。

所以你需要首先获取“数据”JSONArray,访问每个数组项,从项中找到“images”对象,最后你可以访问“thumbnail”对象。

您的代码似乎访问了顶级对象中没有“url”属性的“url”。

如果您想通过JSONObject框架访问,您应该实现如下代码:

public String getUrl(JSONObject response) throws JSONException {
  JSONArray data = response.getJSONArray("data");
  JSONObject object = data.getJSONObject(0);
  JSONObject images = object.getJSONObject("images");
  JSONObject thumbnail = images.getJSONObject("thumbnail");
  return thumbnail.getString("url");
}

如果您需要url的集合,则需要迭代访问所有数据对象,如下所示:

JSONArray data = response.getJSONArray("data");
for (int i = 0; i < data.length(); i++) {
  JSONObject object = data.getJSONObject(0);
  // ...
}

这是处理Android Framework提供的JSON字符串的基本功能。 如果您认为还有很长的路要走,请考虑使用GSON或Jackson JSON处理器。