异常后未显示进度对话框

时间:2017-09-04 17:46:08

标签: android

我正在尝试使用以下代码进行文件下载。就下载而言,一切正常。我遇到的一个问题是,只要出现下载失败,就不会显示进度对话框。我错过了这里明显的一切吗?造成这种情况的原因是什么?

非常感谢任何帮助

public void onButtonClick(View view) {
    mDialog = new ProgressDialog(MainActivity.this);
    mDialog.setMessage("Downloading File");
    mDialog.setCancelable(false);
    mDialog.show();// Does not show during download exceptions- is it because the below download code fails abruptly?
    new NTLMTestData ().asyncNTLMTest("http://myurl/FileName.csv", this, getApplicationContext());
}

// The below call backs get fired from async    
@Override
public void onSuccess(String msg) {
    mDialog.dismiss();
}

@Override
public void onFailure(Exception ex) {
    mDialog.dismiss();
}

1 个答案:

答案 0 :(得分:1)

尝试这种方式

public void onButtonClick(View view) {
 try{
    mDialog = new ProgressDialog(MainActivity.this);
    mDialog.setMessage("Downloading File");
    mDialog.setCancelable(false);
    mDialog.show();
    new NTLMTestData ().asyncNTLMTest("http://myurl/FileName.csv", this, 
    getApplicationContext());
 } catch (Exception e){
    showError();
 }

}

private void showError(){
    mDialog = new ProgressDialog(MainActivity.this);
    mDialog.setTitle("Error")
    mDialog.setMessage("An error has occurred during download");
    mDialog.setPositiveButton("Ok", new 
    DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int which) { 
            mDialog.dismiss();
        }
     })
    mDialog.setCancelable(false);
    mDialog.show();
}

// The below call backs get fired from async    
@Override
public void onSuccess(String msg) {
    mDialog.dismiss();
}

@Override
public void onFailure(Exception ex) {
   //Leave blank
}

希望有所帮助