1 网络请求
public class HttpUtils { public static String doGet(String urlStr){ HttpURLConnection conn = null; InputStream is = null; ByteArrayOutputStream baos = null; try { URL url = new URL(urlStr); conn = (HttpURLConnection) url.openConnection(); conn.setReadTimeout(5000); conn.setConnectTimeout(5000); conn.setRequestMethod("GET"); if(conn.getResponseCode()==200){ is = conn.getInputStream(); baos = new ByteArrayOutputStream(); int len =-1; byte[] buf = new byte[512]; while ((len = is.read(buf))!=-1){ baos.write(buf,0,len); baos.flush(); } Log.d(MainActivity.Tag, "doGet: "+baos.toString()); return baos.toString(); } } catch (MalformedURLException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); }finally { if(baos!=null){ try { baos.close(); } catch (IOException e) { e.printStackTrace(); } } if(is!=null){ try { is.close(); } catch (IOException e) { e.printStackTrace(); } } if(conn!=null) { conn.disconnect(); } } return null; } }
2 业务逻辑
public class ChapterBiz { public void loadDatas(final Context context, final CallBack callback, boolean useCache){ AsyncTask<Boolean,Void,List<Chapter>> asyncTask = new AsyncTask<Boolean, Void, List<Chapter>>() { private Exception ex; @Override protected List<Chapter> doInBackground(Boolean... booleans) { boolean isUseCache = booleans[0]; List<Chapter> chapterList = new ArrayList<Chapter>(); try { if(isUseCache){ //Todo load datas from db } //Todo load from net if(chapterList.isEmpty()){ List<Chapter> chapterListFromNet = loadFromNet(context); chapterList.addAll(chapterListFromNet); } }catch (Exception ex){ ex.printStackTrace(); this.ex = ex; } return chapterList; } @Override protected void onPostExecute(List<Chapter> chapters) { if(ex!=null){ callback.onFailed(ex); return; } callback.onSuccess(chapters); } }; asyncTask.execute(useCache); } private List<Chapter> loadFromNet(Context context) { String Url ="http://www.imooc.com/api/expandablelistview"; List<Chapter> chapters=new ArrayList<>(); //1 发请求获取 string数据 String content =HttpUtils.doGet(Url); Log.d(MainActivity.Tag,"获取网络数据成功"+content); //2 content-->List<Chapter> if (content!=null){ chapters = parseContent(content); } return chapters; } private List<Chapter> parseContent(String content) { List<Chapter> chapterList = new ArrayList<Chapter>(); try { JSONObject root = new JSONObject(content); int errorCode =root.getInt("errorCode"); if(errorCode==0){ JSONArray data = root.getJSONArray("data"); for (int i = 0; i < data.length(); i++) { JSONObject chapterJsonObj = data.getJSONObject(i); int id =chapterJsonObj.optInt("id"); String name =chapterJsonObj.optString("name"); Chapter chapter = new Chapter(id,name); chapterList.add(chapter); JSONArray childrenJsonArray=chapterJsonObj.optJSONArray("children"); if(childrenJsonArray!=null){ for (int j = 0; j <childrenJsonArray.length() ; j++) { JSONObject childJsonObj = childrenJsonArray.getJSONObject(j); int childId =childJsonObj.getInt("id"); int pi = childJsonObj.getInt("pid"); String childName =childJsonObj.getString("name"); ChapterItem chapterItem = new ChapterItem(childId,childName); chapter.addChild(chapterItem); } } } } } catch (JSONException e) { e.printStackTrace(); } return chapterList; } public static interface CallBack{ void onSuccess(List<Chapter> chapters); void onFailed(Exception ex); } }
3获取数据改变expandLisView
private void loadDatas() { mchapterBiz.loadDatas(this, new ChapterBiz.CallBack() { @Override public void onSuccess(List<Chapter> chapters) { mDatas.addAll(chapters); Log.d(Tag, "onSuccess: "+chapterAdapter); chapterAdapter.notifyDataSetChanged(); } @Override public void onFailed(Exception ex) { ex.printStackTrace(); } }, false); }