-4

I have to parse Json data and save it into a Sqlite database, I want to do it in the background so that it will not affect the activity performance.

My data and UI are not connected.

After parsing and saving the data into the Sqlite process should automatically stop.

Younes
  • 462
  • 7
  • 15
Arun kumar
  • 1,894
  • 3
  • 22
  • 28

2 Answers2

2

Try this it may help

private void createHandler() {
    Thread thread = new Thread() {
        public void run() {
            Looper.prepare();

            final Handler handler = new Handler();
            handler.postDelayed(new Runnable() {
                @Override
                public void run() {
                   // Do parsing here
                    handler.removeCallbacks(this);
                    Looper.myLooper().quit();
               }
            }, 20);

            Looper.loop();
        }
    };
    thread.start();
}
Vishwajit Palankar
  • 3,033
  • 3
  • 28
  • 48
2

Since there is no real question asked here, I Will give an answer that seems suiting to your problem.

First you need to use Asyntasks, then after you treated the response you should do something like getting the instance of your database and calling the right Insertion method by passing it your the JSONObject. If it's a JSONArray, then you should loop on the array calling that function (passing the object you're on in the loop).

You could also have an object that will parse your JSON into itself, then you could call the insert methode which suits to the object.

Bxtr
  • 316
  • 1
  • 13
  • the asyntask will slow the activity .I don't want to affect UI performance of activity and till asynctask finish user will move to another activity and it will still running that will slow the app . – Arun kumar Jul 06 '15 at 12:34
  • they're meant to do that kind of job, there's nothing faster than a threading solution, and you'l still be able to use the UI, so I can't see the problem with asynctask – Bxtr Jul 06 '15 at 12:37
  • 1
    should we use background service that will run separately and we can write our parsing and data saving logic inside this service ?? – Arun kumar Jul 06 '15 at 12:42
  • 1
    yes it is able to, and it should be used to. The only thing it needs is the application context to get the instance of your SQLite database – Bxtr Jul 06 '15 at 12:44
  • let's say obj is your JSON Object, you just need to parse it using obj.get[type]("Key"); Like obj.getInt("remoteDatabaseId"); – Bxtr Jul 06 '15 at 12:48
  • that parsing and saving this all i know I just want the best way so that my UI will not slow .as I experience asynctast slow the activity – Arun kumar Jul 06 '15 at 12:51