Refresh json thread to parse data on specific intervals

458 views Asked by At

Ideally I want to implement

Method level synchronization where whole method is synchronized every 1minute

Not

Block level synchronization where only some set of statements are synchronized

But I don't know why when my new runnable handler execute the method at the set interval. It parses unchanged json data again and again populating my listview in a redundant manner.

How can I implement a handler that will only parse data from json that has changed/updated from previous 60seconds, without repeating data on list view?

    private class MyAsyncTask extends AsyncTask<Void, Void, String> {

    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        dialog = new ProgressDialog(RegionListActivity.this);
        dialog.setMessage("Please wait..");
        dialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
        dialog.show();
    }

    @Override
    protected String doInBackground(Void... params) {
        String url_structure = GlobalData.MAIN_URL + sessionId
                + "/user_session/?sections=app-basic&company_id="
                + companyId;
        String response_structure = CallWebService
                .getResponseOfUrl(url_structure);
        parseRegionList(response_structure);

        String url_unit = GlobalData.MAIN_URL + sessionId
                + "/reporting/system/unit_status/?sections&company_id="
                + companyId;
        String response_unit = CallWebService.getResponseOfUrl(url_unit);
        parseUnitStatus(response_unit);
        return null;

    }

    @SuppressWarnings("null")
    @Override
    protected void onPostExecute(String result) {
        super.onPostExecute(result);
        dialog.dismiss();

        if (!alRegions.isEmpty() && !alParents.isEmpty()) {
            expandableAdapter = new ExpandableListAdapter(
                    RegionListActivity.this, alParents);
            lvExpandableRL.setAdapter(expandableAdapter);

            GlobalData.alAssetsGlobal = hashMapAssets;
            GlobalData.alComponetsGlobal = hashMapComponents;

        }

        else {
            lvExpandableRL.setVisibility(View.GONE);
            tvNoRecordsRL.setVisibility(View.VISIBLE);
        }

        tvCompanyNameRL.setText(GlobalData.COMPANY_NAME);
        tvActiveAlarmsRL.setText(GlobalData.ACTIVE_ALARMS);
        tvUnitsOfflineRL.setText(GlobalData.OFFLINE_COUNT);

        /*
         * try { if(alUnitNumber.size()>0 && alUnitNumber != null) {
         * al_unit_snumber=alUnitNumber;
         * lvUnitsOffline.setVisibility(View.VISIBLE); adapter= new
         * UnitListAdapter(RegionListActivity.this, alUnitNumber);
         * lvUnitsOffline.setAdapter(adapter); } else {
         * lvUnitsOffline.setVisibility(View.GONE);
         * tvNoRecordsUnitListOffline.setVisibility(View.VISIBLE); } } catch
         * (Exception e) { // TODO Auto-generated catch block
         * e.printStackTrace(); }
         * 
         * if(alUnitStatus.size()>0 && alUnitStatus != null)
         * 
         * alUnitStatus=(ArrayList<DataUnits>)getArguments()
         * .getSerializable(WebElement.RECORDS); if(alUnitStatus.size()>0 &&
         * alUnitStatus != null) { al_unit_snumber=alUnitNumber;
         * lvUnitsOffline.setVisibility(View.VISIBLE); adapter= new
         * UnitListAdapter(RegionListActivity.this, alUnitStatus);
         * lvUnitsOffline.setAdapter(adapter); } else {
         * lvUnitsOffline.setVisibility(View.GONE);
         * tvNoRecordsUnitListOffline.setVisibility(View.VISIBLE); }
         */

    }
    new Handler().postDelayed(new Runnable() {
        public void run() {
            // call JSON methods here
            new MyAsyncTask().execute();
        }
    }, 60000);

}
0

There are 0 answers