1

I'm having trouble figuring out how to make this work.

I'm developing an app in which I download data from a online txt, parse it and add it to my database. This is taking +-30 seconds, and there is only one part of the updater implemented.

When I try to use a Thread or Asynctask, I have problems when trying to pass the arguments to the void which is updating the Database.

How can I implement it on the good way?

Here is my code:

Main activity class:

public class Androideye_principal extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {


    //WhazzupUpdate.DownloadWhazzup(AllUsers, TotalConnections);

    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_androideye_principal);

    PilotsSQLiteHelper pdbh = new PilotsSQLiteHelper(this, "PilotsDB", null, 1);

    SQLiteDatabase dbPilots = pdbh.getWritableDatabase();

    Context context = getApplicationContext();


    String TotalConnections = new String();
    WhazzupUpdate.DownloadWhazzup(dbPilots, TotalConnections, context);   




    dbPilots.close();


    CharSequence text = "Total Users: " + TotalConnections;
    int duration = Toast.LENGTH_LONG;

    Toast toast = Toast.makeText(context, text, duration);
    toast.show();

    [Continues with Buttons stuff and so on...]

Parser class:

public class WhazzupUpdate {

public static void DownloadWhazzup (SQLiteDatabase PilotsDB, String TotalConnections, Context context) {

    try {
        // Create a URL for the desired page
        URL url = new URL("This is my url/whazzup.txt");

        // Read all the text returned by the server
        BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream()));
        String str;
        for (int i = 0; i<=4; i++) {
        in.readLine(); }    // Go to the number of connections  
        TotalConnections = in.readLine();
        for (int i = 0; i<=3; i++) {
            in.readLine(); }    // Go to !CLIENTS
        PilotsDB.execSQL("DELETE FROM Pilots");

        while (((str = in.readLine()) != null) && !(in.readLine().contains("!AIRPORTS"))) {
            // str is one line of text; readLine() strips the newline character(s)

            String[] dataRow = str.split(":");

            if (str.contains("PILOT")) {
                ContentValues NewValue = new ContentValues();
                NewValue.put("VID", dataRow[1]);
                [More NewValue puts...]




            PilotsDB.insert("Pilots", null, NewValue);

            } //End IF Pilot


        } // End While
        in.close();
    } catch (MalformedURLException e) {
    } catch (IOException e) {
    }

}}

As you see, I call WhazzupUpdate.DownloadWhazzup Method in the main activity, and this is when all is getting frozen, but don't know how to derivate it to another threat and keep the references to the Data Bases and so on...

Hope anyone can help me. Thanks in advance.

Dani Aguado
  • 215
  • 1
  • 14

1 Answers1

0

A Thread or AsyncTask would be fine here. I prefer using AsyncTask for most of my heavy-lifting. You can create an AsyncTask and do your work in doInBackground() as it works on a background Thread. Then you can update your UI elements if needed in any of its other methods.

onPostExecute() will run after a result is passed from doInBackground()

onProgressUpdate() will run if you need to update UI during doInBackground() operations by calling publishProgress(). Here you can show a ProgressBar if you need to

and

onPreExecute() will run when you first call your task before doInBackground() runs.

Running the code in a background thread using Thread or AsyncTask will allow your UI to be free while the heavy work is being done.

Example of AsyncTask

Using interface with AsyncTask to post data back yo UI

AsyncTask Docs

Community
  • 1
  • 1
codeMagic
  • 44,549
  • 13
  • 77
  • 93