title: Want to insert json data into the Sqlite Database
body: <p>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.</p>
  
  <p>This is the db part i created for json data.</p>
  
  <pre><code>public class GinfyDbAdapter {
  
      private static final String DATABASE_NAME = "ginfy.db";
      private static final String DATABASE_TABLE_PROJ = "prayers";
      private static final int DATABASE_VERSION = 2;
      public static final String CATEGORY_COLUMN_ID = "id";
      public static final String CATEGORY_COLUMN_TITLE = "title";
      public static final String CATEGORY_COLUMN_CONTENT = "content";
      public static final String CATEGORY_COLUMN_COUNT = "count";
  
  
      private static final String TAG = "ProjectDbAdapter";
      private DatabaseHelper mDbHelper;
      private SQLiteDatabase mDb;
  
      public GinfyDbAdapter(MainActivity mainActivity) {
          // TODO Auto-generated constructor stub
      }
  
      public GinfyDbAdapter GinfyDbAdapter(Context context){
          mDbHelper = new DatabaseHelper(context);
  
          mDb = mDbHelper.getWritableDatabase();
           return this;
      }
  
      public void saveCategoryRecord(String id, String title, String content, String count) {
          ContentValues contentValues = new ContentValues();
          contentValues.put(CATEGORY_COLUMN_ID, id);
          contentValues.put(CATEGORY_COLUMN_TITLE, title);
          contentValues.put(CATEGORY_COLUMN_CONTENT, content);
          contentValues.put(CATEGORY_COLUMN_COUNT, count);
          mDb.insert(DATABASE_NAME, null, contentValues);
          }
      public Cursor getTimeRecordList() {
          return mDb.rawQuery("select * from " + DATABASE_NAME, null);
          }
      private static class DatabaseHelper extends SQLiteOpenHelper {
  
          DatabaseHelper(Context context) {
              super(context, DATABASE_NAME, null, DATABASE_VERSION);
          }
  
  
  
      private static final String DATABASE_CREATE_PROJ =
              "create table " + DATABASE_TABLE_PROJ + " ("
                      + CATEGORY_COLUMN_ID + " integer primary key autoincrement, "
                      + CATEGORY_COLUMN_TITLE + " text not null, " + CATEGORY_COLUMN_CONTENT + " text not null, " + CATEGORY_COLUMN_COUNT + " integer primary key autoincrement   );" ;
  
      @Override
      public void onCreate(SQLiteDatabase db) {
          // TODO Auto-generated method stub
          db.execSQL("CREATE TABLE " + DATABASE_TABLE_PROJ + "( "
                  + CATEGORY_COLUMN_ID + " INTEGER PRIMARY KEY, "
                  + CATEGORY_COLUMN_TITLE + " TEXT, " + CATEGORY_COLUMN_CONTENT + " TEXT, " + CATEGORY_COLUMN_COUNT + " INTEGER PRIMARY KEY )" );
  
      }
  
      @Override
      public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
          // TODO Auto-generated method stub
          db.execSQL("DROP TABLE IF EXISTS"+ DATABASE_NAME);
          onCreate(db);
      }
  
  }
  
  
  }
  </code></pre>
  
  <p>this is mainactivity which is showing listview</p>
  
  <pre><code>public class MainActivity extends Activity implements FetchDataListener,OnClickListener{
      private static final int ACTIVITY_CREATE=0;
      private static final String TAG_CATEGORY = "post";
      private static final String CATEGORY_COLUMN_ID = "id";
      private static final String CATEGORY_COLUMN_TITLE = "title";
      private static final String CATEGORY_COLUMN_CONTENT = "content";
      private static final String CATEGORY_COLUMN_COUNT = "count";
      private static final int Application = 0;
      private ProgressDialog dialog;
      ListView lv;
      ListView lv1;
      private List&lt;Application&gt; items;
      private Button btnGetSelected;
      private Button praycount;
      public int pct;
      private String stringVal;
      private TextView value;
      private int prayers;
      private int prayerid;
      EditText myFilter;
      ApplicationAdapter adapter;
       private GinfyDbAdapter mDbHelper;
       JSONArray contacts = null;
          private SimpleCursorAdapter dataAdapter;
  
      @Override
      protected void onCreate(Bundle savedInstanceState){
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_list_item); 
          mDbHelper=new GinfyDbAdapter(MainActivity.this);
  
          lv1 =(ListView)findViewById(R.id.list); 
          lv =(ListView)findViewById(R.id.list);
  
           ArrayList&lt;HashMap&lt;String, String&gt;&gt; contactList = new ArrayList&lt;HashMap&lt;String, String&gt;&gt;();
  
              // Creating JSON Parser instance
              //JSONParser jParser = new JSONParser();
              JSONObject jsonObject = new JSONObject();
              //JSONArray aJson = jsonObject.getJSONArray("post");
              String url = "http://www.ginfy.com/api/v1/posts.json";
              // getting JSON string from URL
              JSONArray aJson = jsonObject.getJSONFromUrl(url);
  
              try {
                  // Getting Array of Contacts
                  contacts = aJson.getJSONObject(TAG_CATEGORY);
  
                  // looping through All Contacts
                  for(int i = 0; i &lt; contacts.length(); i++){
                      JSONObject c = contacts.getJSONObject(i);
  
                      // Storing each json item in variable
                      String 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(id,title,content,count);
  
                      // creating new HashMap
                      HashMap&lt;String, String&gt; map = new HashMap&lt;String, String&gt;();
  
                      // adding each child node to HashMap key =&gt; value
                      map.put(CATEGORY_COLUMN_ID, id);
                      map.put(CATEGORY_COLUMN_TITLE, title);
                      map.put(CATEGORY_COLUMN_CONTENT, content);
                      map.put(CATEGORY_COLUMN_COUNT, count);
                      // adding HashList to ArrayList
                      contactList.add(map);
                  }
              } catch (JSONException e) {
                  e.printStackTrace();
              }
  
  
  
  
  
          btnGetSelected = (Button) findViewById(R.id.btnget);
          btnGetSelected.setOnClickListener(this);
  
          myFilter = (EditText) findViewById(R.id.myFilter);
  
  
  
  
          //praycount.setOnClickListener(this);
          initView();
      }
  
      private void initView(){
          // show progress dialog
          dialog = ProgressDialog.show(this, "", "Loading...");
          String url = "http://www.ginfy.com/api/v1/posts.json";
          FetchDataTask task = new FetchDataTask(this);
          task.execute(url);
  
  
      }   
  
      @Override
      public boolean onCreateOptionsMenu(Menu menu) {
          super.onCreateOptionsMenu(menu);
          MenuInflater mi = getMenuInflater();
          mi.inflate(R.menu.activity_main, menu); 
          return true;
      }
  
      @Override
      public boolean onMenuItemSelected(int featureId, MenuItem item) {
          createProject();
          return super.onMenuItemSelected(featureId, item);
      }
  
      private void createProject() {
          Intent i = new Intent(this, AddPrayerActivity.class);
          startActivityForResult(i, ACTIVITY_CREATE);   
      }
  
      @Override
      protected void onActivityResult(int requestCode, int resultCode, Intent intent) {
          super.onActivityResult(requestCode, resultCode, intent);
          initView();
      }
  
      @Override
      public void onFetchComplete(List&lt;Application&gt; data){
          this.items = data;
          // dismiss the progress dialog
          if ( dialog != null )
              dialog.dismiss();
          // create new adapter
          ApplicationAdapter adapter = new ApplicationAdapter(this, data);
          /*dataAdapter adapter = new SimpleCursorAdapter(this,
                      R.layout.activity_row,
                      new String[] { CATEGORY_COLUMN_TITLE, CATEGORY_COLUMN_CONTENT, CATEGORY_COLUMN_COUNT }, new int[] {
                              R.id.text2, R.id.text1, R.id.count });*/
  
             //lv.setListAdapter(adapter);
          // set the adapter to list
          lv.setAdapter(adapter);
          lv.setOnItemClickListener(new OnItemClickListener() {
  
              @Override
              public void onItemClick(AdapterView&lt;?&gt; parent, View view,
                      int position, long id) {
  
                  CheckBox chk = (CheckBox) view.findViewById(R.id.checkbox);
                  Application bean = items.get(position);
                  if (bean.isSelected()) {
                      bean.setSelected(false);
                      chk.setChecked(false);
                  } else {
                      bean.setSelected(true);
                      chk.setChecked(true);
                  }
  
              }
          });
      }
  
      // Toast is here...
      private void showToast(String msg) {
          Toast.makeText(this, msg, Toast.LENGTH_SHORT).show();
      }
  
      @Override
      public void onFetchFailure(String msg){
          if ( dialog != null )
              dialog.dismiss();
          Toast.makeText(this, msg, Toast.LENGTH_LONG).show();
      }
  </code></pre>
  
  <p>Before using db it shows in listview also,rite now i want to make db also,for that i wrote some code in mainactivity.</p>
  
  <p>Fetchdatatask.java</p>
  
  <pre><code>public class FetchDataTask extends AsyncTask&lt;String, Void, String&gt;
  {
      private final FetchDataListener listener;
      private  OnClickListener onClickListener;
      private String msg;
  
      public FetchDataTask(FetchDataListener listener)
      {
          this.listener = listener;
      }
  
  
  
      @Override
      protected String doInBackground(String... params)
      {
          if ( params == null )
              return null;
          // get url from params
          String url = params[0];
          try
          {
              // create http connection
              HttpClient client = new DefaultHttpClient();
              HttpGet httpget = new HttpGet(url);
              // connect
              HttpResponse response = client.execute(httpget);
              // get response
              HttpEntity entity = response.getEntity();
              if ( entity == null )
              {
                  msg = "No response from server";
                  return null;
              }
              // get response content and convert it to json string
              InputStream is = entity.getContent();
              return streamToString(is);
          }
          catch ( IOException e )
          {
              msg = "No Network Connection";
  
          }
          return null;
      }
  
      @Override
      protected void onPostExecute(String sJson)
      {
          if ( sJson == null )
          {
              if ( listener != null )
                  listener.onFetchFailure(msg);
              return;
          }
          try
          {
              // convert json string to json object
              JSONObject jsonObject = new JSONObject(sJson);
              JSONArray aJson = jsonObject.getJSONArray("post");
              // create apps list
              List&lt;Application&gt; apps = new ArrayList&lt;Application&gt;();
              for ( int i = 0; i &lt; aJson.length(); i++ )
              {
                  JSONObject json = aJson.getJSONObject(i);
                  Application app = new Application();
                  app.setContent(json.getString("content"));
                  app.setTitle(json.getString("title"));
                   app.setCount(Integer.parseInt(json.getString("count"))); 
                   app.setId(Integer.parseInt(json.getString("id")));
  
  
  
                  // add the app to apps list
                  apps.add(app);
  
  
              }
              //notify the activity that fetch data has been complete
              if ( listener != null )
                  listener.onFetchComplete(apps);
          }
          catch ( JSONException e )
          {
              e.printStackTrace();
              msg = "Invalid response";
              if ( listener != null )
                  listener.onFetchFailure(msg);
              return;
          }
      }
  
      /**
       * This function will convert response stream into json string
       * 
       * @param is
       *            respons string
       * @return json string
       * @throws IOException
       */
      public String streamToString(final InputStream is) throws IOException
      {
          BufferedReader reader = new BufferedReader(new InputStreamReader(is));
          StringBuilder sb = new StringBuilder();
          String line = null;
          try
          {
              while ( (line = reader.readLine()) != null )
              {
                  sb.append(line + "\n");
              }
          }
          catch ( IOException e )
          {
              throw e;
          }
          finally
          {
              try
              {
                  is.close();
              }
              catch ( IOException e )
              {
                  throw e;
              }
          }
          return sb.toString();
      }
  }
  </code></pre>
  
  <p>This fetchdatatask fetch from json and showing in listview,i want that without internet time it has to show in listview also for that i am creating db.</p>
  
  <p>can you check my code is correct,actually it showing error in mainactivity line   JSONArray aJson = jsonObject.getJSONFromUrl(url);</p>