如何在Android上的SQLite中存储JSON对象

时间:2015-11-03 13:19:09

标签: java json sqlite

这是我的SQLite代码

A.do

这是我的主要活动

public class CategoryHelper {

    private static final int DATABASE_VERSION = 2;
    private static final String DATABASE_NAME = "category.db";
    private static final String TABLE_NAME = "tbcategory";
    private static final String KEY_ID = "_id";
    private static final String KEY_NAME = "_name";
    private static final String KEY_AGE = "_age";
    // private static final String EMAIL = "_email";
    private static final String GENDER = "_gender";

    /*
     * private static final String email = "email"; private static final String
     * gender = "gender";
     */

    // http://stackoverflow.com/questions/17441031/want-to-insert-json-data-into-the-sqlite-database

    Category openHelper;
    private SQLiteDatabase database;

    public CategoryHelper(Context context) {
        openHelper = new Category(context);
        database = openHelper.getWritableDatabase();
    }

    public void saveCategoryRecord(String id, String name, String age,
            String gender) {
        try {

            ContentValues contentValues = new ContentValues();
            contentValues.put(KEY_ID, id);
            contentValues.put(KEY_NAME, name);
            contentValues.put(KEY_AGE, age);
            // contentValues.put(EMAIL,m.getEmail());

            contentValues.put(GENDER, gender);

            database.insert(TABLE_NAME, null, contentValues);

        } catch (Exception e) {
            // TODO: handle exception
            Log.e("problem", e + "");
        }
    }

    public Cursor getTimeRecordList() {
        return database.rawQuery("select * from " + TABLE_NAME, null);
    }

    private class Category extends SQLiteOpenHelper {

        public Category(Context context) {
            // TODO Auto-generated constructor stub
            super(context, DATABASE_NAME, null, DATABASE_VERSION);
        }

        @Override
        public void onCreate(SQLiteDatabase db) {
            // TODO Auto-generated method stub
            db.execSQL("CREATE TABLE " + TABLE_NAME + "( " + KEY_ID
                    + " INTEGER PRIMARY KEY AUTOINCREMENT, " + KEY_NAME
                    + " TEXT NOT NULL, " + GENDER + " TEXT NOT NULL, "
                    + KEY_AGE + " TEXT + )");
            Log.d("name", TABLE_NAME.toString());

        }

        @Override
        public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
            // TODO Auto-generated method stub
            db.execSQL("DROP TABLE IF EXISTS" + TABLE_NAME);
            onCreate(db);
        }
    }
}

我一直在logcat中收到这些错误,但我不明白发生了什么

    @Override
    protected List<getsetclass> doInBackground(String... urls) {
        try {


            HttpGet httppost = new HttpGet("https://infograins.com/INFO01/socialpad/api.php?action=searchuser&userid=109&lat=22.6858623&long=75.8597824");

            HttpClient httpclient = new DefaultHttpClient();
            HttpResponse response = httpclient.execute(httppost);

            // StatusLine stat = response.getStatusLine();
            int status = response.getStatusLine().getStatusCode();

            if (status == 200) {
                HttpEntity entity = response.getEntity();
                String data = EntityUtils.toString(entity);

                JSONObject jsono = new JSONObject(data);
                JSONArray jarray = jsono.getJSONArray("Searchuser");//get json array
                //Log.e("jarray", jarray.toString());

                  List<getsetclass> mpojo = new Vector<getsetclass>();// put getset class into listview 
                    for (int i = 0; i < jarray.length(); i++) {//convert one by one and stored in json object 
                    JSONObject object = jarray.getJSONObject(i);

                     String email = object.getString("email");//get json object
                     //Log.w("email",email.toString());
                     String name = object.getString("fullname");
                     Log.e("name",name.toString());

                     String gender = object.getString("gender");
                     //Log.e("gender",gender.toString());
                     String age = object.getString("age");
                   //  Log.e("age",age.toString());
                     String id=object.getString("userid");
                    String image=object.getString("image1");
                   Log.w("image", image.toString());
                   Log.d("bhjs--+______________------_-_","jgfjgfsjfs");




                     getsetclass m=new getsetclass(email, gender, name, age, image, id);
                     m.setAge(age);
                     m.setGender(gender);
                     m.setFullname(name);
                     m.setEmail(email);
                     m.setId(id);
                     m.setImage(image);
                     database.saveCategoryRecord(id,name,age,gender);
                        gs.add(m);}
                return mpojo;

            }

0 个答案:

没有答案