java - Android JSON access thumbnail element -
i have json result http://ajax.googleapis.com/ajax/services/feed/load?v=1.0&num=25&q=http://www.abc.net.au/news/feed/51120/rss.xml
i trying access thumbnails each news article located in entries>mediagroups>contents>thumbnails>url
however unsure of how access deep in element. code far.
public list<newsobj> constructjson(string jsonin){ arraylist<newsobj> newslist = new arraylist<>(); try{ //add more levels extract json jsonobject jsonobject1 = new jsonobject(jsonin); string responsedata = jsonobject1.getstring("responsedata"); log.d("receivejsonobjectlevel1",responsedata); jsonobject jsonobject2 = new jsonobject(responsedata); string feed = jsonobject2.getstring("feed"); log.d("receivejsonobjectlevel2",feed); jsonobject jsonobject3 = new jsonobject(feed); string entries = jsonobject3.getstring("entries"); log.d("receivejsonobjectlevel3", entries); jsonarray jsonarray1 = new jsonarray(entries); for(int i=0; i<jsonarray1.length();i++){ jsonobject mediagroups = jsonarray1.getjsonobject(i); string mediaitems = mediagroups.getstring("mediagroups"); string title = mediagroups.getstring("title"); string url = mediagroups.getstring("link"); string description = mediagroups.getstring("contentsnippet"); string publisheddate = mediagroups.getstring("publisheddate"); // main information news article //for further thumbnail sizes? jsonarray jsonarray2 = new jsonarray(mediaitems); for(int j=0;j<jsonarray2.length();j++){ jsonobject contents = jsonarray2.getjsonobject(j); string contentitems = contents.getstring("contents"); log.d("receivejsonobjectarray2",contentitems); jsonarray jsonarray3 = new jsonarray(contentitems); for(int k=0;k<jsonarray3.length();k++){ jsonobject items = jsonarray3.getjsonobject(k); //too specific string imgurl = items.getstring("url"); //log.d("receivejsonobjectarray3",imgurl); newsobj aobj = new newsobj(title, imgurl,url, publisheddate); newslist.add(aobj); } } } }catch (jsonexception e){ e.printstacktrace(); log.d("receivejsonerror",e.tostring()); } return newslist; }
you need make jsonarray thumbnails. reference see example
try
public list<newsobj> constructjson(string jsonin){ arraylist<newsobj> newslist = new arraylist<>(); try{ //add more levels extract json jsonobject jsonobject1 = new jsonobject(jsonin); string responsedata = jsonobject1.getstring("responsedata"); log.d("receivejsonobjectlevel1",responsedata); jsonobject jsonobject2 = new jsonobject(responsedata); string feed = jsonobject2.getstring("feed"); log.d("receivejsonobjectlevel2",feed); jsonobject jsonobject3 = new jsonobject(feed); string entries = jsonobject3.getstring("entries"); log.d("receivejsonobjectlevel3", entries); jsonarray jsonarray1 = new jsonarray(entries); for(int i=0; i<jsonarray1.length();i++){ jsonobject mediagroups = jsonarray1.getjsonobject(i); string mediaitems = mediagroups.getstring("mediagroups"); string title = mediagroups.getstring("title"); string url = mediagroups.getstring("link"); string description = mediagroups.getstring("contentsnippet"); string publisheddate = mediagroups.getstring("publisheddate"); // main information news article //for further thumbnail sizes? jsonarray jsonarray2 = new jsonarray(mediaitems); for(int j=0;j<jsonarray2.length();j++){ jsonobject contents = jsonarray2.getjsonobject(j); string contentitems = contents.getstring("contents"); log.d("receivejsonobjectarray2",contentitems); jsonarray jsonarray3 = new jsonarray(contentitems); for(int k=0;k<jsonarray3.length();k++){ jsonobject items = jsonarray3.getjsonobject(k); //too specific string imgurl = items.getstring("url"); //log.d("receivejsonobjectarray3",imgurl); string thumbnails = items.getstring("thumbnails"); jsonarray jsonarray4 = new jsonarray(thumbnails); for(int l=0;l<jsonarray4.length();l++){ jsonobject thumbnails1 = jsonarray4.getjsonobject(l); string height = items.getstring("height"); string width = items.getstring("width"); string thumburl = items.getstring("url"); } newsobj aobj = new newsobj(title, imgurl,url, publisheddate); newslist.add(aobj); } } } }catch (jsonexception e){ e.printstacktrace(); log.d("receivejsonerror",e.tostring()); } return newslist; }
Comments
Post a Comment