0

I have an AsyncTask in my activity class and when I check some data in doInBackground(), I just want to change/set an instance variable of my activity class, but somehow there is nothing what is changing! :( And if the variable is changed another AsyncTask should start.

Now here is the code:

public class LogIn extends Activity {

private boolean emailNotAvalaible;

private void setemailNotAvalaible(boolean emailNotAvalaible) {
    this.emailNotAvalaible= emailNotAvalaible;
}

private Button loginBtn;


 @Override
     protected void onCreate(Bundle savedInstanceState) {
     super.onCreate(savedInstanceState);
         setContentView(R.layout.login_activity);


 loginBtn = (Button) findViewById(R.id.login_btn);

       loginBtn.setOnClickListener(new View.OnClickListener() {
    public void onClick(View v) {

              new Register().execute("");

              if (emailNotAvalaible== true) {

         new Installation().execute("");

          }

            }// end of onClick()
       });// end of setOnClickListener

}// end of onCreate();


    public class Register extends AsyncTask<String,Integer,String>{ 

       @Override
   protected void onPreExecute() {

       ...

       }//end of onPreExecute()

       @Override
   protected String doInBackground(String... params) {

       ArrayList<NameValuePair> postParamsEmail = new ArrayList<NameValuePair>();
       postParamsEmail.add(new BasicNameValuePair("email", email));

         try {

            String emailCheck = executeHttpPost("http://.../doubleEmail.php", postParamsEmail);

    try {

               JSONArray jsonarr = new JSONArray( emailCheck ); 
               String emailAvalaible = jsonarr.getString(0);

            if( emailAvalaible.equals("no") ){ doubleEmail = "no"; }else{ doubleEmail = "yes"; }

     } catch (JSONException e) {
            e.printStackTrace();
     }
     } catch (Exception e1) {
       e1.printStackTrace();
     }

       }

     return "String";

    }// end of doInBackground()

    @Override
    protected void onPostExecute(String result){
        super.onPostExecute(result);

        if (doubleEmail.equals("no")){
             LogIn.this.setEmailNotAvalaible(true);
        }

    }

    }//end of AsyncTask class

    private static HttpClient getHttpClient() {
    if (mHttpClient == null) {
        mHttpClient = new DefaultHttpClient();
        final HttpParams params = mHttpClient.getParams();
        HttpConnectionParams.setConnectionTimeout(params, HTTP_TIMEOUT);
        HttpConnectionParams.setSoTimeout(params, HTTP_TIMEOUT);
        ConnManagerParams.setTimeout(params, HTTP_TIMEOUT);
    }
    return mHttpClient;
    }//end of getHttpClient()

    public static String executeHttpPost(String url, ArrayList<NameValuePair> postParameters) throws Exception {

    BufferedReader in = null;

    try {
        HttpClient client = getHttpClient();
        HttpPost request = new HttpPost(url);
        UrlEncodedFormEntity formEntity = new UrlEncodedFormEntity(postParameters);
        request.setEntity(formEntity);
        HttpResponse response = client.execute(request);
        in = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));

        StringBuffer sb = new StringBuffer("");
        String line = "";
        String NL = System.getProperty("line.separator");
        while ((line = in.readLine()) != null) {
            sb.append(line + NL);
        }
        in.close();

        String result = sb.toString();
        return result;

    } finally {
        if (in != null) {
            try {
                in.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
    }//end of executeHttpPost()

    }//end of activity class

Some code is not shown, but this code isn't important for the solution. The php-file just checks if the entered email does exist in the database. So, the major question is how can I easily change the variable 'emailNotAvalaible' in doInBackground or in onPostExecute?

Thanks for your help!!!

EDIT:

Hello again, thanks for everybodys help, to change the variable works fine, but I guess my problem is, that before my Register AsyncTask is allready finished, the new AsyncTask proofs the variable and wants to start, but just a second after that the variable is set. So, How can I ensure that the second AsyncTask only starts when the first AsyncTask is Allready finished? thanks for your help guys!!!

3 Answers3

0

this should not be an issue. here is an example that works fine:

    public class Register extends AsyncTask<String,Integer,String>{ 

    @Override
    protected void onPreExecute() {

        Log.d("", "on pre bool: " + bool);


    }//end of onPreExecute()

    @Override
    protected String doInBackground(String... params) {
        bool = true;
        return "";
    }

    @Override
    protected void onPostExecute(String result){
        super.onPostExecute(result);
        Log.d("", "on post, bool: " + bool);
    }

}

where bool = private boolean in your main activity. here is the logcat:

 07-19 11:57:25.943: D/(21843): on pre bool: false
 07-19 11:57:29.736: D/(21843): on post, bool: true

my guess is that your variable, doubleEmail, is not getting set to "no".

droideckar
  • 1,077
  • 10
  • 20
0

There are several ways but the postExecute method can solve your problem look this: how to pass the result of asynctask onpostexecute method into the parent activity android

Community
  • 1
  • 1
Tobiel
  • 1,543
  • 12
  • 19
0

So, I think I have found at least one solution for my problem, this is maybe not the best one, but it works fine.

Now, for those who are interested in my solution.

I have found it here : multithreading , thanks to Boris Strandjev

I have chosen the 'get' - option : new Register().execute("").get(2000, TimeUnit.MILLISECONDS);

If there is any better solution, please tell me, otherwise thanks for trying to help me!

Community
  • 1
  • 1