title: Timer HttpGet asyntask to localDatabase + display in ListView
body: <p>Hi i'm completely new to Android. Very used to .Net and SQL Server. Hence, having issue adopting to SQLite. I'm currently able to display the result in a <code>listView</code> from a URL via <code>HttpGet</code>. Kindly be gentle with me. Helps are greatly appreciated, as I'm stuck in SQLite for the past week. </p>
  
  <p>Just want check if it's possible to set it such that </p>
  
  <ol>
  <li><p><code>HttpGet</code> will retrieve and insert into SQLite table. </p></li>
  <li><p><code>listView</code> retrieve All columns data from SQLite table. </p></li>
  <li><p>The <code>HttpGet</code> AsyncTask will run only if 48hrs has pass. So for example if the the download happens on Monday, the database should not be updated if the person use the app on Tuesday. However, the data will be updated on Wed when the user on it. </p></li>
  <li><p>It will delete all column in the table before inserting into SQlite table. </p>
  
  <p>EncounterActivity.Java</p>
  
  <pre><code>public class EncounterActivity extends Activity {
  ListView list;
  TextView eid;
  TextView eclerkship;
  TextView ename;
  TextView etype;
  TextView erequiredattempts;
  EncounterDbAdapter encounterDB;
  Context myContext;
  
  Button Btngetdata;
  ArrayList&lt;HashMap&lt;String, String&gt;&gt; encouterlist = new ArrayList&lt;HashMap&lt;String, String&gt;&gt;();
  
  //URL to get JSON Array
  private static String url = "SorryHadToRemoveThisDueToSomeReason";
  
  //JSON Node Names 
  private static final String TAG_ENCOUNTERS = "encounters";
  private static final String TAG_E_ID = "e_id";
  private static final String TAG_E_CLERKSHIP = "clerkship";
  private static final String TAG_E_NAME = "encounter_name";  
  private static final String TAG_E_TYPE = "type";
  private static final String TAG_E_REQUIRED_ATTEMPTS = "required_attempts";
  
  JSONArray android = null;
  
  
  
  
  @Override
  protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
  
      setContentView(R.layout.activity_encounter);
      encouterlist = new ArrayList&lt;HashMap&lt;String, String&gt;&gt;();
  
  
               new JSONParse().execute();
  
     }
  
  
  
  private class JSONParse extends AsyncTask&lt;String, String, JSONObject&gt; {
       private ProgressDialog pDialog;
      @Override
      protected void onPreExecute() {
          super.onPreExecute();
           eid = (TextView)findViewById(R.id.eid);
           eclerkship = (TextView)findViewById(R.id.eclerkship);
           ename = (TextView)findViewById(R.id.ename);
           etype = (TextView)findViewById(R.id.etype);
           erequiredattempts = (TextView)findViewById(R.id.erequiredattempts);
          pDialog = new ProgressDialog(EncounterActivity.this);
          pDialog.setMessage("Getting Data ...");
          pDialog.setIndeterminate(false);
          pDialog.setCancelable(true);
          pDialog.show();
  
  
  
      }
  
      @Override
      protected JSONObject doInBackground(String... args) {
  
          JSONParser jParser = new JSONParser();
  
          // Getting JSON from URL
          JSONObject json = jParser.getJSONFromUrl(url);
          return json;
      }
       @Override
       protected void onPostExecute(JSONObject json) {
           pDialog.dismiss();
           try {
  
  
                  // Getting JSON Array from URL
                  android = json.getJSONArray(TAG_ENCOUNTERS);
                  for(int i = 0; i &lt; android.length(); i++){
                  JSONObject c = android.getJSONObject(i);
  
                  // Storing  JSON item in a Variable
                  String eid = c.getString(TAG_E_ID);
                  String eclerkship = c.getString(TAG_E_CLERKSHIP);
                  String ename = c.getString(TAG_E_NAME);
                  String etype = c.getString(TAG_E_TYPE);     
                  String erequiredattempts = c.getString(TAG_E_REQUIRED_ATTEMPTS);
  
                  // Opening of database
                  encounterDB = new EncounterDbAdapter(myContext);
                  encounterDB.open();
                  encounterDB.insertEncounterEntry(eid, eclerkship, ename, etype, erequiredattempts);
  
                  // Adding value HashMap key =&gt; value
  
                  HashMap&lt;String, String&gt; map = new HashMap&lt;String, String&gt;();
  
                  map.put(TAG_E_ID, eid);
                  map.put(TAG_E_CLERKSHIP, eclerkship);
                  map.put(TAG_E_NAME, ename);
                  map.put(TAG_E_TYPE, etype);
                  map.put(TAG_E_REQUIRED_ATTEMPTS, erequiredattempts);
  
                  encouterlist.add(map);
                  list=(ListView)findViewById(R.id.list);
  
  
  
  
  
                  ListAdapter adapter = new SimpleAdapter(EncounterActivity.this, encouterlist,
                          R.layout.list_v,
                          new String[] { TAG_E_ID, TAG_E_CLERKSHIP, TAG_E_NAME, TAG_E_TYPE, TAG_E_REQUIRED_ATTEMPTS }, new int[] {
                                  R.id.eid, R.id.eclerkship, R.id.ename, R.id.etype, R.id.erequiredattempts});
  
                  list.setAdapter(adapter);
                  list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
  
                      @Override
                      public void onItemClick(AdapterView&lt;?&gt; parent, View view,
                                              int position, long id) {
                          Toast.makeText(EncounterActivity.this, "You Clicked at "+encouterlist.get(+position).get("type"), Toast.LENGTH_SHORT).show();
  
                      }
                  });
  
                  }
          } catch (JSONException e) {
              e.printStackTrace();
          }
  
  
       }
  }
  
  }
  </code></pre></li>
  </ol>
  
  <p>JsonParser.java</p>
  
  <pre><code>public class JSONParser {
  
      static InputStream is = null;
      static JSONObject jObj = null;
      static String json = "";
  
      // constructor
      public JSONParser() {
  
      }
  
      public JSONObject getJSONFromUrl(String url) {
  
          // Making HTTP request
          try {
              // defaultHttpClient
              DefaultHttpClient httpClient = new DefaultHttpClient();
              HttpGet httpGet = new HttpGet(url);
  
              HttpResponse httpResponse = httpClient.execute(httpGet);
              HttpEntity httpEntity = httpResponse.getEntity();
              is = httpEntity.getContent();           
  
          } catch (UnsupportedEncodingException e) {
              e.printStackTrace();
          } catch (ClientProtocolException e) {
              e.printStackTrace();
          } catch (IOException e) {
              e.printStackTrace();
          }
  
          try {
              BufferedReader reader = new BufferedReader(new InputStreamReader(
                      is, "iso-8859-1"), 8);
              StringBuilder sb = new StringBuilder();
              String line = null;
              while ((line = reader.readLine()) != null) {
                  sb.append(line + "\n");
              }
              is.close();
              json = sb.toString();
          } catch (Exception e) {
              Log.e("Buffer Error", "Error converting result " + e.toString());
          }
  
          // try parse the string to a JSON object
          try {
              jObj = new JSONObject(json);
          } catch (JSONException e) {
              Log.e("JSON Parser", "Error parsing data " + e.toString());
          }
  
          // return JSON String
          return jObj;
  
      }
  }
  </code></pre>
  
  <p>EncounterDbAdapter.java</p>
  
  <pre><code>public class EncounterDbAdapter {
  
      private static final String DATABASE_NAME = "mediLearner.db";
      private static final String DATABASE_TABLE = "EncounterDb";
      private static final int DATABASE_VERSION = 2;
      private SQLiteDatabase _db;
      private final Context context;
  
  
  
      public static final String E_ID = "_id";
      public static final int COLUMN_KEY_ID = 0;
      public static final String CLERKSHIP  = "entry_clerkship";
      public static final int COLUMN_CLERKSHIP_ID = 1;
      public static final String ENCOUNTER_NAME = "entry_encounter";
      public static final int COLUMN_ENCOUNTER_NAME = 2;
      public static final String TYPE = "entry_type";
      public static final int COLUMN_TYPE = 3;
      public static final String REQUIRED_ATTEMPTS = "entry_requiredAttempts";
      public static final int COLUMN_REQUIRED_ATTEMPTS = 4;
  
  
  
  
      protected static final String DATABASE_CREATE = "create table "
              + DATABASE_TABLE + " (" + E_ID
              + " PRIMARY KEY, " + CLERKSHIP + " Text, "
              + ENCOUNTER_NAME + " text, " + TYPE + " Text, " 
              + REQUIRED_ATTEMPTS + " Text);";
  
      private String encounterDBADAPTER_LOG_CAT = "MY_LOG";
  
      private myDBOpenHelper dbHelper;
  
      public EncounterDbAdapter(Context _context) {
  
          this.context = _context;
          dbHelper = new myDBOpenHelper(context, DATABASE_NAME, null,
                  DATABASE_VERSION);
  
      }
  
      public void close() {
              _db.close();
              Log.w(encounterDBADAPTER_LOG_CAT, "DB closed");
      }
  
      public void open() throws SQLiteException {
          try{
              _db=dbHelper.getWritableDatabase();
              Log.w(encounterDBADAPTER_LOG_CAT, "DB opened as writable database");
  
          }catch(SQLiteException ex){
              _db=dbHelper.getReadableDatabase();
              Log.w(encounterDBADAPTER_LOG_CAT, "DB opened as readable database");
          }
      }
  //insertion
      public long insertEncounterEntry(String ID , String clerkship, String encounter, String type, String requiredAttempts ) {
  
          //insert new task
          ContentValues newEntryValues=new ContentValues();
          newEntryValues.put(E_ID, ID);
          newEntryValues.put(CLERKSHIP,clerkship);
          newEntryValues.put(ENCOUNTER_NAME,encounter);
          newEntryValues.put(TYPE, type);
          newEntryValues.put(REQUIRED_ATTEMPTS, requiredAttempts);
  
          //Insert the row
          Log.w(encounterDBADAPTER_LOG_CAT, "Inserted E_ID="+ID+" CLERKSHIP="+clerkship + "Inserted ENCOUNTER_NAME="+encounter + "Inserted TYPE="+type +  "Inserted REQUIRED_ATTEMPTS="+requiredAttempts +"into table"+DATABASE_TABLE);
          return _db.insert(DATABASE_TABLE, null, newEntryValues);
      }
  
      public boolean removeEntry(long _rowIndex) {
  
          if(_db.delete(DATABASE_TABLE, E_ID+"="+_rowIndex, null)&lt;=0)
          {
              Log.w(encounterDBADAPTER_LOG_CAT,"Removing Entrying where id="+_rowIndex+"Failed");
              return false;
          }
          Log.w(encounterDBADAPTER_LOG_CAT,"Removing Entrying where id="+_rowIndex+"Success");
          return true;
  
      }
  
      //updating
      public boolean updateEntry(int ID , String clerkship, String encounter_name, String type, String requiredAttempts ) {
  
          return false;
      }
  
      //retrival 
      public Cursor retrieveAllEntriesCursor() {
  
          Cursor c=null;
          try{
              c=_db.query(DATABASE_TABLE,new String[]{E_ID, CLERKSHIP, ENCOUNTER_NAME, TYPE, REQUIRED_ATTEMPTS}, null,null,null,null,null);
          }catch(SQLException sle){
              Log.w(encounterDBADAPTER_LOG_CAT,"Retrieve fail!!");
          }
          return c;
      }
  
  
      public class myDBOpenHelper extends SQLiteOpenHelper {
  
          public myDBOpenHelper(Context context, String name,
                  CursorFactory factory, int version) {
              super(context, name, factory, version);
              // TODO Auto-generated constructor stub
          }
  
          @Override
          public void onCreate(SQLiteDatabase db) {
              // TODO Auto-generated method stub
              db.execSQL(DATABASE_CREATE);
              Log.w(encounterDBADAPTER_LOG_CAT, "Helper : DB " + DATABASE_TABLE
                      + " Created!!");
          }
  
          @Override
          public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
              // TODO Auto-generated method stub
  
          }
  
      } // End of myDBOpenHelper
  
  }// End of myDBAdapter
  </code></pre>