0

I'm working on Android project where I'm collecting some information from web server. Everytime the application starts I'm downloading JSON data and reenter the information in database. I have this scenario :

  1. Application starts, I'm deleting all data in sqlite database and insert it again (that's needed if there are some changes in json data).
  2. While I'm downloading the new json user can't see any information because I've already delete it. He can see periodically when the single items are downloaded and insert in database.
  3. After whole process is done and everything is downloaded and insert user can see all the new available information.

So I need an idea how to do this thing: When I start downloading the data from json when app starts user must be available to see the old info. When I'm ready with all the information and everything is downloaded, the whole JSON I have to delete/update/insert the new data and everything must happen so quick that the user must don't notice that.

Any idea how can I do this?

Thanks in advance!

Android-Droid
  • 14,365
  • 41
  • 114
  • 185

2 Answers2

1

There are 3 possibilities.

AsyncTask

This is the first 'obvious' choice until you realize the framework has many issues with it (like how to handle onConfigChange)

ContentHandler / Account / SycnService

This is the most complicated solution. First you must know these 3 pieces come together. So you'll have to write a "dumb" Account if you don't need one, and a "half-dumb" ContentHandler if you have a SQLLite.

Service

I chose to use an IntentService and I am very satisfied.

  1. When the activity starts, it displays Data. It registers for Intent my.app.DATA_UPDATED. And send an Intent my.app.DATA_REQUESTED
  2. An IntentService is declared in the manifest with intent filter DATA_REQUESTED.
  3. When the intent is received, the service fetchs the data, and update the database. When done, it sends a DATA_UPDATED intent.
  4. The activity refreshes the View, with the current data when intent DATA_UPDATED is received.
Community
  • 1
  • 1
rds
  • 26,253
  • 19
  • 107
  • 134
0

You need to use AsyncTask

step-1 delete all the daa from database and clear listview in onPreExecute() method

step-2 download the data from server in doBackground() and save to database

step-3 onPostExecute update the listview with the latest data

import android.content.Context;
import android.os.AsyncTask;
import android.view.View;



public class LoadingDataFromServer extends AsyncTask {
    Context currentContext = null;
ListView listview = null;
    boolean isCancelled = false;

    // private ProgressDialog progressDialog = null;
    public LoadingDataFromServer(Context context, ListView list) {
        currentContext = context;
listview = list;

    }

    @Override
    protected void onPreExecute() {
        //delete all the daa from database
//clear listview

        super.onPreExecute();
    }

    @Override
    protected Object doInBackground(Object... params) {
        // do background processing

        try {
            // getdata from server and store in SQLitedatabase

        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return null;
    }

    @Override
    protected void onPostExecute(Object result) {
        // TODO Auto-generated method stub
        // progressDialog.dismiss();
//update the listview after getting the data

        super.onPostExecute(result);
    }

    @Override
    protected void onCancelled() {
        // TODO Auto-generated method stub
        isCancelled = true;
        super.onCancelled();
    }

}
Sunil Kumar Sahoo
  • 53,011
  • 55
  • 178
  • 243
  • I'm using Service to download the data, because while I'm downloading JSON I want to show ProgressBar in whole application.I can't do that in AsyncTask. – Android-Droid Dec 28 '11 at 09:22