你好,我很难理解如何用我的 JSON 解析器做我想做的事。我目前正在解析位于http://api.pathofexile.com/leagues?type=all的数据以提取联赛 ID(目前它们是“默认”、“硬核”、“2 月 12 日 Solo HC Race”,等)我的解析器当前将这些 id 放入列表视图中。
我遇到的问题是这些 id 会随着时间而改变。他们也有自己的 JSON url,我需要根据联赛 id 对其进行解析。例如,如果联赛 id 是“默认”,我需要获取该 id 并将其放入 url:
"api.pathofexile.com/leagues/"+ id +"?ladder=1&ladderOffset=1&ladderLimit=2" 获取http://api.pathofexile.com/leagues/Default?ladder=1&ladderOffset=1&ladderLimit=2
基本上,我希望能够单击列表视图中的 id 并让它更新 url 并将新数据解析到单独的列表中。
这是我的 JSON 解析器:
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();
}
}
}
}
在这一点上我很迷茫。我现在尝试设置解析器的方式是否可行?谁能指出我正确的方向?
谢谢。
编辑进一步澄清:
现在我的解析器会将联赛 id 从 url "api.pathofexile.com/leagues?type=all" 放入 ListView 中,如下所示:
- 默认
- 铁杆
- 2 月 12 日个人 HC 比赛
(这些项目会随着时间而改变)
我希望能够单击“默认”,这会将 url 更改为“api.pathofexile.com/leagues/Default?ladder=1&ladderOffset=1&ladderLimit=2”并将其解析为新的 ListView。
更新: 使用 Darwind 提供的项目,我改变了它(使用我的一些其他代码)以在单击联赛 ID 后显示我想要的 JSON 数据。
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();
}
}
}
}