Want To Insert Json Data Into The Sqlite Database
I want to store an json data to db,it has to display my apps has to display some previous data without internet time also.For that i want to create an db for json data to store. T
Solution 1:
Create a class which act's as the intermediate between your Db class and the main activity to insert the data into the db and vice versa
publicclassCategory {
String id;
String title;
String content;
String count;
publicCategory(String id, String title, String content, String count) {
super();
this.id = id;
this.title = title;
this.content = content;
this.count = count;
}
publicStringgetId() {
return id;
}
publicvoidsetId(String id) {
this.id = id;
}
publicStringgetTitle() {
return title;
}
publicvoidsetTitle(String title) {
this.title = title;
}
publicStringgetContent() {
return content;
}
publicvoidsetContent(String content) {
this.content = content;
}
publicStringgetCount() {
return count;
}
publicvoidsetCount(String count) {
this.count = count;
}
}
In your main activity where you do the json parsing create an object of DB class and call one the save record method at there like i did below
DatabaseHelper mDbHelper;
publicclassABCextendsActivity {
@OverrideprotectedvoidonCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.abc);
mDbHelper= newDatabaseHelper (this);
newGetSyncDataAsyncTask().execute();
}
}
privateclassGetDataAsyncTaskextendsAsyncTask<Void, Void, Boolean> {
privateProgressDialogDialog = newProgressDialog(ABC.this);
protectedvoidonPreExecute() {
Dialog.setMessage("Loading.....");
Dialog.show();
}
@OverrideprotectedvoidonPostExecute(Boolean result) {
super.onPostExecute(result);
Dialog.dismiss();
Intent intent = newIntent(ABC.this, XYZ.class);
startActivity(intent);
}
@OverrideprotectedBooleandoInBackground(Void... params) {
getData();
returnnull;
}
}
publicvoidgetProdData() {
// getting JSON string from URLJSONParser parser = newJSONParser();
JSONObject jsonObject = newJSONObject();
//JSONArray aJson = jsonObject.getJSONArray("post");String url = "http://www.ginfy.com/api/v1/posts.json";
// getting JSON string from URLJSONArray aJson = jsonObject.getJSONFromUrl(url);
try {
// Getting Array of Contacts
contacts = aJson.getJSONObject(TAG_CATEGORY);
// looping through All Contactsfor(int i = 0; i < contacts.length(); i++){
JSONObject c = contacts.getJSONObject(i);
// Storing each json item in variableString id = c.getString(CATEGORY_COLUMN_ID);
String title = c.getString(CATEGORY_COLUMN_TITLE);
String content = c.getString(CATEGORY_COLUMN_CONTENT);
String count = c.getString(CATEGORY_COLUMN_COUNT);
mDbHelper.saveCategoryRecord(newCategory(id,title,content,count));
}
} catch (JSONException e) {
e.printStackTrace();
}
}
At last in your db class insert the values like below
public void saveCategoryRecord(Category category) {
String query = "insert into"+ TABLE_NAME+ values( ?,?,?,?,?, ? )";
SQLiteStatement stmt = mDb.compileStatement(query);
stmt.bindString(1, category.getId());
stmt.bindString(2, category.getTitle());
stmt.bindString(3, category.getContent());
stmt.bindString(4, category.getCount());
stmt.execute();
}
I have tried to use the same things as you have used. This is the way i think now you got the concept to do that
Post a Comment for "Want To Insert Json Data Into The Sqlite Database"