Asynctask 在 5 分钟后超时 [android]



我在AsyncTask doInBackground中运行了一个长时间运行的进程(这是一个servlet调用),该进程可能会运行超过5分钟。但是 5 分钟后,servlet 返回,我遇到了超时问题。

下面是我的代码:

import java.util.ArrayList;
import java.util.List;
import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import com.example.hanamom.LoginActivity.GetXMLTask;
import android.app.Activity;
import android.app.ActivityManager;
import android.app.ActivityManager.RunningTaskInfo;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.ComponentName;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.app.ProgressDialog;
public class NewInstallationActivity extends Activity {
Button install;
EditText branch_enter;
EditText cl_enter;
EditText sid_enter;
EditText sidpwd_enter;
EditText systempwd_enter;
EditText instance_enter;
EditText installationlocation_enter;
Bundle extras;
EditText desc_enter;
ProgressDialog pd;
public static final String URL = "<a servlet call>";
ArrayList<NameValuePair> postParameters = new ArrayList<NameValuePair>();
Intent intent;
String login_user,host,rootuser,rootpassword;
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.install);
    addListenerOnButton();
    Log.e("test", "Entered New Instlalaiton activity");
    branch_enter = (EditText) findViewById(R.id.branch_enter);
    cl_enter = (EditText) findViewById(R.id.cl_enter);
    sid_enter = (EditText) findViewById(R.id.sid_enter);
    sidpwd_enter = (EditText) findViewById(R.id.sidpwd_enter);
    systempwd_enter = (EditText) findViewById(R.id.systempwd_enter);
    instance_enter=(EditText) findViewById(R.id.instance_enter);
    installationlocation_enter=(EditText) findViewById(R.id.installationlocation_enter);

    Bundle extras = this.getIntent().getExtras();
     login_user=extras.getString("login_user").trim();
     host = extras.getString("promptshost");
     rootuser = extras.getString("lrootUser");
     rootpassword = extras.getString("lrootpwd");
     Log.e("login user",login_user);
     Log.e("rootuser=",rootuser);
     Log.e("rootpassword=",rootpassword);

}
private void addListenerOnButton() {
    // TODO Auto-generated method stub
    final Context context = this;
    branch_enter = (EditText) findViewById(R.id.branch_enter);
    cl_enter = (EditText) findViewById(R.id.cl_enter);
    sid_enter = (EditText) findViewById(R.id.sid_enter);
    sidpwd_enter = (EditText) findViewById(R.id.sidpwd_enter);
    systempwd_enter = (EditText) findViewById(R.id.systempwd_enter);
    instance_enter=(EditText) findViewById(R.id.instance_enter);
    installationlocation_enter=(EditText) findViewById(R.id.installationlocation_enter);
    install = (Button) findViewById(R.id.install);
    install.setOnClickListener(new OnClickListener() {
        public void onClick(View arg0) {
            GetXMLTask task = new GetXMLTask();
            task.execute(new String[] { URL });
        }
    });
  }
      private class GetXMLTask extends AsyncTask<String, Void, String> {

        String response = "false";
        private ProgressDialog pd;
        protected void onPreExecute() {
            pd = new ProgressDialog(NewInstallationActivity.this);
            pd.setMessage("Please wait while SAP HANA system is being Installed..");
            pd.setIndeterminate(false);
            pd.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
            pd.setProgress(0);
            pd.show();
        }

        protected String doInBackground(String... urls) {
            try {

                Log.e("NewInstallationActivty", "New installation");
                String branch=branch_enter.getText().toString();
                String cl = cl_enter.getText().toString();
                String sid=sid_enter.getText().toString();
                String sidpwd=sidpwd_enter.getText().toString();
                String systempwd=systempwd_enter.getText().toString();
                String instance=instance_enter.getText().toString();
                String installationlocation=installationlocation_enter.getText().toString();
                Log.e("Branch=",branch);
                Log.e("Host=",host);
                Log.e("sid=",sid);
                Log.e("sidpwd=",sidpwd);
                Log.e("systempwd=",systempwd);
                Log.e("instance=",instance);
                Log.e("installationlocation=",installationlocation);
                Log.e("rootuser=",rootuser);
                Log.e("rootpassword=",rootpassword);
            //  String osuser=osuser_enter.getText().toString();
            //  String ospassword=ospassword_enter.getText().toString();

                    Log.e("NewInstlalationActivity", "installtion");
                        postParameters.add(new BasicNameValuePair("branch",branch));
                        postParameters.add(new BasicNameValuePair("cl", cl));
                        postParameters.add(new BasicNameValuePair("sid", sid));
                        postParameters.add(new BasicNameValuePair("sidpwd", sidpwd));
                        postParameters.add(new BasicNameValuePair("systempwd",systempwd));
                        postParameters.add(new BasicNameValuePair("instance",instance));
                        postParameters.add(new BasicNameValuePair("installationlocation",installationlocation));
                        postParameters.add(new BasicNameValuePair("host",host));
                        postParameters.add(new BasicNameValuePair("rootuser", rootuser));
                        postParameters.add(new BasicNameValuePair("rootpassword", rootpassword));
                        postParameters.add(new BasicNameValuePair("intent","install"));
                        Log.e("NewInstlalationActivity", "post parameters");
                        for (String url : urls) {
                            response = CustomHttpClient.executeHttpPost(url,postParameters);
                     }
        }
    catch(Exception e) {}
            return "Success";
}

        protected void onPostExecute(String output) {
             Log.e("Login Activity",response);
             while(!response.trim().equals("True"))
             {
                 pd.show();
                 pd.setProgress(100);
                 continue;
             }
                if (response.trim().equals("True"))
                {
                    Log.e("Entered Response = True", response);
                pd.dismiss();
                Intent intent = new Intent(getBaseContext(), NewInstallationActivity.class);
                startActivity(intent);
                }
                else
                {
                    Log.e("Else code","Else");
                    AlertDialog alertDialog = new AlertDialog.Builder(
                            NewInstallationActivity.this).create();

            alertDialog.setTitle("Installation Failed!");
            // Setting Dialog Message
            alertDialog.setMessage(" Check logs at /var/tmp");
            // Setting Icon to Dialog
            //alertDialog.setIcon(R.drawable.tick);
            // Setting OK Button
            alertDialog.setButton("OK", new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int which) {
                    // Write your code here to execute after dialog closed
  Toast.LENGTH_SHORT).show();
                    }
            });
            // Showing Alert Message
            alertDialog.show();
                }
        }

}
}

在这里,我的进程对话框将等到我从 servlet 获得 True,但恰好在 5 分钟后,它正在发送一个 false。

有什么方法可以等待超过5分钟。任何提示都会有所帮助。

谢谢。

您可以尝试以下操作:

private final static long TIMETOWAIT = 30000;  // Set your timeout.
....
GetXMLTask.get(TIMETOWAIT,TimeUnit.MILLISECONDS);
....

供参考: http://developer.android.com/reference/android/os/AsyncTask.html#get%28long,%20java.util.concurrent.TimeUnit%29

相关内容

  • 没有找到相关文章

最新更新