So this is how my code is supposed to work!
- Issue an http post(in a AyncTask class) to a mysql database using php to get json encoded data.
- 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");
}