0

在我的应用程序中,我执行从网络加载数据,然后将其显示给用户。在加载数据应用程序显示进度对话框之前。如果用户在加载操作过程中锁定手机,或者服务器超载并且无法及时响应我的应用程序冻结,因为它没有关闭进度对话框,或者在某些情况下由于缺少所需的数据而崩溃,我会遇到问题。

如果在加载数据时发生了一些错误,我想向用户显示一些对话框,让他知道错误并询问他应用程序是否应该重复上一个请求。我尝试使用 AlertDialog ,但我没有成功。

这是一项活动的代码(这里没有进度对话框,但它演示了我如何加载数据):

    @EActivity(R.layout.layout_splash)
    @RoboGuice
    public class SplashScreenActivity extends Activity {

    @Inject
    private AvtopoiskParserImpl parser;

    @Bean
    BrandsAndRegionsHolder brandsAndRegionsHolder;

    @ViewById(R.id.splash_progress)
    ProgressBar progressBar;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        loadData();
    }

    @Background
    protected void loadData() {
        publishProgress(10);
        LinkedHashMap<String, Integer> brands = null;
        try {
            brands = parser.getBrands();
        } catch (IOException e) {
            Log.e(e.getMessage());
        }
        publishProgress(50);

        LinkedHashMap<String, Integer> regions = null;
        try {
            regions = parser.getRegions();
        } catch (IOException e) {
            Log.e(e.getMessage());
        }

        publishProgress(70);
        populateData(brands, regions);
    }

    @UiThread
    protected void populateData(LinkedHashMap<String, Integer> brands, LinkedHashMap<String, Integer> regions) {
        Intent intent = new Intent(SplashScreenActivity.this, SearchActivity_.class);
        brandsAndRegionsHolder.brandsMap = brands;
        brandsAndRegionsHolder.regionsMap = regions;
        publishProgress(100);
        startActivity(intent);
        finish();
    }

    @UiThread
    void publishProgress(int progress) {
        progressBar.setProgress(progress);
    }

}

parser.getBrands()并且parser.getRegions()正在从网络加载数据。

我想做这样的事情:

    boolean repeatRequest = true;
    while (repeatRequest) {
        try {
            brands = parser.getBrands();
            repeatRequest = false;
        } catch (IOException e) {
            Log.e(e.getMessage());
            repeatRequest = showErrorDialog();
        }
    }

但我没有设法这样做,因为这段代码在后台线程中执行,但对话框应该显示在 UI 线程中。我相信这应该是这样做的标准方法,但没有设法找到它。任何想法我该如何实现这个?

4

1 回答 1

1

最好的方法是使用AsyncTask.

private class LoadDataTask extends AsyncTask<Void, Integer, Object> {
    private ProgressDialog mProgress;

    protected Object doInBackground(Void... params) {
        // This method runs in background
        Object result = null;
        try {
            result = parser.parse();
        } catch (Exception e) {
            result = e.getMessage();
        }
        return result;
    }

    protected void onProgressUpdate(Integer... progress) {
        // This method runs in UI thread
        mProgress.setProgress(progress[0]);
    }

    protected void onPreExecute() {
        // This method runs in UI thread
        mProgress = new ProgressDialog(context);
        mProgress.show();
    }

    protected void onPostExecute(Object result) {
        // This method runs in UI thread
        mProgress.dismiss();

        if (result instance of String) {
            // Here you can launch AlertDialog with error message and proposal to retry
            showErrorDialog((String) result);
        } else {
            populateData(result);
        }
    }
}
于 2012-11-04T14:39:40.547 回答