android - Issue creating JSONObject with Instagram API response -
i have made call instagram endpoint images contain tag #selfie. issue having in dealing response. massive json response, , need "thumbnail" image "url".
how can go structuring object data in 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; } }
here sample of 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 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 },
"thumbnail" in "images" object, , "images" object in first object of "data" array.
so need obtain "data" jsonarray first, access each array item, find "images" object item, , can access "thumbnail" object.
your code seems access "url" in top level object not have "url" property.
if you'd access via jsonobject framework, should implement code this:
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"); }
if need collection of url, need iterate accessing data objects this:
jsonarray data = response.getjsonarray("data"); (int = 0; < data.length(); i++) { jsonobject object = data.getjsonobject(0); // ... }
this basic function dealing json string android framework provides. if feel long way go, consider using gson or jackson json processor.
Comments
Post a Comment