4

I'm Trying to save data from Json into SQLite. For now I keep the data from Json into HashMap.

I already search it, and there's said use the ContentValues. But I still don't get it how to use it.

I try looking at this question save data to SQLite from json object using Hashmap in Android, but it doesn't help a lot.

Is there any option that I can use to save the data from HashMap into SQLite?

Here's My code.

MainHellobali.java

// Hashmap for ListView
ArrayList<HashMap<String, String>> all_itemList; 

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main_helloballi);

    all_itemList = new ArrayList<HashMap<String, String>>();
    // Calling async task to get json
    new getAllItem().execute();
}


private class getAllItem extends AsyncTask<Void, Void, Void> {


    @Override
    protected Void doInBackground(Void... arg0) {
        // Creating service handler class instance
        ServiceHandler sh = new ServiceHandler();

        // Making a request to url and getting response
        String jsonStr = sh.makeServiceCall(url, ServiceHandler.GET);

        Log.d("Response: ", "> " + jsonStr);

        if (jsonStr != null) {
            try {

                all_item = new JSONArray(jsonStr);


                // looping through All Contacts
                for (int i = 0; i < all_item.length(); i++) {
                    JSONObject c = all_item.getJSONObject(i);

                        String item_id = c.getString(TAG_ITEM_ID);
                        String category_name = c.getString(TAG_CATEGORY_NAME);
                        String item_name = c.getString(TAG_ITEM_NAME);

                        // tmp hashmap for single contact
                        HashMap<String, String> allItem = new HashMap<String, String>();

                        // adding each child node to HashMap key => value
                        allItem.put(TAG_ITEM_ID, item_id);
                        allItem.put(TAG_CATEGORY_NAME, category_name);
                        allItem.put(TAG_ITEM_NAME, item_name);  

                        // adding contact to contact list
                        all_itemList.add(allItem);

                }
            } catch (JSONException e) {
                e.printStackTrace();
            }
        } else {
            Log.e("ServiceHandler", "Couldn't get any data from the url");
        }

        return null;
    }
}

I have DatabasehHandler.java and AllItem.java too. I can put it in here if its necessary.

Thanks before

** Add Edited Code **

 // looping through All Contacts
                for (int i = 0; i < all_item.length(); i++) {
                    JSONObject c = all_item.getJSONObject(i);

                        String item_id = c.getString(TAG_ITEM_ID);
                        String category_name = c.getString(TAG_CATEGORY_NAME);
                        String item_name = c.getString(TAG_ITEM_NAME);

                        DatabaseHandler databaseHandler = new DatabaseHandler(this); //error here "The Constructor DatabaseHandler(MainHellobali.getAllItem) is undefined

}
Community
  • 1
  • 1
Matthew
  • 95
  • 4
  • 14

2 Answers2

2

As mentioned by @birdy you can just store the JSON data as String inside your database. In my case I've already done the same thing you are trying to achieve, in my case I've just created an abstract datasource that will be extended for any JSON object I will set in my database. But basically you just need a method to convert a JSONObject to a ContentValues object.

public ContentValues jsonToContentValues(JSONObject json) {
        ContentValues values = new ContentValues();
        values.put("MY_COLUMN", json.optString("MY_JSON_VALUE"));
        return values;
}

After you have your content value object all set you just need to insert the values on your database.

return database.insert("MY_TABLE_NAME", null, contentValues);
GhostDerfel
  • 1,533
  • 1
  • 11
  • 18
  • thanks a lot, but I'm still a little bit confused. Where should I put that ContentValues. Sorry, I'm newbie at this thing. – Matthew Aug 21 '14 at 16:24
  • I don't think I could understand your comment. You just need to get you JSON and call the method jsonToContentValues (you need to edit this method) after this the method will return you a ContentValues object, if everything is Ok you just need to call your database insert – GhostDerfel Aug 21 '14 at 16:27
  • Is it something like this question? http://stackoverflow.com/questions/6293268/save-data-to-sqlite-from-json-object-using-hashmap-in-android – Matthew Aug 21 '14 at 16:41
  • Yes, exactly like that... If you need to understand better how to work with your database in Android I highly recommend you to read this article : http://www.vogella.com/tutorials/AndroidSQLite/article.html – GhostDerfel Aug 21 '14 at 16:44
  • Okay, But when I try to call my `DatabaseHandler` I got an error. You can see it at my question on Add edited Code – Matthew Aug 21 '14 at 16:48
  • If DatabaseHandler extends SQLiteOpenHelper you can't call the constructor with your AsyncTask, the constructor expects a Context – GhostDerfel Aug 21 '14 at 16:51
  • Is there a way to call it with the ASyncTask? Because my DatabaseHandler extends SQLiteOpenHelper. – Matthew Aug 21 '14 at 16:57
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/59747/discussion-between-ghostderfel-and-matthew). – GhostDerfel Aug 21 '14 at 16:58
1

If what you need is to store JSON data - just store it as a text. Than after taking it back from database you can again parse it into map.

amukhachov
  • 5,822
  • 1
  • 41
  • 60
  • Thanks for your answer. Do you have any example ? Because I try it before and I got an error. You can check my question. – Matthew Aug 21 '14 at 16:08