0

根据工作流程的过程,我在一项活动中创建了 3 个视图。即 viewA->viewB->viewC,然后在 viewC 上,当我执行 HTTP-POST(使用 AsyncTask)时,显示进度对话框。我尝试了两种显示进度对话框的方法:

  1. 使用runOnUiThread() 显示progressDialog,它没有显示。
  2. 在 AsyncTask 中编写 show progressDialog 代码。在 onPreExecute() 中显示进度对话框并在 onPostExecute() 中关闭,它显示在 doinbackground 任务之后,并且 onPostExecute() 也没有执行。

任何人都可以帮忙吗?

谢谢

山姆

这是主要的活动代码:

 public void setA(){
    setContentView(R.layout.a_fm);
    Button aNextBtn=(Button)findViewById(R.id.aNextBtn);
    aNextBtn.setOnClickListener(this);
 }
 public void setB(){
    setContentView(R.layout.b_fm);
    Button bNextBtn=(Button)findViewById(R.id.bNextBtn);
    bNextBtn.setOnClickListener(this);
 }
 public void setC(){
    setContentView(R.layout.c_fm);
    Button cNextBtn=(Button)findViewById(R.id.cNextBtn);
    cNextBtn.setOnClickListener(this);
}
   public void onClick(View v) {
    // TODO Auto-generated method stub
    switch (v.getId()) {
    case R.id.aNextBtn:
                setB();
                break;
    case R.id.bNextBtn:
                setB();
                break;
    case R.id.cNextBtn:
                postmsg();
                break;
       }
    }
    public void postmsg(final Info info)
{
    postDialog=new ProgressDialog(AssistFm.this);
    postDialog.setMessage(getString(R.string.alert_sendmsg_sending));
    postDialog.show();
    this.runOnUiThread(new Runnable() {@Override
        public void run() {
            // TODO Auto-generated method stub
            send_online=sendlogtowebservice(info);
            SEND_COUNT++;
            if (send_online)
            {
                postDialog.dismiss();
                AlertDialog.Builder builder = new Builder(A_activity.this);
                builder.setMessage(getString(R.string.alert_sendmsg_success));  
                builder.setTitle(getString(R.string.sendmsg_title)); 
                builder.setPositiveButton(getString(R.string.button_OK), new DialogInterface.OnClickListener() {   
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.dismiss();           
                        setA();
                        SEND_COUNT=0;
                    }
                });
                builder.create().show();    
            }
            else 
            {  
                postDialog.dismiss();
                if (SEND_COUNT<SEND_COUNT_MAX)
                {
                    AlertDialog.Builder builder = new Builder(A_activity.this);
                    builder.setMessage(getString(R.string.alert_sendmsg_retry));  
                    builder.setTitle(getString(R.string.sendmsg_title)); 
                    builder.setNegativeButton(getString(R.string.button_Cancel), new DialogInterface.OnClickListener() {   
                        public void onClick(DialogInterface dialog, int which) {
                            dialog.dismiss();       
                        }
                    });

                    builder.setPositiveButton(getString(R.string.button_OK), new DialogInterface.OnClickListener() {   
                        public void onClick(DialogInterface dialog, int which) {
                            dialog.dismiss();
                            postmsg(info);
                            SEND_COUNT++;
                        }
                    });
                    builder.create().show();
                }
                else 
                {
                    AlertDialog.Builder builder = new Builder(AssistFm.this);
                    builder.setMessage(getString(R.string.alert_sendmsg_error));  
                    builder.setTitle(getString(R.string.sendmsg_title)); 

                    builder.setPositiveButton(getString(R.string.button_OK), new DialogInterface.OnClickListener() {   
                        public void onClick(DialogInterface dialog, int which) {
                            dialog.dismiss();
                            setA();
                        }
                    });
                    builder.create().show();
                }
            }
        }
    });

 }
 private boolean sendlogtowebservice(Info info) {
    boolean isTrue = false;
    int result_code = 0;
    Object []param = new Object[3];  
    HttpResponse response = null;    
    String result_str;
    try {   
        String sURL=url;
        HttpClient client = new DefaultHttpClient();        
        ArrayList<BasicNameValuePair> paierList = new ArrayList<BasicNameValuePair>();
        paierList.add(new BasicNameValuePair("person_firstname", info.person_firstname)); 
        paierList.add(new BasicNameValuePair("person_lastname", info.person_lastname)); 
        paierList.add(new BasicNameValuePair("person_mobile", info.person_mobile)); 

        param[0] = sURL;  
        param[1] = paierList;  
        param[2] = client;  
        AsyncTask<Object, Object, HttpResponse> res  = new HttpReqTask().execute(param);
        response = (HttpResponse) res.get();  
        result_code=response.getStatusLine().getStatusCode();
        result_str = EntityUtils.toString(response.getEntity());
        if ( result_str.equals("00"))
        {
            isTrue = true;
        }
    } 
    catch (ClientProtocolException  e) {
        e.printStackTrace();
    }
    catch (IOException e) 
    { 
       // TODO Auto-generated catch block 
       e.printStackTrace(); 
    } 
    catch (Exception e) 
    {  
        Log.e("HttpAPI.callHttpPost()", "Error", e);  
    }  
    return isTrue;

};

