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