これが私のコードがどのように機能するかです!
- php を使用して mysql データベースに (AyncTask クラスで) http post を発行し、json でエンコードされたデータを取得します。
- dbCodeHelper クラスの各行から 1 つずつ json でエンコードされたデータを取得します (これは、Android デバイスでデータベースを処理するために必要なすべてを行います (新しい行とすべてを挿入します)。
最初のステップは、インデントしたとおりに機能しています。問題は、2 番目のステップが機能しないことです。データの挿入が行われる dbCodeHelper クラスに json_data オブジェクトが渡されていないと思います!
対応するコード部分は次のとおりです。
//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");
}