开发者

Android several Web Service Calls on load

I have been learning Android for the past 3 months or so. However, I haven't come across anything like this yet.

I want to access several different web services when the application is initially loaded. The response from these web services should go into the DB for retrieval where needed in the application. I have a splash screen from which I am attempting to do this:

public class SplashScreen extends BaseActivity {

    protected static final int SPLASH_DURATION = 2000;

    protected ContactInfoRetriever contactInfoRetriever = new ContactInfoRetriever();

    public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.splash_screen);
    startSplashThread();
    }

    private void startSplashThread() {
    Thread splashThread = new Thread() {
        @Override
            public void run() {
            try {
            Looper.prepare();
            // fire off the calls to the different web services.
            updateContactInfo();
            updateFooInfo();
            updateBarInfo();

            int waited = 0;
            while (waited < SPLASH_DURATION) {
                sleep(100);
                waited += 100;
            }                   
            }
            catch (InterruptedException e) {
            Log.e(SplashScreen.class.getSimpleName(), "The splash thread was interrupted.");
            }
            finally {
            finish();
            startActivity(new Intent(SplashScreen.this, LandingPageActivity.class));
            }
        }

        };
    splashThread.start();
    }

    protected void updateContactInfo() {

    PerformContactInfoSearchTask task = new PerformContactInfoSearchTask();
    task.execute();
    }    

    protected void updateFooInfo() {
    PerformFooTask task = new PerformFooTask();
    task.execute();
    }

    protected void updateBarInfo() {
    PerformBarTask task = new PerformBarTask();
    task.execute();
    }

    private class PerformContactInfoSearchTask extends AsyncTask<String, Void, ContactInfo> {


    @Override
        protected ContactInfo doInBackground(String... params) {
        // this calls a class which calls a web service, and is then passed to an XML parser.
        // the result is a ContactInfo object
        return contactInfoRetriever.retrieve();
    }

    @Override
        protected void onPostExecute(final ContactInfo result) {
        runOnUiThread(new Runnable() {

            public void run() {
            InsuranceDB db = new InsuranceDB(SplashScreen.this);
            // insert the ContactInfo into the DB
            db.insertContactInfo(result);
            }
        });
    }

    }   

    private class PerformFooTask extends AsyncTask<String, Void, FooInfo&开发者_开发问答gt; {
    // similar to the PerformContactInfoSearchTask
    }

    private class PerformBarTask extends AsyncTask<String, Void, BarInfo> {
    // similar to the PerformContactInfoSearchTask
    }

}

I haven't been successful with this yet. What is the best way to do this? I don't need to update the UI thread when the tasks are completed. Does this mean I should be using something other than AsyncTask? I read something about a Looper and Handler. Is this the correct thing to use? Any code samples would be wonderful.

Thanks, Zack


More than likely you have a race condition in that the web service calls are not completed before the wait period has ended and the activity is finished.

A better approach may be to:

  • remove the waiting, finish(), and startActivity() from the splash thread.
  • combine all the web service calls into on AsyncTask.
  • move all the db operations to the task's doInBackground method.
  • in the task's onPostExecuteMethod, do your finish and startActivity().
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