本文的实例是基于《Android系统回顾(八):网络通信(一)》中的项目添加而来的。
实现的效果是:在PostActivity里,当美女们加载完成之后,把她们的信息异步地写入数据库中。
/MeiNvLiuLanQi/src/com/example/meinvliulanqi/basic_service/IDBOperate.java文件:
package com.example.meinvliulanqi.basic_service;
import android.database.sqlite.SQLiteDatabase;
public interface IDBOperate
{
public boolean doOperate(SQLiteDatabase db);
public void onDBOpreaterFinished(Boolean result);
}
/MeiNvLiuLanQi/src/com/example/meinvliulanqi/basic_service/MeinvDbHelper.java文件:
package com.example.meinvliulanqi.basic_service;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
public class MeinvDbHelper extends SQLiteOpenHelper
{
// 数据库的基本参数
public static final String DB_NAME = "meinvDB.db";
// --------------------------------------------------------------
// 数据表的基本描述,表名和表中的列名
public static final String DB_TABLE_MEINV_INFO = "tab_meinv_info";
public static final String KEY_ID = "id";
public static final String KEY_HEAD_PATH = "head_path";
public static final String KEY_NAME = "name";
public static final String KEY_BIRTHDAY = "birthday";
public static final String KEY_ADDRESS = "address";
public static final String KEY_STATURE = "stature";
public static final String KEY_WEIGHT = "weight";
public static final String KEY_BUST = "bust";
public static final String KEY_WAISTLINE = "waistline";
public static final String KEY_HIP = "hip";
// --------------------------------------------------------------
// 创建数据库的SQL语句
private static final String DATABASE_CREATE = String.format(
"create table %s (%s integer primary key autoincrement, %s text not null, %s text not null, %s text not null, %s text not null, %s float not null, %s float not null, %s text not null, %s float not
null, %s float not null)",
DB_TABLE_MEINV_INFO,
KEY_ID,
KEY_HEAD_PATH,
KEY_NAME,
KEY_BIRTHDAY,
KEY_ADDRESS,
KEY_STATURE,
KEY_WEIGHT,
KEY_BUST,
KEY_WAISTLINE,
KEY_HIP);
// 定义构造函数
public MeinvDbHelper(Context context, int version)
{
// SQLiteOpenHelper(Context context, String name, CursorFactory factory,
// int version)
// 第一个参数:创建数据库的上下文,比如,如果传入的是com.example.meinvliulanqi.ui.MainActivity的实例,那数据库文件在用户手机中的实际存储位置将是:/data/data/com.example.meinvliulanqi.ui/databases
// 第二个参数:欲创建的数据库的名称,比如名称是meinvDB.db,那么在数据库成功创建完成之后/data/data/com.example.meinvliulanqi.ui/databases路径下就会有meinvDB.db、meinvDB.db-journal两个文件,一个是数据库文件,另一个是日志文件
// 第三、第四个参数:CursorFactory用于产生Cursor对象,version为创建数据库的版本
super(context, DB_NAME, null, version);
}
// 重写onCreate函数,当磁盘上不存在数据库时负责创建新数据库
@Override
public void onCreate(SQLiteDatabase arg0)
{
arg0.execSQL(DATABASE_CREATE);
}
// 重写onUpgrade函数,当存在数据库版本不一致时,升级磁盘上的数据库到当前版本,覆盖安装后可能会出现这种情况
@Override
public void onUpgrade(SQLiteDatabase arg0, int arg1, int arg2)
{
arg0.execSQL("DROP TABLE IF EXIST " + DB_TABLE_MEINV_INFO);
onCreate(arg0);
}
}
/MeiNvLiuLanQi/src/com/example/meinvliulanqi/basic_service/Task_operate_db.java文件:
package com.example.meinvliulanqi.basic_service;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.os.AsyncTask;
import android.util.Log;
public class Task_operate_db extends AsyncTask<Void, Integer, Boolean>
{
private Context context;
private int db_version;
private IDBOperate operate;
public Task_operate_db(Context _con, int _ver, IDBOperate _ope)
{
context = _con;
db_version = _ver;
operate = _ope;
}
@Override
protected Boolean doInBackground(Void... para)
{
MeinvDbHelper helper = new MeinvDbHelper(context, db_version);
SQLiteDatabase db = null;
try
{
db = helper.getWritableDatabase();
return operate.doOperate(db);
} catch (Exception e)
{
Log.e("thinking-------", e.getMessage());
return false;
}
}
@Override
protected void onProgressUpdate(Integer... values)
{
}
@Override
protected void onPostExecute(Boolean result)
{
this.operate.onDBOpreaterFinished(result);
}
}
/MeiNvLiuLanQi/src/com/example/meinvliulanqi/ui/PostActivity.java文件:
package com.example.meinvliulanqi.ui;
import org.json.JSONArray;
import org.json.JSONException;
import com.example.meinvliulanqi.basic_service.IDBOperate;
import com.example.meinvliulanqi.basic_service.IGetdata;
import com.example.meinvliulanqi.basic_service.MeinvDbHelper;
import com.example.meinvliulanqi.basic_service.Task_getimgs;
import com.example.meinvliulanqi.basic_service.Task_operate_db;
import com.example.meinvliulanqi.basic_service.Task_postdata;
import android.app.Activity;
import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.util.Log;
import android.widget.GridView;
public class PostActivity extends Activity implements IGetdata, IDBOperate
{
private MeinvBaseAdapter ada;
private GridView gid_meinv;
private JSONArray meinvs;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_post);
Task_postdata task = new Task_postdata(this);
task.execute(new String[] {
"http://192.168.10.111:8011/MeinvInfo.ashx",
"{\"method\":\"get_meinv\",\"check_num\":1001}" });
}
@Override
public void onGetInfoData(String info)
{
try
{
meinvs = new JSONArray(info);
initUIInfo();
} catch (JSONException e)
{
Log.e("thinking-------", e.getMessage());
}
}
private void initUIInfo()
{
gid_meinv = (GridView) findViewById(R.id.gid_meinv_post);
ada = new MeinvBaseAdapter(meinvs, new Bitmap[meinvs.length()], this);
gid_meinv.setAdapter(ada);
String[][] img_infos = new String[meinvs.length()][2];
for (int i = 0; i < img_infos.length; i++)
{
try
{
img_infos[i][0] = meinvs.getJSONObject(i)
.getString("photoPath");
} catch (JSONException e)
{
img_infos[i][0] = "";
Log.e("thinking-------", e.getMessage());
}
img_infos[i][1] = i + "";
}
Task_getimgs task = new Task_getimgs(this);
task.execute(img_infos);
Task_operate_db task_db = new Task_operate_db(this, 1, this);
task_db.execute();
}
@Override
public void onGetImgData(Bitmap[] img)
{
ada.setImgs(img);
}
@Override
public boolean doOperate(SQLiteDatabase db)
{
try
{
ContentValues newValues = new ContentValues();
for (int i = 0; i < meinvs.length(); i++)
{
newValues.put(MeinvDbHelper.KEY_NAME, meinvs.getJSONObject(i)
.getString("name"));
newValues.put(MeinvDbHelper.KEY_ADDRESS, meinvs
.getJSONObject(i).getString("address"));
newValues.put(MeinvDbHelper.KEY_HEAD_PATH,
"/sdcard/meinvliulanqi/" + i + ".jpg");
newValues.put(MeinvDbHelper.KEY_BUST, meinvs.getJSONObject(i)
.getString("bust"));
newValues.put(MeinvDbHelper.KEY_HIP, meinvs.getJSONObject(i)
.getDouble("hip"));
newValues.put(MeinvDbHelper.KEY_BIRTHDAY,
meinvs.getJSONObject(i).getString("birthday"));
newValues.put(MeinvDbHelper.KEY_STATURE, meinvs
.getJSONObject(i).getDouble("stature"));
newValues.put(MeinvDbHelper.KEY_WEIGHT, meinvs.getJSONObject(i)
.getDouble("weight"));
newValues.put(MeinvDbHelper.KEY_WAISTLINE, meinvs
.getJSONObject(i).getDouble("waistline"));
db.insert(MeinvDbHelper.DB_TABLE_MEINV_INFO, null, newValues);
}
return true;
} catch (Exception e)
{
Log.e("thinking-------", e.getMessage());
return false;
}
}
@Override
public void onDBOpreaterFinished(Boolean result)
{
// TODO Auto-generated method stub
}
}
实现的效果:
当页面加载完毕之后,adb在/data/data/com.example.meinvliulanqi.ui/databases路径下可以看到:
把这个meinvDB.db文件拷出来,用Sqlite3工具查看可以看到:
结果表明数据已经写入数据库了。