android - How to fetch data of API -
@override protected string doinbackground(string... params) { try{ jsonparser jparser = new jsonparser(); string url="http://earlykid.com/android/?page=2"; idarray=jparser.getjsonfromurl(url); log.e("fetch data",idarray.tostring()); mlatestlist.clear(); for(int i=0;i<idarray.length();i++){ try{ jsonobject jobject; mkids=new kids(); jobject=idarray.getjsonobject(i); mkids.settotalpages(jobject.getstring("totalitems")); mkids.setcurrentpage(jobject.getstring("currentpage")); }catch(jsonexception e){ log.e("log_tag", "error parsing data "+e.tostring()); log.e("log_tag", "failed data was:\n" + idarray); } } }catch(exception e){ } return null; } @override protected void onpostexecute(string result) { mprogress.dismiss(); }
when fetch data code.it shows me error.
logcat here:-
error parsing data org.json.jsonexception: value {"totalitems":38,"currentpage":"2","items":[{"id":"atb1le9_wzk","title":"abcd alphabets song - songs kids","category":"education","thumbnail":"http:\/\/www.earlykid.com\/android\/timthumb.php?w=600&h=330src=http:\/\/i1.ytimg.com\/vi\/atb1le9_wzk\/hqdefault.jpg"},{"id":"uxeesu0qnro","title":"the rich man , sons story - animated stories kids","category":"education","thumbnail":"http:\/\/www.earlykid.com\/android\/timthumb.php?w=600&h=330src=http:\/\/i1.ytimg.com\/vi\/uxeesu0qnro\/hqdefault.jpg"},{"id":"hmiykdyrelk","title":"here go round mulberry bush - nursery rhyme kids","category":"education","thumbnail":"http:\/\/www.earlykid.com\/android\/timthumb.php?w=600&h=330src=http:\/\/i1.ytimg.com\/vi\/hmiykdyrelk\/hqdefault.jpg"},{"id":"9tlncurms5c","title":"old mac donald had farm - nursery rhymes kids","category":"education","thumbnail":"http:\/\/www.earlykid.com\/android\/timthumb.php?w=600&h=330src=http:\/\/i1.ytimg.com\/vi\/9tlncurms5c\/hqdefault.jpg"},{"id":"dpq_5gr_mmo","title":"five little monkeys jumping on bed - nursery rhymes","category":"education","thumbnail":"http:\/\/www.earlykid.com\/android\/timthumb.php?w=600&h=330src=http:\/\/i1.ytimg.com\/vi\/dpq_5gr_mmo\/hqdefault.jpg"},{"id":"cvwhp2xfljw","title":"rain rain go away - nursery rhyme","category":"education","thumbnail":"http:\/\/www.earlykid.com\/android\/timthumb.php?w=600&h=330src=http:\/\/i1.ytimg.com\/vi\/cvwhp2xfljw\/hqdefault.jpg"},{"id":"weva9if6i3s","title":"i'm little teapot nursery rhyme lyrics","category":"education","thumbnail":"http:\/\/www.earlykid.com\/android\/timthumb.php?w=600&h=330src=http:\/\/i1.ytimg.com\/vi\/weva9if6i3s\/hqdefault.jpg"},{"id":"tqhyrssam5y","title":"ten little fingers ten little toes - nursery rhyme","category":"education","thumbnail":"http:\/\/www.earlykid.com\/android\/timthumb.php?w=600&h=330src=http:\/\/i1.ytimg.com\/vi\/tqhyrssam5y\/hqdefault.jpg"},{"id":"fdgolmgf1ne","title":"jingle bells christmas song","category":"education","thumbnail":"http:\/\/www.earlykid.com\/android\/timthumb.php?w=600&h=330src=http:\/\/i1.ytimg.com\/vi\/fdgolmgf1ne\/hqdefault.jpg"},{"id":"y83fbhn6fbk","title":"pussy cat pussy cat have been? - nursery rhyme","category":"film","thumbnail":"http:\/\/www.earlykid.com\/android\/timthumb.php?w=600&h=330src=http:\/\/i1.ytimg.com\/vi\/y83fbhn6fbk\/hqdefault.jpg"},{"id":"uuqnhzeiwei","title":"thank world sweet - kids song","category":"film","thumbnail":"http:\/\/www.earlykid.com\/android\/timthumb.php?w=600&h=330src=http:\/\/i1.ytimg.com\/vi\/uuqnhzeiwei\/hqdefault.jpg"},{"id":"g0u1iwumg8q","title":"ding dong bell - nursery rhyme","category":"film","thumbnail":"http:\/\/www.earlykid.com\/android\/timthumb.php?w=600&h=330src=http:\/\/i1.ytimg.com\/vi\/g0u1iwumg8q\/hqdefault.jpg"}]
what getting jsonobject
url http://earlykid.com/android/?page=2
you have this
idarray=jparser.getjsonfromurl(url); // wrong. json object
{
represents json object node
[
represents json array node
your json
{ "totalitems": 38, "totalpages": 3, "itemsperpage": 12, "currentpage": "2", "items": [ { "id": "atb1le9_wzk", "category": "education", "title": "abcd alphabets song - songs kids", "thumbnail": "http://www.earlykid.com/android/timthumb.php?w=600&h=330src=http://i1.ytimg.com/vi/atb1le9_wzk/hqdefault.jpg" }, { "id": "uxeesu0qnro", "category": "education", "title": "the rich man , sons story - animated stories kids", "thumbnail": "http://www.earlykid.com/android/timthumb.php?w=600&h=330src=http://i1.ytimg.com/vi/uxeesu0qnro/hqdefault.jpg" }, { "id": "hmiykdyrelk", "category": "education", "title": "here go round mulberry bush - nursery rhyme kids", "thumbnail": "http://www.earlykid.com/android/timthumb.php?w=600&h=330src=http://i1.ytimg.com/vi/hmiykdyrelk/hqdefault.jpg" }, { "id": "9tlncurms5c", "category": "education", "title": "old mac donald had farm - nursery rhymes kids", "thumbnail": "http://www.earlykid.com/android/timthumb.php?w=600&h=330src=http://i1.ytimg.com/vi/9tlncurms5c/hqdefault.jpg" }, { "id": "dpq_5gr_mmo", "category": "education", "title": "five little monkeys jumping on bed - nursery rhymes", "thumbnail": "http://www.earlykid.com/android/timthumb.php?w=600&h=330src=http://i1.ytimg.com/vi/dpq_5gr_mmo/hqdefault.jpg" }, { "id": "cvwhp2xfljw", "category": "education", "title": "rain rain go away - nursery rhyme", "thumbnail": "http://www.earlykid.com/android/timthumb.php?w=600&h=330src=http://i1.ytimg.com/vi/cvwhp2xfljw/hqdefault.jpg" }, { "id": "weva9if6i3s", "category": "education", "title": "i'm little teapot nursery rhyme lyrics", "thumbnail": "http://www.earlykid.com/android/timthumb.php?w=600&h=330src=http://i1.ytimg.com/vi/weva9if6i3s/hqdefault.jpg" }, { "id": "tqhyrssam5y", "category": "education", "title": "ten little fingers ten little toes - nursery rhyme", "thumbnail": "http://www.earlykid.com/android/timthumb.php?w=600&h=330src=http://i1.ytimg.com/vi/tqhyrssam5y/hqdefault.jpg" }, { "id": "fdgolmgf1ne", "category": "education", "title": "jingle bells christmas song", "thumbnail": "http://www.earlykid.com/android/timthumb.php?w=600&h=330src=http://i1.ytimg.com/vi/fdgolmgf1ne/hqdefault.jpg" }, { "id": "y83fbhn6fbk", "category": "film", "title": "pussy cat pussy cat have been? - nursery rhyme", "thumbnail": "http://www.earlykid.com/android/timthumb.php?w=600&h=330src=http://i1.ytimg.com/vi/y83fbhn6fbk/hqdefault.jpg" }, { "id": "uuqnhzeiwei", "category": "film", "title": "thank world sweet - kids song", "thumbnail": "http://www.earlykid.com/android/timthumb.php?w=600&h=330src=http://i1.ytimg.com/vi/uuqnhzeiwei/hqdefault.jpg" }, { "id": "g0u1iwumg8q", "category": "film", "title": "ding dong bell - nursery rhyme", "thumbnail": "http://www.earlykid.com/android/timthumb.php?w=600&h=330src=http://i1.ytimg.com/vi/g0u1iwumg8q/hqdefault.jpg" } ] }
parsing
try { jsonobject jb = new jsonobject(myjsonstring); string totalitems = jb.getstring("totalitems"); log.i("......", "" + totalitems); string totalpages = jb.getstring("totalpages"); string itemsperpage = jb.getstring("itemsperpage"); string currentpage = jb.getstring("currentpage"); jsonarray jarr = jb.getjsonarray("items"); (int = 0; < jarr.length(); i++) { jsonobject jb1 = jarr.getjsonobject(i); string id = jb1.getstring("id"); string categoy = jb1.getstring("category"); string title = jb1.getstring("title"); string pic = jb1.getstring("thumbnail"); log.i("........", id); } } catch (exception e) { }
Comments
Post a Comment