【ListViewJSON】【工具类的功能与实现】
接上一篇博客,这篇博客的重点是分析listviewjson项目中的工具类的功能,以及如何更好地使用这套工具。
项目源码结构图:
假设现在有一个新的项目,同样是解析json数据,并将其显示到listview中。
那么现在考虑一下如何在两个项目之间进行移植。
首先com.demo.app.common是可以直接进行移植的。
那么需要重新写的就是1、bean 2、adapter 3、以及所有和获取列表数据有关、将数据加载到listvie文中有关的类。
MainActivity中通过
NewsList list = appContext.getNewsList();
获取列表数据。
而再看appContext中的getNewsList()方法:
public NewsList getNewsList() throws AppException { NewsList list = null; String key = "newslist_"; if (isNetworkConnected() && !isReadDataCache(key)) { try { list = ApiClient.getNewsList(this); } catch (AppException e) { list = (NewsList) readObject(key); if (list == null) throw e; } } else { list = (NewsList) readObject(key); if (list == null) list = new NewsList(); } return list; }
使用的是ApiClient.getNewsList()方法,注意的是ApiClient的getNewsList是一个类方法,具体如下:
public static NewsList getNewsList(AppContext appContext) throws AppException { String newUrl = URLs.NEWS_LIST; try { System.out.println("获取新闻列表:" + newUrl); System.out.println(http_get(appContext,newUrl)); return NewsList.parse(StringUtils.toJSONArray(http_get(appContext, newUrl))); } catch (Exception e) { System.out.println(e); if (e instanceof AppException) throw (AppException) e; throw AppException.network(e); } }
分析ApiClient.getNewsList,可以看出它是先通过http_get获取一个字符串:
private static String http_get(AppContext appContext, String url) throws AppException { // System.out.println("get_url==> "+url); String cookie = getCookie(appContext); String userAgent = getUserAgent(appContext); HttpClient httpClient = null; GetMethod httpGet = null; String responseBody = ""; int time = 0; do { try { httpClient = getHttpClient(); httpGet = getHttpGet(url, cookie, userAgent); int statusCode = httpClient.executeMethod(httpGet); if (statusCode != HttpStatus.SC_OK) { throw AppException.http(statusCode); } responseBody = httpGet.getResponseBodyAsString(); // System.out.println("XMLDATA=====>"+responseBody); break; } catch (HttpException e) { time++; if (time < RETRY_TIME) { try { Thread.sleep(1000); } catch (InterruptedException e1) { } continue; } // 发生致命的异常,可能是协议不对或者返回的内容有问题 e.printStackTrace(); throw AppException.http(e); } catch (IOException e) { time++; if (time < RETRY_TIME) { try { Thread.sleep(1000); } catch (InterruptedException e1) { } continue; } // 发生网络异常 // e.printStackTrace(); throw AppException.network(e); } finally { // 释放连接 httpGet.releaseConnection(); httpClient = null; } } while (time < RETRY_TIME); responseBody = responseBody.replaceAll("\\p{Cntrl}", ""); return responseBody; }
在通过StringUtils.toJSONArray将其转换为一个json数组:
public static JSONArray toJSONArray(String json) throws JSONException { if (!isEmpty(json)) { // if (json.indexOf("[") == 0) { // json = json.substring(1, json.length()); // } // if (json.lastIndexOf("]") == json.length()) { // json = json.substring(0, json.length() - 1); // } } return new JSONArray(json); }
最后通过NewsList的parse将json数据放到NewsList中。
public static NewsList parse(JSONArray obj) throws IOException, AppException, JSONException { NewsList newslist = new NewsList(); if (null != obj) { newslist.newsCount = obj.length(); for (int i = 0; i < obj.length(); i++) { JSONObject newsJson = obj.getJSONObject(i); News news = new News(); news.setId(newsJson.getString("ID")); news.setTitle(newsJson.getString("Title")); news.setFirstPicUrl(newsJson.getString("FirstPicUrl")); news.setPublishTime(newsJson.getString("PublishTime")); newslist.newslist.add(news); } } return newslist; } }
在这里可以看一下,NewsList和News的成员变量。
News的成员变量。
public class News extends Base { private String title; private String publishTime; private String id; private String firstPicUrl;
NewsList的成员变量:
public class NewsList extends Base { private int catalog; private int pageSize; private int newsCount; private List<News> newslist = new ArrayList<News>();
最后看一下adapter如何将newslist放到listview中,重点就是getView方法。
@Override public View getView(int position, View convertView, ViewGroup parent) { // TODO Auto-generated method stub if (convertView == null) { holder = new ViewHolder(); convertView = inflater.inflate(R.layout.listview_item, null); holder.title = (TextView) convertView .findViewById(R.id.textview_home_listview_title); holder.time = (TextView) convertView .findViewById(R.id.textview_home_listview_time); holder.img = (ImageView) convertView .findViewById(R.id.imageview_home_listview_thumb); convertView.setTag(holder); } else { holder = (ViewHolder) convertView.getTag(); } holder.title.setText(list.get(position).getTitle()); holder.time.setText(list.get(position).getPublishTime()); String imgURL = list.get(position).getFirstPicUrl(); if (imgURL.endsWith("portrait.gif") || StringUtils.isEmpty(imgURL)) { holder.img.setImageResource(R.drawable.umeng_socialize_share_pic); } else { if (!imgURL.contains("http")) { imgURL = URLs.HTTP + URLs.HOST + "/" + imgURL; } bmpManager.loadBitmap(imgURL, holder.img); } return convertView; }
以上。