我一直在研究一个Android应用程序,它读取大小约3MB的JSON文件,并在屏幕上显示一些数据。但是,加载需要花费大量时间,并且大部分时间用于生成包含整个文件的JSON对象。我已经看了几个基准测试,但是我远远不及标准JSON解析器的结果说我应该这么快。构建JSON对象大约需要20秒,代码如下所示。
private String obtainLeaderboardsData(String host) {
StringBuilder builder = new StringBuilder();
HttpClient client = new DefaultHttpClient();
String URL = "http://" + host + "/api/wow/challenge/region?locale=en_GB";
HttpGet httpGet = new HttpGet("http://eu.battle.net/api/wow/challenge/region?locale=en_GB");
try {
HttpResponse response = client.execute(httpGet);
StatusLine statusLine = response.getStatusLine();
int statusCode = statusLine.getStatusCode();
if (statusCode == 200) {
HttpEntity entity = response.getEntity();
InputStream content = entity.getContent();
BufferedReader reader = new BufferedReader(new InputStreamReader(content));
String line;
while ((line = reader.readLine()) != null) {
builder.append(line);
}
} else {
Log.e(MainActivity.class.toString(), "Failed to download file");
}
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return builder.toString();
}
private class LoadData extends AsyncTask<Void, Void, ArrayList<Run>> {
@Override
protected ArrayList<Run> doInBackground(Void... arg0) {
ArrayList<Run> runs = new ArrayList<Run>();
String[] hosts = {"eu.battle.net"};
for(String host : hosts) {
String leaderboardsData = obtainLeaderboardsData(host);
try {
JSONObject leaderboard = new JSONObject(leaderboardsData);
JSONArray challenges = leaderboard.getJSONArray("challenge");
for(int i = 0; i < challenges.length(); i++) {
JSONObject challenge = challenges.getJSONObject(i);
JSONObject o_dungeon = challenge.getJSONObject("map");
String dungeon = o_dungeon.getString("name");
JSONArray groups = challenge.getJSONArray("groups");
for(int j = 0; j < groups.length(); j++) {
JSONObject group = groups.getJSONObject(j);
Run run = new Run(group, dungeon);
if(run.hours_passed > 0 && run.hours_passed < 72) {
runs.add(run);
}
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
Collections.sort(runs, new CustomComparator());
return runs;
}
@Override
protected void onPostExecute(ArrayList<Run> runs) {
ListView allList = (ListView) findViewById(R.id.allList);
ArrayList<HashMap<String, String>> list = new ArrayList<HashMap<String, String>>(runs.size());
for (Run run : runs) {
HashMap<String, String> item = new HashMap<String, String>();
item.put("rank_dungeon_timer_passed", "#" + run.ranking + " " + run.dungeon + " " + run.timer + " (" + run.hours_passed + " hours ago)");
String members = "";
for(int i = 0; i < run.members.size(); i++) {
members = members + run.members.get(i) + "\n";
}
item.put("members", members);
list.add(item);
}
String[] from = new String[] {"rank_dungeon_timer_passed", "members"};
int[] to = new int[] {android.R.id.text1, android.R.id.text2};
int nativeLayout = android.R.layout.two_line_list_item;
allList.setAdapter(new SimpleAdapter(MainActivity.this, list, nativeLayout , from, to));
}
}
花费时间最长的行是:
JSONObject leaderboard = new JSONObject(leaderboardsData);
这是一个在完整的JSON文件中读取的错误方法吗?
编辑:由于我没有使用大部分数据,我是否可以通过网址查询添加参数来过滤掉一些我不需要的字段?
答案 0 :(得分:2)
我猜它正在解析那个电话中的信息而不是你需要的信息。我通过Jackson完成了所有JSON解析。它设置起来非常快速和简单。如果你有时间测试它,它可能会让你的生活从现在开始变得更容易。