Asynctask在5分钟后超时[android]

时间:2014-12-17 15:39:48

标签: android servlets android-asynctask timeout

我在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分钟。任何提示都会有所帮助。

感谢。

1 个答案:

答案 0 :(得分:0)

您可以尝试以下操作:

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