private void processData(String res, boolean isCache) throws JSONException { JSONObject root = new JSONObject(res); JSONArray array = root.getJSONArray("data"); LinkedHashMap<String, List<PrefixVideo>> tmp = new LinkedHashMap<String, List<PrefixVideo>>(); for (int i = 0; i < array.length(); i++) { JSONObject group = array.getJSONObject(i); String title = group.getString("title"); JSONArray data = group.getJSONArray("data"); for (int j = 0; j < data.length(); j++) { final PrefixVideo info = new PrefixVideo(data.getJSONObject(j), title); info.downloadListener = this; if (!isCache) { PrefixVideo p = PrefixVideo.query(info.id); if (p == null) { PrefixVideo.insert(info); } else { info.total = p.total; } } info.initialize(); List<PrefixVideo> items = tmp.get(title); if (items == null) { items = new LinkedList<PrefixVideo>(); tmp.put(title, items); } items.add(info); if (!info.coverFile.exists()) { App.THREAD.execute( new Runnable() { @Override public void run() { info.downloadCoverImage(); handler.sendEmptyMessage(HANDLER_UPDATE); } }); } } } List<PrefixVideoGroup> groups = new LinkedList<PrefixVideoGroup>(); Set<Entry<String, List<PrefixVideo>>> entrys = tmp.entrySet(); for (Entry<String, List<PrefixVideo>> i : entrys) { PrefixVideoGroup group = new PrefixVideoGroup(i.getValue().get(0).type, i.getKey()); group.items.addAll(i.getValue()); groups.add(group); } synchronized (adapter.dataSource) { adapter.dataSource.clear(); adapter.dataSource.addAll(groups); } handler.sendEmptyMessage(HANDLER_UPDATE); }
void obtainList() { String cache = App.CACHE.read(CACHE_KEY); if (!TextUtils.isEmpty(cache)) { try { processData(cache, true); } catch (JSONException e) { ELog.e("Exception:" + e.getMessage()); e.printStackTrace(); } } DefaultHttpCallback callback = new DefaultHttpCallback( new DefaultHttpCallback.EventListener() { @Override public void onSuccess(HttpResult result) { try { String res = result.getResult(); ELog.i(res); processData(res, false); App.CACHE.save(CACHE_KEY, res); } catch (Exception e) { ELog.e("Exception:" + e.getMessage()); e.printStackTrace(); } } @Override public void onForceClose(ExceptionHttpResult result) {} @Override public void onException(ExceptionHttpResult result) {} }); HttpRequest req = new HttpRequest(Constants.GET_MV_PREFIX, null, HttpRequest.Method.GET, callback); App.THREAD.execute(req); }
private void download(PrefixVideo info) { info.state = PrefixVideo.STATE_DOWNLOADING; adapter.notifyDataSetChanged(); App.THREAD.execute(info.downloadTask); }