java - JSON issue in Android project -
i messing around code tutorial: http://mobisys.in/blog/2012/01/parsing-json-from-url-in-android/
i downloaded source code , got set in project. launched app , pressed "get json" button , worked.
the url being used in tutorial http://mobisys.in/quicknotify/get_departments.php?company_id=1.
the json comes page looks this:
[{"dept":"mobile","dept_id":"1"},{"dept":"web","dept_id":"2"}]
i wanted use own url changed 1 gave own. changed selectors match new link.
the new url http://football.myfantasyleague.com/2007/export?type=topadds&w=12&json=1 , returns in browser:
{"version":"1.0","topadds":{"week":"12","player":[{"percent":"41.65","id":"8827"},{"percent":"36.89","id":"8752"},{"percent":"25.60","id":"0519"},{"percent":"16.35","id":"5730"},{"percent":"14.14","id":"6562"},{"percent":"13.56","id":"7547"},{"percent":"10.34","id":"3869"},{"percent":"9.87","id":"5660"},{"percent":"8.75","id":"7553"},{"percent":"8.36","id":"7084"},{"percent":"8.12","id":"7480"},{"percent":"8.05","id":"4907"},{"percent":"7.10","id":"6616"},{"percent":"6.85","id":"7625"},{"percent":"6.66","id":"3494"},{"percent":"6.56","id":"8117"},{"percent":"6.11","id":"7071"},{"percent":"5.91","id":"4092"},{"percent":"5.86","id":"7399"},{"percent":"5.78","id":"6715"},{"percent":"4.68","id":"4932"},{"percent":"4.49","id":"3969"},{"percent":"4.46","id":"7837"},{"percent":"4.38","id":"7895"},{"percent":"4.24","id":"7021"},{"percent":"4.13","id":"0525"},{"percent":"4.05","id":"5666"},{"percent":"3.86","id":"6528"},{"percent":"3.81","id":"0512"},{"percent":"3.70","id":"3875"},{"percent":"3.68","id":"4984"},{"percent":"3.67","id":"8135"},{"percent":"3.48","id":"5482"},{"percent":"3.32","id":"1653"},{"percent":"3.29","id":"8244"},{"percent":"3.20","id":"0511"},{"percent":"2.76","id":"3369"},{"percent":"2.73","id":"5065"},{"percent":"2.72","id":"6786"},{"percent":"2.70","id":"8326"},{"percent":"2.66","id":"7150"},{"percent":"2.65","id":"7034"},{"percent":"2.64","id":"3336"},{"percent":"2.59","id":"6950"},{"percent":"2.54","id":"8685"},{"percent":"2.48","id":"8500"},{"percent":"2.44","id":"8339"},{"percent":"2.38","id":"0507"},{"percent":"2.35","id":"8074"},{"percent":"2.34","id":"7883"},{"percent":"2.27","id":"8667"},{"percent":"2.21","id":"8742"},{"percent":"2.18","id":"0502"},{"percent":"2.15","id":"7740"},{"percent":"2.12","id":"7472"},{"percent":"2.11","id":"3298"},{"percent":"2.02","id":"9008"},{"percent":"2.01","id":"6743"},{"percent":"2.01","id":"6643"}]},"encoding":"iso-8859-1"}
now, when launch app , click on "get json" button "success[]" instead of "success -ex: data...-" , no list appears data, before.
what should make work new url?
here code using:
myfantasyleagueactivity.java
public final static string baseurl="http://football.myfantasyleague.com/"; arraylist<dept_hold> deptlist=new arraylist<dept_hold>(); private class getdeptaynctask extends asynctask<hashtable<string,string>,void,string>{ @override protected string doinbackground(hashtable<string,string>... params) { hashtable ht=params[0]; string json=helperhttp.getjsonresponsefromurl(baseurl+"2007/export?type=topadds&w=12&json=1", ht); if(json!=null) parsejsonstring(deptlist,json); else{ return "invalid company id"; } return "success"; } protected void parsejsonstring(arraylist<dept_hold> deptlist,string json){ try { jsonarray array=new jsonarray(json); for(int i=0;i<array.length();i++){ jsonobject j=array.getjsonobject(i); dept_hold d=new dept_hold(); d.one=j.optstring("percent",""); d.two=j.optstring("id",""); deptlist.add(d); } } catch (jsonexception e) { e.printstacktrace(); } } @override protected void onpostexecute(string result){ if(result=="success") { toast.maketext(myfantasyleagueactivity.this, "success"+deptlist, toast.length_short).show(); deptarrayadapter adapter=new deptarrayadapter(myfantasyleagueactivity.this,r.id.text1,deptlist); listview listv=(listview)findviewbyid(r.id.lv); listv.setadapter(adapter); } else{} } }
i'm sure solution obvious , i'll feel stupid, spent time looking @ this. need new set of eyes me figure out what's going on.
any suggestions?
the original json has array @ top level, new json object. in parsejsonstring
method, first need create jsonobject
out of data. need drill topadds
, player
properties array of percent/id pairs. without error checking:
protected void parsejsonstring(arraylist<dept_hold> deptlist,string json){ try { jsonobject top = new jsonobject(json); jsonobject topadds = (jsonobject) top.get("topadds"); jsonarray array = (jsonarray) topadds.get("player"); for(int = 0; < array.length(); i++) { jsonobject j = array.getjsonobject(i); dept_hold d = new dept_hold(); d.one = j.optstring("percent",""); d.two = j.optstring("id",""); deptlist.add(d); } } catch (jsonexception e) { e.printstacktrace(); } }
Comments
Post a Comment