Hello, I’m having a hard time understanding how to do what I want with my JSON parser. I’m currently parsing the data located at http://api.pathofexile.com/leagues?type=all to pull out the league ids (currently they are “Default”, “Hardcore”, “Feb 12 Solo HC Race”, etc.) My parser currently puts these ids into a listview.
The problem I’m having is that these ids will change over time. They also have their own JSON urls that I need to parse based on the league id. For example, if the league id is “Default” I need to take that id and put it into the url:
“api.pathofexile.com/leagues/”+ id +”?ladder=1&ladderOffset=1&ladderLimit=2″ to get http://api.pathofexile.com/leagues/Default?ladder=1&ladderOffset=1&ladderLimit=2
Basically, I want to be able to click the id in the listview and have it update the url and parse the new data into a separate list.
Here is what I have for my JSON parser:
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 is = 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
try {
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
for (int i = 0; i < 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 child node to HashMap key => value
map.put(TAG_ID, id);
//map.put(TAG_EVENT, event);
// adding HashList to ArrayList
leaguesList.add(map);
}
} catch (JSONException e) {
e.printStackTrace();
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return 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 at this point. Is what I’m trying to do even possible with how I have the parser set up now? Can anybody point me in the right direction?
Thanks.
EDIT for further clarification:
Right now my parser will put the league ids into a ListView from the url “api.pathofexile.com/leagues?type=all” like below:
- Default
- Hardcore
- Feb 12 Solo HC Race
(These items will change over time)
I want to be able to click on “Default,” which will change the url to “api.pathofexile.com/leagues/Default?ladder=1&ladderOffset=1&ladderLimit=2” and parse it into a new ListView.
UPDATE:
Using the project provided by Darwind, I changed it around (using some of my other code) to show the JSON data that I wanted after clicking on the league id.
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 was 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
try {
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpGet HttpGet = new HttpGet(params[0]);
HttpResponse httpResponse = httpClient.execute(HttpGet);
String json = EntityUtils.toString(httpResponse.getEntity());
// Getting a 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
for (int i = 0; i < entries.length(); i++) {
JSONObject ent = entries.getJSONObject(i);
// storing each JSON item in a 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 account object
JSONObject account = ent.getJSONObject(TAG_ACCOUNT);
String accName = account.getString(TAG_ACCNAME);
// creating new HashMap
HashMap<String, String> map = new HashMap<String, String>();
// adding each child node to 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 a league to display
aLeague = new League();
//aLeague.setLeagueId(id);
//aLeague.setDescription(description);
} catch (JSONException e) {
e.printStackTrace();
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return 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, basically as vsnyc pointed out your title is probably a bit misleading.
The way I see it, is this:
I basically went and kanged all your code and changed it a bit. For instance I mentioned in a comment, that you should use a class to represent every league and then override the
toStringmethod for this new kind of object, so that this class could be used directly in yourListView.As the first url you’re using:
http://api.pathofexile.com/leagues?type=allis returning a JSON Array and not a single object and the objects inside this array is different from the data returned when selecting a specific league, I wouldn’t recommend to use the same AsyncTask for getting both the list of leagues and a single league.Instead after you’ve populated the first
ListViewwith leagues, I would create a second view, that is populated by getting data from a specific league.To get to the second view representing one league, you need to implement the
OnItemClickListenerfor theListViewof the first view. In theOnItemClickListeneryou will get the selected leagues name and pass it to the next view which in turn will contact the server and get theJSONobject representing the given league and parse the data.I’ve created a small project as this was quite a lot of code to add to the answer and it would take me more time to remove all unnecessary code, than just to give you a full working project. 😉
Here’s the project in a zip file:
https://dl.dropbox.com/u/27724371/AndroidListViews.zip
Let me know if this wasn’t what you were looking for.