我收到了IllegalStateException

时间:2014-05-20 14:43:25

标签: android android-asynctask

FATAL EXCEPTION: main    
java.lang.IllegalStateException: Cannot execute task: the task is already running.
    at android.os.AsyncTask.executeOnExecutor(AsyncTask.java:550)
    at android.os.AsyncTask.execute(AsyncTask.java:511)
    at com.bugs3.munishgupta.cabup.ForgotPassword$1.onClick(ForgotPassword.java:59)
    at android.view.View.performClick(View.java:3511)
    at android.view.View$PerformClick.run(View.java:14105)
    at android.os.Handler.handleCallback(Handler.java:605)
    at android.os.Handler.dispatchMessage(Handler.java:92)
    at android.os.Looper.loop(Looper.java:137)
    at android.app.ActivityThread.main(ActivityThread.java:4424)
    at java.lang.reflect.Method.invokeNative(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:511)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:825)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:592)
    at dalvik.system.NativeStart.main(Native Method)

以下是ForgotPassword.java

  package com.bugs3.munishgupta.cabup;

  import java.util.concurrent.ExecutionException;

  import org.json.JSONException;
  import org.json.JSONObject;

  import android.app.Activity;
  import android.content.Intent;
  import android.os.Bundle;
  import android.view.Menu;
  import android.view.View;
  import android.view.View.OnClickListener;
  import android.widget.Button;
  import android.widget.EditText;
  import android.widget.Toast;

  public class ForgotPassword extends Activity {
  EditText etFgMob, etFgEmail;
  Button btFgPass;
  String em, mob;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.forgot_password);
        etFgEmail=(EditText)findViewById(R.id.etFgEmail);
        etFgMob=(EditText)findViewById(R.id.etFgMob);
        btFgPass=(Button)findViewById(R.id.btFgPass);
        btFgPass.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                em=etFgEmail.getText().toString();
                mob=etFgMob.getText().toString();
                if(!(em.equals(null) || mob.equals(null)))
                {
                    if(android.util.Patterns.EMAIL_ADDRESS.matcher(em).matches()==false)
                        etFgEmail.setError("Enter valid Email");
                    else if(mob.length() != 10)
                        etFgMob.setError("Enter valid Mobile no.");
                    else
                    {
                        JSONGetRequest jRequest = JSONGetRequest
                                .getSingleInstance(ForgotPassword.this);

                        try {

                            String url = (Tags.url + "op=select&query=select password from driver"
                                    + " where mob='"+ mob +"' and email='" + em + "'")
                                    .replaceAll(" ", "%20");

                            JSONObject json = jRequest.execute(url).get();
                            if (json.getInt("success") == 0) {
                                jRequest.cancel(true);
                                url=(Tags.url + "op=select&query=select password from customer"
                                        + " where mob='"+ mob +"' and email='" + em + "'")
                                        .replaceAll(" ", "%20");

                                json = jRequest.execute(url).get();
                            }
                            if (json.getInt("success") == 0) {
                                jRequest.cancel(true);
                                url=(Tags.url + "op=select&query=select password from agency"
                                        + " where mob='"+ mob +"' and email='" + em + "'")
                                        .replaceAll(" ", "%20");
                                json = jRequest.execute(url).get();
                            }
                            if (json.getInt("success") == 1) {
                                Intent i = new Intent(ForgotPassword.this,
                                        Login.class);
                                json=(json.getJSONArray("entities")).getJSONObject(0);
                                Toast.makeText(getApplicationContext(),
                                        json.getString("password"), Toast.LENGTH_SHORT)
                                        .show();
                                startActivity(i);
                            } else {

                                Toast.makeText(getApplicationContext(),
                                    json.getString("no account exist"), Toast.LENGTH_SHORT)
                                    .show();

                            }

                        } catch (InterruptedException e) {
                            // TODO Auto-generated catch block
                            e.printStackTrace();
                        } catch (ExecutionException e) {
                            // TODO Auto-generated catch block
                            e.printStackTrace();
                        } catch (JSONException e) {
                            // TODO Auto-generated catch block
                            e.printStackTrace();
                        }
                    }
                }
            }
        });

    }
    @Override
    protected void onPause() {
        // TODO Auto-generated method stub
        super.onPause();
        finish();
    }
  }

1 个答案:

答案 0 :(得分:2)

您连续多次致电execute()

JSONObject json = jRequest.execute(url).get();
if (json.getInt("success") == 0) {
    jRequest.cancel(true);
    url=(Tags.url + "op=select&query=select password from customer"
            + " where mob='"+ mob +"' and email='" + em + "'")
            .replaceAll(" ", "%20");

    json = jRequest.execute(url).get();
}
if (json.getInt("success") == 0) {

    url=(Tags.url + "op=select&query=select password from agency"
            + " where mob='"+ mob +"' and email='" + em + "'")
            .replaceAll(" ", "%20");
    json = jRequest.execute(url).get();
}

您可以在此处第二个jRequest.cancel(true)声明的顶部调用if来解决此问题。