这是 HTTP-POST AsyncTask 代码:

 import java.util.ArrayList;

 import org.apache.http.HttpResponse;
 import org.apache.http.NameValuePair;
 import org.apache.http.client.HttpClient;
 import org.apache.http.client.entity.UrlEncodedFormEntity;
 import org.apache.http.client.methods.HttpPost;
 import org.apache.http.protocol.HTTP;

 import android.os.AsyncTask;


 public class HttpReqTask extends AsyncTask<Object, Object, HttpResponse>{

  @Override  
  protected HttpResponse doInBackground(Object... params){  
    String url = (String)params[0];  
    ArrayList<NameValuePair> paierList  = (ArrayList<NameValuePair>)params[1];  
    HttpClient httpclient  = (HttpClient)params[2];  

    HttpPost request = new HttpPost(url);  
    HttpResponse response = null;  
     try {  
        request.setEntity(new UrlEncodedFormEntity(paierList, HTTP.UTF_8));  
        response = httpclient.execute(request);  
    } catch (Exception e) {  
        throw new RuntimeException(e);  
    }  
    return response;  
}  

 }  
4

2 回答 2

0

似乎没有人知道答案...根据我的研究,我可以通过创建一个新线程来显示progessDialog,但是新线程中的AlertDialog现在无法显示...

    public void postmsg(final OnlineHistoryInfo info)
{
    dialog=new ProgressDialog(mActivity.this);
    dialog.setMessage(getString(R.string.alert_sendmsg_sending));
    dialog.setCancelable(false);
    dialog.show();
    myLog.i("dialog show!");        
    new Thread(){
        @Override
        public void run()
        {
            try 
            {
                myLog.i("new thread: run!");
                // TODO Auto-generated method stub
                send=sendlogtowebservice(info);
                //send_online=true;
                SEND_COUNT++;

            }
            finally
            {
                dialog.dismiss();
               if (send)
                {
                    myLog.i("send:true!");
                    new DataRule(AssistFm.this).saveOnlineHistory(info);
                    AlertDialog.Builder builder = new Builder(mActivity.this);
                    builder.setMessage(getString(R.string.alert_sendmsg_success));  
                    builder.setTitle(getString(R.string.sendmsg_title)); 
                    builder.setPositiveButton(getString(R.string.button_OK), new DialogInterface.OnClickListener() {   
                        public void onClick(DialogInterface dialog, int which) {
                            setA();
                            SEND_COUNT=0;
                        }
                    });
                    builder.create().show();    
                }
                else 
                {  
                    myLog.i("send:false!");
                    if (SEND_COUNT<SEND_COUNT_MAX)
                    {
                        AlertDialog.Builder builder = new Builder(mActivity.this);
                        builder.setMessage(getString(R.string.alert_sendmsg_retry));  
                        builder.setTitle(getString(R.string.sendmsg_title)); 
                        builder.setNegativeButton(getString(R.string.button_Cancel), new DialogInterface.OnClickListener() {   
                            public void onClick(DialogInterface dialog, int which) {
                                dialog.dismiss();       
                            }
                        });

                        builder.setPositiveButton(getString(R.string.button_OK), new DialogInterface.OnClickListener() {   
                            public void onClick(DialogInterface dialog, int which) {
                                postmsg(info);
                                SEND_COUNT++;
                            }
                        });
                        builder.create().show();
                    }
                    else 
                    {
                        AlertDialog.Builder builder = new Builder(mActivity.this);
                        builder.setMessage(getString(R.string.alert_sendmsg_error));  
                        builder.setTitle(getString(R.string.sendmsg_title)); 

                        builder.setPositiveButton(getString(R.string.button_OK), new DialogInterface.OnClickListener() {   
                            public void onClick(DialogInterface dialog, int which) {
                                setA();
                            }
                        });
                        builder.create().show();
                    }
                }
            }
        }
    }.start();



}//postmsg() finish
于 2013-08-08T06:24:45.210 回答
0

没有人知道这是怎么回事?

我将 AsyncTask 更改为下面,当它试图关闭进度对话框时,进度对话框只是显示一个冲洗掉。

public class HttpReqTask extends AsyncTask<Object, Object, HttpResponse>{
private Context mCtx;
private ProgressDialog progressDialog;

public HttpReqTask(Context  context){     
    myLog.i("HttpReqTask constructor, before show dialog!");  
            progressDialog = new ProgressDialog(mCtx);
    progressDialog.setMessage("Your progress dialog message...");
            progressDialog.show();  
            myLog.i("HttpReqTask constructor finish, start show dialog!");  
}

@Override  
protected HttpResponse doInBackground(Object... params){  
    myLog.i("do in background execute");  
    String url = (String)params[0];  
    ArrayList<NameValuePair> paierList  = (ArrayList<NameValuePair>)params[1];  
    HttpClient httpclient  = (HttpClient)params[2];  
    String s=null;  
    HttpPost request = new HttpPost(url);  
    HttpResponse response = null;  
     try {  
        request.setEntity(new UrlEncodedFormEntity(paierList, HTTP.UTF_8));  
        response = httpclient.execute(request); 
        //add for longer process time.
        for (int i=0;i<10000000;i++)
        {
            s="a";
        }
    } catch (Exception e) {  
        throw new RuntimeException(e);  
    }  
     myLog.i("finish in background execute");  
    return response;  
}  

@Override    
protected void onPostExecute(HttpResponse result) {     
    myLog.i("HttpReqTask onPostExecute(), before dismiss dialog!");  
    if (progressDialog!=null) progressDialog.dismiss();      
    myLog.i("HttpReqTask onPostExecute(), after dismiss dialog!");  
}      

}

于 2013-08-07T15:28:32.643 回答