新闻客户端案例

第一次进入新闻客户端需要请求服务器获取新闻数据,做listview的展示,
为了第二次再次打开新闻客户端时能快速显示新闻,需要将数据缓存到数据库中,下次打开可以直接去数据库中获取新闻直接做展示。

总体步骤:
  1.写布局listview ok

  2.找到listview,设置条目的点击事件。 ok

  3.获取数据提供给listview做展示。

    3.1:获取本地数据库缓存的新闻数据,让listview显示。如果没有网络不至于显示空界面。
    3.2:请求服务器获取新闻数据,是一个json字符串,需要解析json,封装到list集合中。提供给listview展示。

public static String newsPath_url = "xxxx";
 //封装新闻的假数据到list中返回
 public static ArrayList<NewsBean> getAllNewsForNetWork(Context context) {
 ArrayList<NewsBean> arrayList = new ArrayList<NewsBean>();
 try{
 //1.请求服务器获取新闻数据
 //获取一个url对象,通过url对象得到一个urlconnnection对象
 URL url = new URL(newsPath_url);
 HttpURLConnection connection = (HttpURLConnection) url.openConnection();
 //设置连接的方式和超时时间
 connection.setRequestMethod("GET");
 connection.setConnectTimeout(10*1000);
 //获取请求响应码
 int code = connection.getResponseCode();
 if(code == 200){
 //获取请求到的流信息
 InputStream inputStream = connection.getInputStream();
 String result = StreamUtils.streamToString(inputStream);

 //2.解析获取的新闻数据到List集合中。

 JSONObject root_json = new JSONObject(result);//将一个字符串封装成一个json对象。
 JSONArray jsonArray = root_json.getJSONArray("newss");//获取root_json中的newss作为jsonArray对象

 for (int i = 0 ;i < jsonArray.length();i++){//循环遍历jsonArray
 JSONObject news_json = jsonArray.getJSONObject(i);//获取一条新闻的json

 NewsBean newsBean = new NewsBean();

 newsBean. id = news_json.getInt("id");
 newsBean. comment = news_json.getInt("comment");//评论数
 newsBean. type = news_json.getInt("type");//新闻的类型,0 :头条 1 :娱乐 2.体育
 newsBean. time = news_json.getString("time");
 newsBean. des = news_json.getString("des");
 newsBean. title = news_json.getString("title");
 newsBean. news_url = news_json.getString("news_url");
 newsBean. icon_url = news_json.getString("icon_url");

 arrayList.add(newsBean);

 } //3.清楚数据库中旧的数据,将新的数据缓存到数据库中
 new NewsDaoUtils(context).delete();
 new NewsDaoUtils(context).saveNews(arrayList);
 }

 }catch (Exception e) {
 e.printStackTrace();
 }
 return arrayList;
 }

    3.3: 获取服务端数据成功后,需要缓存到本地数据库,缓存前需要清空本地数据库。

  4.创建一个Adapter继承BaseAdapter,封装4个方法,需要接收获取的新闻数据 

  5.将adapter设置给listview。