簡體   English   中英

Android加載多個Web服務調用

[英]Android several Web Service Calls on load

我過去三個月左右一直在學習Android。 但是,我還沒有遇到過這樣的事情。

最初加載應用程序時,我想訪問幾個不同的Web服務。 這些Web服務的響應應該進入數據庫,以便在應用程序中需要的地方進行檢索。 我有一個初始屏幕,我正在嘗試這樣做:

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> {
    // similar to the PerformContactInfoSearchTask
    }

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

}

我還沒有成功。 做這個的最好方式是什么? 任務完成后,我不需要更新UI線程。 這是否意味着我應該使用AsyncTask以外的其他工具? 我讀了一些關於Looper和Handler的文章。 這是正確的東西嗎? 任何代碼示例都會很棒。

謝謝,扎克

您很有可能會遇到競爭狀況,因為在等待期結束且活動結束之前,Web服務調用尚未完成。

更好的方法可能是:

  • 從啟動線程中刪除waiting,finish()和startActivity()。
  • 將所有Web服務調用合並到A​​syncTask上。
  • 將所有數據庫操作移至任務的doInBackground方法。
  • 在任務的onPostExecuteMethod中,完成並完成startActivity()。

高溫超導

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM