Android - 无法从自定义 AsyncTask class 更新 activity ProgressDialog

Android - Can't update the activity ProgressDialog from custom AsyncTask class

我创建了一个名为 ConnectionHttp 的自定义 AsyncTask class。其中要return一个Json字符串,在主activity(test1.java)中通过一个按钮调用。连接一切正常,我可以获取字符串。

关键是我想在后台任务运行时在主 activity 上显示 ProgressDialog,但它不起作用。我没有看到任何 ProgressDialog。

1) 如何修复 progressDialog

2) 我如何解决日志中的问题(应用程序可能做了太多工作......)(我在真实设备上测试)

这是我的代码:

test1.java(主要activity)

public class test1 {

 public static final String URL = ".....";
private ProgressDialog pDialog;
private TextView tv;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_test1);


    pDialog = new ProgressDialog(test1.this);
    tv = (TextView)findViewById(R.id.tvTest);

    Button btRun = (Button) findViewById(R.id.btTestRun);
    btRun.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            try {
                ConnectionHttp conn = new ConnectionHttp(test1.this, pDialog, URL);

                String str = conn.execute().get();
                tv.setText(str);

            } catch (InterruptedException e) {
                e.printStackTrace();
            } catch (ExecutionException e) {
                e.printStackTrace();
            }
        }
    });
}

ConnectionHttp.java

public class ConnectionHttp extends AsyncTask {

private ProgressDialog pDialog;
private Context context;
private String LOGIN_URL;
private JSONParser jsonParser;

// TAGS
private final String TAG_SUCCESS = "success";
private final String TAG_MESSAGE = "message";
private final String TAG_VALUES = "values";

public ConnectionHttp(Context context, ProgressDialog pDialog, String url) {
    this.context = context;
    this.pDialog = pDialog;
    this.LOGIN_URL = url;
    this.jsonParser = new JSONParser();
}



@Override
public void onPreExecute() {
    super.onPreExecute();
    pDialog.setMessage("Connexion au serveur distant...");
    pDialog.setIndeterminate(false);
    pDialog.setCancelable(true);
    pDialog.show();
}

@Override
public String doInBackground(String... args) {
    // TODO Auto-generated method stub
    int success;
    String values = "";
    try {

        List<NameValuePair> params = new ArrayList<>();
        // adding parameters
        //  ...
        params.add(.....);

         //

        Log.d("request #1", "starting");

        JSONObject json = jsonParser.makeHttpRequest(LOGIN_URL, "POST", params);

        // success tag for json
        success = json.getInt(TAG_SUCCESS);
        if (success == 1) {
            Log.d("request #1", "Successfull");
            values =  json.getString(TAG_VALUES);
            return json.getString(TAG_MESSAGE);
        }else{

            return json.getString(TAG_MESSAGE);

        }
    } catch (JSONException e) {
        e.printStackTrace();
    }

    return values;
}

public void onPostExecute(String message) {
    Log.d("request #1", "done.");
    pDialog.setMessage("done.");
    pDialog.dismiss();
} 

Android 日志

D/request #1﹕ starting

D/request #1 : Successfull !

I/Choreographer﹕ Skipped 49 frames! The application may be doing too much work on its main thread.

D/request #1﹕ done

不要将进度对话框传递给异步 class。而是在 Async class.

上创建它

修改onPreExecute为这个

@Override
public void onPreExecute() {
   super.onPreExecute();
   pdialog = new ProgressDialog(getActivity(),"","Connexion au serveur distant...");
   pDialog.setIndeterminate(false);
   pDialog.setCancelable(true);
   pDialog.show();
}

从 test1.java

中删除进度对话框

首先从 AsyncTask 中删除进度对话框并使用传递的上下文创建 ProgressDialog;

public ConnectionHttp(Context context, ProgressDialog pDialog, String url) {
this.context = context;
this.pDialog = new ProgressDialog(context);
this.LOGIN_URL = url;
this.jsonParser = new JSONParser();
}