user3926415
user3926415

Reputation: 3

onPostExecute isn't called in AsyncTask

For some reason this isn't working. I do get a response from doInBackground and have saved it in result. I'm able to see the result after doInBackground and the correct result is received, its just that onPostExecute isn't running.

private class AttemptLogin extends AsyncTask<String, Void, Void>
    {
        String result;
        TextView error =(TextView)findViewById(R.id.errorlogin);
        @Override
        protected void onPreExecute() {}
        @Override
        protected Void doInBackground(String... params) {


            HttpClient httpclient = new DefaultHttpClient();
            HttpPost httppost = new HttpPost(url);

            try {

                nameValuePairs.add(new BasicNameValuePair("username", params[0]));
                nameValuePairs.add(new BasicNameValuePair("password", params[1]));

                httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
                System.out.print(nameValuePairs);

                result = httpclient.execute(httppost, new BasicResponseHandler());
                System.out.print(result);


                //System.out.println("check");
                nameValuePairs.clear();




            } catch (ClientProtocolException e) {

                System.out.println("check1");


            } catch (IOException e) {

                System.out.println(e.getMessage());
            }

            return null;
        }
        @Override
        protected void onProgressUpdate(Void... values) {}
        @Override
        protected void onPostExecute(Void voids) {
            super.onPostExecute(voids);
            if (result.equals("Success"))
            {validate();
            System.out.print(result);}

            else if (result.equals("Failure")){
                System.out.print(result);
                error.setText("Don't match");
            }
            else{
                System.out.print("no response");
            }

        }






    }

Upvotes: 0

Views: 350

Answers (1)

nouseforname
nouseforname

Reputation: 748

I think you have to pass the result as argument like it is supposed to be:

Check this sample:

class AttemptLogin extends AsyncTask<String, Void, String>
{
    TextView error =(TextView)findViewById(R.id.errorlogin);

    @Override
    protected void onPreExecute() {}

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

        if (params[0].equals(""))
            return null;

        String result = null;
        HttpClient httpclient = new DefaultHttpClient();
        HttpPost httppost = new HttpPost(url);

        try {

            nameValuePairs.add(new BasicNameValuePair("username", params[0]));
            nameValuePairs.add(new BasicNameValuePair("password", params[1]));

            httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
            System.out.print(nameValuePairs);

            result = httpclient.execute(httppost, new BasicResponseHandler());
            System.out.print(result);

            //System.out.println("check");
            nameValuePairs.clear();

            return result.trim();

        } catch (ClientProtocolException e) {

            System.out.println("check1");
            return null;

        } catch (IOException e) {

            System.out.println(e.getMessage());
            return null;
        }
    }
    @Override
    protected void onProgressUpdate(Void... values) {}

    @Override
    protected void onPostExecute(String result) {

        if (result.equals("Success"))
        {
            validate();
            System.out.print(result);}
        }
        else if (result.equals("Failure")){
            System.out.print(result);
            error.setText("Don't match");
        }
        else{
            System.out.print("no response");
        }
    }
}

Upvotes: 1

Related Questions