/** * 获取指定类型的新闻列表 * * @param cid 类型ID * @param newsList 保存新闻信息的集合 * @param startnid 分页 * @param firstTimes 是否第一次加载 */ private int getSpeCateNews( int cid, List<HashMap<String, Object>> newsList, int startnid, Boolean firstTimes) { if (firstTimes) { // 如果是第一次,则清空集合里数据 newsList.clear(); } // 请求URL和字符串 String url = "http://10.0.2.2:8080/web/getSpecifyCategoryNews"; String params = "startnid=" + startnid + "&count=" + NEWSCOUNT + "&cid=" + cid; SyncHttp syncHttp = new SyncHttp(); try { // 以Get方式请求,并获得返回结果 String retStr = syncHttp.httpGet(url, params); JSONObject jsonObject = new JSONObject(retStr); // 获取返回码,0表示成功 int retCode = jsonObject.getInt("ret"); if (0 == retCode) { JSONObject dataObject = jsonObject.getJSONObject("data"); // 获取返回数目 int totalnum = dataObject.getInt("totalnum"); if (totalnum > 0) { // 获取返回新闻集合 JSONArray newslist = dataObject.getJSONArray("newslist"); for (int i = 0; i < newslist.length(); i++) { JSONObject newsObject = (JSONObject) newslist.opt(i); HashMap<String, Object> hashMap = new HashMap<String, Object>(); hashMap.put("nid", newsObject.getInt("nid")); hashMap.put("newslist_item_title", newsObject.getString("title")); hashMap.put("newslist_item_digest", newsObject.getString("digest")); hashMap.put("newslist_item_source", newsObject.getString("source")); hashMap.put("newslist_item_ptime", newsObject.getString("ptime")); hashMap.put("newslist_item_comments", newsObject.getString("commentcount")); newsList.add(hashMap); } return SUCCESS; } else { if (firstTimes) { return NONEWS; } else { return NOMORENEWS; } } } else { return LOADERROR; } } catch (Exception e) { e.printStackTrace(); return LOADERROR; } }
/** * 获取新闻详细信息 * * @return */ private String getNewsBody() { String retStr = "网络连接失败,请稍后再试"; SyncHttp syncHttp = new SyncHttp(); String url = "http://10.0.2.2:8080/web/getNews"; String params = "nid=" + mNid; try { String retString = syncHttp.httpGet(url, params); JSONObject jsonObject = new JSONObject(retString); // 获取返回码,0表示成功 int retCode = jsonObject.getInt("ret"); if (0 == retCode) { JSONObject dataObject = jsonObject.getJSONObject("data"); JSONObject newsObject = dataObject.getJSONObject("news"); retStr = newsObject.getString("body"); } } catch (Exception e) { e.printStackTrace(); } return retStr; }