java - Understanding how to use my Android JSON Parser -
hello, i'm having hard time understanding how want json parser. i'm parsing info located @ http://api.pathofexile.com/leagues?type=all pull out league ids (currently "default", "hardcore", "feb 12 solo hc race", etc.) parser puts these ids listview.
the problem i'm having these ids alter on time. have own json urls need parse based on league id. example, if league id "default" need take id , set url:
"api.pathofexile.com/leagues/"+ id +"?ladder=1&ladderoffset=1&ladderlimit=2" http://api.pathofexile.com/leagues/default?ladder=1&ladderoffset=1&ladderlimit=2
basically, want able click id in listview , have update url , parse new info separate list.
here have json parser:
class="lang-java prettyprint-override">public class leagues extends listactivity { public static string url = "http://api.pathofexile.com/leagues?type=all"; private static final string tag_id = "id"; static inputstream = null; static jsonobject jobj = null; static string json = ""; jsonarray leagues = null; public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); //setcontentview(r.layout.list_item); new getjsontask().execute(""); } protected class getjsontask extends asynctask<string, integer, arraylist<hashmap<string, string>>> { protected arraylist<hashmap<string, string>> leagueslist; private final progressdialog dialog = new progressdialog(leagues.this); // onpreexecute method protected void onpreexecute() { this.dialog.setmessage("loading, please wait.."); this.dialog.setcancelable(false); this.dialog.show(); } // doinbackground method @override protected arraylist<hashmap<string, string>> doinbackground(string... params) { leagueslist = new arraylist<hashmap<string, string>>(); // defaulthttpclient seek { defaulthttpclient httpclient = new defaulthttpclient(); httpget httpget = new httpget(url); httpresponse httpresponse = httpclient.execute(httpget); json = entityutils.tostring(httpresponse.getentity()); // getting array of entries jsonarray leagues = new jsonarray(json); // looping through entries (int = 0; < leagues.length(); i++) { jsonobject league = leagues.getjsonobject(i); string id = league.getstring(tag_id); // creating new hashmap hashmap<string, string> map = new hashmap<string, string>(); // adding each kid node hashmap key => value map.put(tag_id, id); //map.put(tag_event, event); // adding hashlist arraylist leagueslist.add(map); } } grab (jsonexception e) { e.printstacktrace(); } grab (clientprotocolexception e) { // todo auto-generated grab block e.printstacktrace(); } grab (ioexception e) { // todo auto-generated grab block e.printstacktrace(); } homecoming leagueslist; } // onpostexecute method protected void onpostexecute(arraylist<hashmap<string, string>> result) { listadapter adapter = new simpleadapter(getapplicationcontext(), leagueslist, r.layout.league, new string[] { tag_id }, new int[] { r.id.id }); // selecting single listview item listview lv = getlistview(); lv.setadapter(adapter); if (this.dialog.isshowing()) { this.dialog.dismiss(); } } } }
i'm pretty lost @ point. i'm trying possible how have parser set now? can point me in right direction?
thanks.
edit farther clarification:
right parser set league ids listview url "api.pathofexile.com/leagues?type=all" below:
default hardcore feb 12 solo hc race(these items alter on time)
i want able click on "default," alter url "api.pathofexile.com/leagues/default?ladder=1&ladderoffset=1&ladderlimit=2" , parse new listview.
update: using project provided darwind, changed around (using of other code) show json info wanted after clicking on league id.
class="lang-java prettyprint-override">public class leagueview extends listactivity { //private textview leagueid, leaguedescription; private static final string tag_ladder = "ladder"; private static final string tag_entries = "entries"; private static final string tag_online = "online"; private static final string tag_rank = "rank"; private static final string tag_character = "character"; private static final string tag_charname = "name"; private static final string tag_level = "level"; private static final string tag_class = "class"; private static final string tag_experience = "experience"; private static final string tag_account = "account"; private static final string tag_accname = "name"; private static final string tag_accname2 = "name2"; @override public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); //setcontentview(r.layout.league_layout); //leagueid = (textview) findviewbyid(r.id.leagueid); //leaguedescription = (textview) findviewbyid(r.id.leaguedescription); string urltojson = getintent().getextras().getstring("leagueurl"); if (urltojson == null) { toast.maketext(this, "url null!", toast.length_long).show(); } else { new leaguetask().execute(urltojson.replaceall(" ", "%20")); } } protected class leaguetask extends asynctask<string, integer, league> { private final progressdialog dialog = new progressdialog(leagueview.this); protected arraylist<hashmap<string, string>> entrieslist; // onpreexecute method protected void onpreexecute() { this.dialog.setmessage("retrieving ladder information..."); this.dialog.setcancelable(false); this.dialog.show(); } // doinbackground method @override protected league doinbackground(string... params) { league aleague = new league(); entrieslist = new arraylist<hashmap<string, string>>(); // defaulthttpclient seek { defaulthttpclient httpclient = new defaulthttpclient(); httpget httpget = new httpget(params[0]); httpresponse httpresponse = httpclient.execute(httpget); string json = entityutils.tostring(httpresponse.getentity()); // getting jsonobject filled. jsonobject leagueobject = new jsonobject(json); //string id = leagueobject.getstring("id"); //string description = leagueobject.getstring("description"); jsonobject ladder = leagueobject.getjsonobject(tag_ladder); jsonarray entries = ladder.getjsonarray(tag_entries); // looping through entries (int = 0; < entries.length(); i++) { jsonobject ent = entries.getjsonobject(i); // storing each json item in variable string online = ent.getstring(tag_online); if (online == "false") online = "offline"; else online = "online"; string rank = ent.getstring(tag_rank); // pull out character object jsonobject character = ent.getjsonobject(tag_character); string chname = character.getstring(tag_charname); string lvl = character.getstring(tag_level); string cl = character.getstring(tag_class); string xp = character.getstring(tag_experience); // pull out business relationship object jsonobject business relationship = ent.getjsonobject(tag_account); string accname = account.getstring(tag_accname); // creating new hashmap hashmap<string, string> map = new hashmap<string, string>(); // adding each kid node hashmap key => value map.put(tag_online, online); map.put(tag_charname, chname); map.put(tag_accname2, accname); map.put(tag_rank, rank); map.put(tag_level, lvl); map.put(tag_experience, xp); map.put(tag_class, cl); entrieslist.add(map); } // creating league display aleague = new league(); //aleague.setleagueid(id); //aleague.setdescription(description); } grab (jsonexception e) { e.printstacktrace(); } grab (clientprotocolexception e) { // todo auto-generated grab block e.printstacktrace(); } grab (ioexception e) { // todo auto-generated grab block e.printstacktrace(); } homecoming aleague; } // onpostexecute method protected void onpostexecute(league result) { //leagueid.settext(result.getleagueid()); //leaguedescription.settext(result.getdescription()); listadapter adapter = new simpleadapter(getapplicationcontext(), entrieslist, r.layout.league_layout, new string[] { tag_rank, tag_charname, tag_level, tag_experience, tag_class }, new int[] { r.id.rank, r.id.chname, r.id.lvl, r.id.experience, r.id.cl }); // selecting single listview item listview lv = getlistview(); lv.setadapter(adapter); if (this.dialog.isshowing()) { this.dialog.dismiss(); } } } }
ok so, vsnyc pointed out title bit misleading.
the way see it, this:
get list of leagues parsing json array be able click league in list , view supplying info selected league, parsing json object.i went , kanged code , changed bit. instance mentioned in comment, should utilize class represent every league , override tostring
method new kind of object, class used straight in listview
.
as first url you're using: http://api.pathofexile.com/leagues?type=all
returning json array , not single object , objects within array different info returned when selecting specific league, wouldn't recommend utilize same asynctask getting both list of leagues , single league.
instead after you've populated first listview
leagues, create sec view, populated getting info specific league.
to sec view representing 1 league, need implement onitemclicklistener
listview
of first view. in onitemclicklistener
selected leagues name , pass next view in turn contact server , json
object representing given league , parse data.
i've created little project quite lot of code add together reply , take me more time remove unnecessary code, give total working project. ;-)
here's project in zip file:
https://dl.dropbox.com/u/27724371/androidlistviews.zip
let me know if wasn't looking for.
java android json
No comments:
Post a Comment