我在一个活动中创建了3个视图,具体取决于工作流程的过程。 即viewA-> viewB-> viewC,然后在viewC上,当我执行HTTP-POST(使用AsyncTask)时,显示进度对话框。 我试过两种方法来显示进度对话框:
任何人都可以提供帮助吗?
由于
SAM
以下是主要活动代码:
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;
}
}
答案 0 :(得分: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!");
}
}
答案 1 :(得分: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