0

So this is how my code is supposed to work!

  1. Issue an http post(in a AyncTask class) to a mysql database using php to get json encoded data.
  2. get the json encoded data from each row 1 by 1 in dbCodeHelper class(which does everything needed for handling the database on android device.(inserting new rows and all)

1st step is working exactly as I indented. The problem is, 2nd step is not working. I think the json_data object is not getting passed to the dbCodeHelper class in which the inserting data takes place!

Here is the corresponding code parts:

//dbConnection class
JSONObject json_data;
dbCodeHelper db = new dbCodeHelper();
try{
    JSONArray jArray = new JSONArray(result);
    for(int i=0;i<jArray.length();i++){
            json_data = jArray.getJSONObject(i);
            Log.i("log_tag","id: "+json_data.getString("facts"));
            db.addAllFacts(json_data);
    }
    Log.i("log_tag","facts: "+json_data.getString("facts"));
}
catch(JSONException e){
        Log.e("log_tag", "Error parsing data "+e.toString());
}

...
//from dbCodeHelper Class
public void addAllFacts(JSONObject json_data)
{
    SQLiteDatabase db = this.getWritableDatabase();

    ContentValues values = new ContentValues();
    try {
        values.put(KEY_SL_NUMBER, json_data.getInt("sl_number"));
        values.put(KEY_NAME, json_data.getString("facts"));

        db.insert(TABLE_NAME, null, values);
        db.close();

    } catch (JSONException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
        Log.i("log_tag","id: "+ "something messed up in the dbcodehlper class(inserting all the data)!!!");
    }

}

//from mainactivity
protected void onResume() {
    super.onResume();
    if (prefs.getBoolean("firstrun", true)) {

        //getDbData(prefs);
        // Do first run stuff here then set 'firstrun' as false
        // using the following line to edit/commit prefs
        prefs.edit().putBoolean("firstrun", false).commit();
    }
    connectionDb cdb = new connectionDb();
    cdb.execute("http://coolfacts.in/app/dbconnectservice.php");
}
Simon Hayter
  • 3,131
  • 27
  • 53
defiant
  • 3,161
  • 11
  • 41
  • 65
  • Have you tried getting the JSON objects you are passing into addAllFacts(JSONObject) via logcat to verify your assumption? What is showing up? – dmason82 Dec 21 '12 at 13:01
  • Log.i("log_tag","facts: "+json_data.getString("facts")); this is showing exactly what it is meant to display! – defiant Dec 21 '12 at 13:13
  • One thing you could try doing if you can read your JSON data is changing your db.insert() call to db.insertOrThrow() and add a catch clause for SQLExceptions to see why it isn't getting into your SQLite database and/or assign a long to db.insert, as it should return the row number of the new database entry. Since if you can see the data from within your function, it would most likely be the database that isn't cooperating. – dmason82 Dec 21 '12 at 13:37
  • I think I am failing at passing the context to the dbCodeHelper. dbCodeHelper db = new dbCodeHelper(); Since I am not in an activity class, I am not able to pass the context here! :/ – defiant Dec 21 '12 at 13:58
  • 1
    Well, you can modify your application class to allow for you to put the context into your DBHelper class. The answer from [link]http://stackoverflow.com/questions/2002288/static-way-to-get-context-on-android will show you how to do that. – dmason82 Dec 21 '12 at 14:05
  • That worked! :) thanks mate! Give it as an answer and I will mark it as one! :D – defiant Dec 21 '12 at 14:44
  • No worries. Glad to be of help. – dmason82 Dec 21 '12 at 14:48

1 Answers1

1

In order to pass your context within your DBHelper class you can follow the answer listed on the following site: Using Application context everywhere?

Community
  • 1
  • 1
dmason82
  • 399
  • 4
  • 5