历史上的今天
意义:通过输入日期获取网络新闻,已经搜索过的直接保存本地
布局搭建
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.example.lesson13_5_databasecatch.MainActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:orientation="horizontal">
<EditText
android:id="@+id/et_date"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="请输入日期 MM-dd" />
<Button
android:id="@+id/btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="请求" />
</LinearLayout>
<ListView
android:id="@+id/lv"
android:layout_width="match_parent"
item布局
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="10dp">
<TextView
android:id="@+id/tv_title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:lines="1"
android:text="标题"
android:textColor="@android:color/black"
android:textSize="18sp" />
<TextView
android:id="@+id/tv_time"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="right"
android:text="2017-08-24" />
<ImageView
android:id="@+id/pic"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:adjustViewBounds="true"
android:src="@mipmap/ic_launcher" />
```**Model**
<div class="se-preview-section-delimiter"></div>
package com.example.lesson13_5_databasecatch;
import android.util.Log;
import com.google.gson.Gson;
import java.io.IOException;
import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.FormBody;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;
/**
* Created by ASUS on 2017/8/24.
* berlin 2017/08/25
*/
public class Model {
public static final String URL = “http://route.showapi.com/119-42“;
public static final String APPID = "13074";
public static final String SIGN = "ea5b4bf2e140498bb772d1bf2a51a7a0";
public static void getHistoryToday(String date, final BeanCallback callback) {
//加入
OkHttpClient client = new OkHttpClient();
RequestBody body = null;
if (date == null) {
//当天
body = new FormBody.Builder().add("showapi_appid", APPID).add("showapi_sign", SIGN).build();
} else {
//历史数据(date)
body = new FormBody.Builder().add("showapi_appid", APPID).add("showapi_sign", SIGN).add("date", date).build();
}//请求网络
Request request = new Request.Builder().url(URL).post(body).build();
Call call = client.newCall(request);
//异步解析
call.enqueue(new Callback() {
@Override
public void onFailure(Call call, IOException e) {
Log.e("TAG", "-------网络请求异常");
}
@Override
public void onResponse(Call call, Response response) throws IOException {
// Log.e(“TAG”, “——-” + response.body().string());
Gson gson = new Gson();
//
TodayBean bean = gson.fromJson(response.body().string(), TodayBean.class);
Log.e(“TAG”, “———–” + bean.toString());
**TodayBean**
<div class="se-preview-section-delimiter"></div>
package com.example.lesson13_5_databasecatch;
import java.util.List;
/**
* Created by ASUS on 2017/8/24.
*/
public class TodayBean {
@Override
public String toString() {
return "TodayBean{" +
"showapi_res_body=" + showapi_res_body +
", showapi_res_code=" + showapi_res_code +
", showapi_res_error='" + showapi_res_error + '\'' +
'}';
}
private ShowapiResBodyBean showapi_res_body;
private int showapi_res_code;
private String showapi_res_error;
public ShowapiResBodyBean getShowapi_res_body() {
return showapi_res_body;
}
public void setShowapi_res_body(ShowapiResBodyBean showapi_res_body) {
this.showapi_res_body = showapi_res_body;
}
public int getShowapi_res_code() {
return showapi_res_code;
}
public void setShowapi_res_code(int showapi_res_code) {
this.showapi_res_code = showapi_res_code;
}
public String getShowapi_res_error() {
return showapi_res_error;
}
public void setShowapi_res_error(String showapi_res_error) {
this.showapi_res_error = showapi_res_error;
}
public static class ShowapiResBodyBean {
@Override
public String toString() {
return "ShowapiResBodyBean{" +
"ret_code=" + ret_code +
", list=" + list +
'}';
}
private int ret_code;
private List<ListBean> list;
public int getRet_code() {
return ret_code;
}
public void setRet_code(int ret_code) {
this.ret_code = ret_code;
}
public List<ListBean> getList() {
return list;
}
public void setList(List<ListBean> list) {
this.list = list;
}
public static class ListBean {
@Override
public String toString() {
return "ListBean{" +
"day=" + day +
", img='" + img + '\'' +
", month=" + month +
", title='" + title + '\'' +
", year='" + year + '\'' +
'}';
}
private int day;
private String img;
private int month;
private String title;
private String year;
public int getDay() {
return day;
}
public void setDay(int day) {
this.day = day;
}
public String getImg() {
return img;
}
public void setImg(String img) {
this.img = img;
}
public int getMonth() {
return month;
}
public void setMonth(int month) {
this.month = month;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getYear() {
return year;
}
public void setYear(String year) {
this.year = year;
}
}
}
}
**TodayOpenHelper**
<div class="se-preview-section-delimiter"></div>
package com.example.lesson13_5_databasecatch;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
/**
* Created by ASUS on 2017/8/24.
*/
public class TodayOpenHelper extends SQLiteOpenHelper {
public static final String DB_NAME = "databasecatch.db";
public static final String TABLE_NAME = "history";
public static final int VERSION = 1;
public static final String ID = "_id";
public static final String IMG = "_img";
public static final String DAY = "_day";
public static final String MONTH = "_month";
public static final String YEAR = "_year";
public static final String TITLE = "_title";
//create table user()
public static final String CREATE = "create table " + TABLE_NAME + "(" + ID + " integer primary key autoincrement," +
IMG + " text," +
DAY + " integer not null," +
MONTH + " integer not null," +
YEAR + " integer not null," +
TITLE + " text not null" +
");";
public TodayOpenHelper(Context context) {
super(context, DB_NAME, null, VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
//db?? :(SQLiteDatabase db)
db.execSQL(CREATE);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
}
}
**TodayDAO**
<div class="se-preview-section-delimiter"></div>
import android.util.Log;
import java.util.ArrayList;
import java.util.List;
/**
* Created by ASUS on 2017/8/24.
*/
public class TodayDAO {
TodayOpenHelper helper;
public TodayDAO(Context context) {
helper = new TodayOpenHelper(context);
}
//读
public List<TodayBean.ShowapiResBodyBean.ListBean> queryFormDate(int month, int day) {
SQLiteDatabase readableDatabase = helper.getReadableDatabase();
Cursor query = readableDatabase.query(TodayOpenHelper.TABLE_NAME, null, TodayOpenHelper.MONTH + "=? and " + TodayOpenHelper.DAY + "=?", new String[]{month + "", day + ""}, null, null, null);
List<TodayBean.ShowapiResBodyBean.ListBean> list = new ArrayList<>();
while (query.moveToNext()) {
String title = query.getString(query.getColumnIndex(TodayOpenHelper.TITLE));
String img = query.getString(query.getColumnIndex(TodayOpenHelper.IMG));
String year = query.getString(query.getColumnIndex(TodayOpenHelper.YEAR));
TodayBean.ShowapiResBodyBean.ListBean bean = new TodayBean.ShowapiResBodyBean.ListBean();
bean.setDay(day);
bean.setImg(img);
bean.setMonth(month);
bean.setYear(year);
bean.setTitle(title);
list.add(bean);
}
readableDatabase.close();
return list;
}
//存
public void addBean(TodayBean.ShowapiResBodyBean.ListBean bean) {
//
SQLiteDatabase readableDatabase = helper.getReadableDatabase();
ContentValues values = new ContentValues();
values.put(TodayOpenHelper.DAY, bean.getDay());
values.put(TodayOpenHelper.MONTH, bean.getMonth());
values.put(TodayOpenHelper.YEAR, bean.getYear());
values.put(TodayOpenHelper.TITLE, bean.getTitle());
if (bean.getImg() != null) {
values.put(TodayOpenHelper.IMG, bean.getImg());
}
readableDatabase.insert(TodayOpenHelper.TABLE_NAME, null, values);
//关闭
readableDatabase.close();
}
public void addBeans(List<TodayBean.ShowapiResBodyBean.ListBean> beans) {
Log.e("TAG", "------------" + beans.toString());
//foreach 遍历bean 目录文件并添加
for (TodayBean.ShowapiResBodyBean.ListBean bean : beans) {
addBean(bean);
}
}
}
**TodayAdapter**
<div class="se-preview-section-delimiter"></div>
package com.example.lesson13_5_databasecatch;
import android.content.Context;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import com.bumptech.glide.Glide;
import java.util.List;
import butterknife.BindView;
import butterknife.ButterKnife;
/**
* Created by ASUS on 2017/8/25.
*/
public class TodayAdapter extends BaseAdapter {
Context context;
List