从服务器到本地数据的存储数据sqlite Android

时间:2016-11-06 10:13:01

标签: android sqlite

我有一个回收站视图,我从服务器获取数据,其工作正常,问题是当我尝试将数据从服务器存储到本地数据库时使用sqlite无法正常工作,我的问题是我可以存储列表目录到SQL表??我试图从列表中获取字符串并将其存储到本地,但它不起作用,这是我的代码希望有人有想法告诉我:)

 public void getRetrofitObject() {
    Retrofit retrofit = new Retrofit.Builder()
            .baseUrl(BASE_URL)
            .addConverterFactory(GsonConverterFactory.create())
            .build();
    APIService service = retrofit.create(APIService.class);
    Call<Result> call = service.getresults();
    call.enqueue(new Callback<Result>() {
        @Override
        public void onResponse(Call<Result> call, Response<Result> response) {
            results = response.body().getResults();
            todoAdapter = new TodoRecyclerAdapter(this,results);
            todoRecyclerView.setAdapter(todoAdapter);
            for (int i = 0 ; i<results.size();i++) {
                 insert(response.body().getResults().get(i).getTODO_TITLE().toString(),response.body().getResults().get(i).getPriority().toString());
            }
        }
        @Override
        public void onFailure(Call<Result> call, Throwable t) {
            Log.d("onFailure", t.toString());
        }});}
 public  void  insert (String x , String y )
{
    db.insertIntoDB(x,y);

}

这是Sqlite插入方法

 public void insertIntoDB(String Title,String Priority){
    Log.d("insert", "before insert");
    SQLiteDatabase db = this.getWritableDatabase();
    ContentValues values = new ContentValues();
    values.put(TODO_TITLE, Title);
    values.put(priority, Priority);
    db.insert(TODO_TABLE, null, values);
    db.close();
}

修改 当我试图从服务器存储字符串像这样

   public void getRetrofitObject() {
    Retrofit retrofit = new Retrofit.Builder()
            .baseUrl(BASE_URL)
            .addConverterFactory(GsonConverterFactory.create())
            .build();
    APIService service = retrofit.create(APIService.class);
    Call<Result> call = service.getresults();
    call.enqueue(new Callback<Result>() {
        @Override
        public void onResponse(Call<Result> call, Response<Result> response) {
            results = response.body().getResults();
            todoAdapter = new TodoRecyclerAdapter(this,results);
            todoRecyclerView.setAdapter(todoAdapter);
            for (int i = 0 ; i<results.size();i++) {

             String x =   results.get(i).getTODO_TITLE().toString(); //this 
                String y =   results.get(i).getPriority().toString();


                db.insertIntoDB(x,y);
            }
        }
        @Override
        public void onFailure(Call<Result> call, Throwable t) {
            Log.d("onFailure", t.toString());
        }});}

我认为这个错误

 FATAL EXCEPTION: main
                                                                         java.lang.NullPointerException
                                                                             at com.example.todo.loginretroft.Activites.TodoList$3.onResponse(TodoList.java:151)

1 个答案:

答案 0 :(得分:0)

在将任何数据插入数据库之前,您应该创建数据库:) 这是一个很好的简单示例 - https://developer.android.com/training/basics/data-storage/databases.html

它看起来像这样:

public class FeedReaderDbHelper extends SQLiteOpenHelper {

    // If you change the database schema, you must increment the database version.
    public static final int DATABASE_VERSION = 1;
    public static final String DATABASE_NAME = "FeedReader.db";

    public FeedReaderDbHelper(Context context) {
        super(context, DATABASE_NAME, null, DATABASE_VERSION);
}

    public void onCreate(SQLiteDatabase db) {
        db.execSQL(SQL_CREATE_ENTRIES);
}
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        // This database is only a cache for online data, so its upgrade policy is
        // to simply to discard the data and start over
        db.execSQL(SQL_DELETE_ENTRIES);
        onCreate(db);
}

    public void onDowngrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        onUpgrade(db, oldVersion, newVersion);
}

}