java - Parse API Data into TextView -
i have api link has json element named title, , trying store value textview. here have far in main activity code supposed display contained string:
@override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); jsonobject jobject; try { jobject = new jsonobject("https://chex-triplebyte.herokuapp.com/api/cats?page=0"); string mresponse = jobject.getstring("title"); textview t = (textview) findviewbyid(r.id.title_image); t.settext(mresponse); } catch (jsonexception e) { // todo auto-generated catch block e.printstacktrace(); } } the api link provided works can see value title trying obtain.
try this:
textview t = (textview) findviewbyid(r.id.title_image); try { url = new url("https://chex-triplebyte.herokuapp.com/api/cats?page=0"); urlconnection = (httpurlconnection) url .openconnection(); urlconnection.connect(); inputstream in = urlconnection.getinputstream(); reader = new bufferedreader(new inputstreamreader(in)); //inputstreamreader isw = new inputstreamreader(in); stringbuffer buffer = new stringbuffer(); string line = ""; while ((line = reader.readline()) != null) { buffer.append(line); } string jsonresponse= buffer.tostring(); jsonobject jsonobj = new jsonobject(jsonresponse); jsonarray jarray = jsono.getjsonarray("jsontitle"); (int = 0; < jarray.length(); i++) { jsonobject object = jarray.getjsonobject(i); t.settext(object.getstring("title")); } } catch (jsonexception e) { e.printstacktrace(); } but have make sure have correct json format this:
{jsontitle:[{"title":"space keybaord cat","timestamp":"2017-09-11t04:00:04z","image_url":"https://triplebyte-cats.s3.amazonaws.com/space.jpg","description":"in space, no 1 can hear purr."},{"title":"jiji","timestamp":"2017-09-11t03:00:04z","image_url":"https://triplebyte-cats.s3.amazonaws.com/jiji.png","description":"you'd think they'd never seen girl , cat on broom before"},{"title":"limecat","timestamp":"2017-09-11t02:00:04z","image_url":"https://triplebyte-cats.s3.amazonaws.com/lime.jpg","description":"destroyer of clockspider , evil followers, limecat 1 true god."},{"title":"astronaut cat","timestamp":"2017-09-11t01:00:04z","image_url":"https://triplebyte-cats.s3.amazonaws.com/astronaut.jpg","description":"houston, have purroblem"},{"title":"grumpy cat","timestamp":"2017-09-11t00:00:04z","image_url":"https://triplebyte-cats.s3.amazonaws.com/grumpy.jpg","description":"queen of rbf"},{"title":"soviet cat","timestamp":"2017-09-10t23:00:04z","image_url":"https://triplebyte-cats.s3.amazonaws.com/soviet.jpg","description":"in soviet russia cat pets you!"},{"title":"serious business cat","timestamp":"2017-09-10t22:00:04z","image_url":"https://triplebyte-cats.s3.amazonaws.com/serious.jpg","description":"srsly guise"},{"title":"sophisticated cat","timestamp":"2017-09-10t21:00:04z","image_url":"https://triplebyte-cats.s3.amazonaws.com/sophisticated.png","description":"i should buy boat"},{"title":"shironeko","timestamp":"2017-09-10t20:00:04z","image_url":"https://triplebyte-cats.s3.amazonaws.com/shironeko.png","description":"the zen master kitty"},{"title":"puss in boots","timestamp":"2017-09-10t19:00:04z","image_url":"https://triplebyte-cats.s3.amazonaws.com/puss.jpg","description":"don't dare litter box on me!"}]}
Comments
Post a Comment