Downloading and Parsing Question Batches

Now that you understand how to download data asynchronously, you can use AsyncTask to handle downloading and displaying the question batches on the game screen. This process is very similar to the process involved in downloading score data. However, you will not publish progress as you go; instead, you will simply display a progress bar until all questions in a given batch are downloaded.

Begin by creating a subclass called QuizTask that extends the AsyncTask class within the QuizGameActivity class, like this:

private class QuizTask extends AsyncTask<Object, String, Boolean> { // TODO: Implement AsyncTask callback methods


You might want to add a custom DEBUG_TAG member variable to the QuizTask class. This allows you to differentiate between debugging information from the background thread and UI thread.

The QuizTask class also needs member variables for the starting question number and a ProgressDialog to display background processing progress to the user:

int startingNumber; ProgressDialog pleaseWaitDialog;

Once you've implemented the QuizTask class, you can update the onCreate() method of the QuizGameActivity class to call the QuizTask class's execute() method when the screen first loads. The execute() method takes two parameters: the server web address for question downloads and the starting question number (an Integer) for the batch to download:

public static final String TRIVIA_SERVER_QUESTIONS =

TRIVIA_SERVER_BASE + "questions.jsp"; // ...

QuizTask downloader = new QuizTask();

downloader.execute(TRIVIA_SERVER_QUESTIONS, startingQuestionNumber)

0 0

Post a comment