user2003256
user2003256

Reputation: 15

Toast in AsyncTask

Can you guys check my codes, i don't know if it's constructed properly, i need to display toast and run a class after the doInBackGround process is done. Well it's not working, so can you guys help me here. Thanks.

This is my codes:

**

class phpconnect extends AsyncTask<String, String, String>{
        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            pDialog = new ProgressDialog(Main.this);
            pDialog.setMessage("Logging in..");
            pDialog.setIndeterminate(false);
            pDialog.setCancelable(true);
            pDialog.show();
        }

        @Override
        protected String doInBackground(String... params) {
             ArrayList<NameValuePair> postParameters = new ArrayList<NameValuePair>();
                postParameters.add(new BasicNameValuePair("eadd", inputEmail.getText().toString()));
                postParameters.add(new BasicNameValuePair("password", inputPassword.getText().toString()));
                //Passing Parameter to the php web service for authentication
                //String valid = "1";
                String response = null;
                try {
                response = CustomHttpClient.executeHttpPost("http://10.0.2.2:8080/TheCalling/log_in.php", postParameters);  //Enter Your remote PHP,ASP, Servlet file link
                String res=response.toString();
                //res = res.trim();
                res= res.replaceAll("\\s+","");
                //error.setText(res);

                } catch (Exception e) {
                    return "1";
                }
            return null;
        }

        /**
         * After completing background task Dismiss the progress dialog
         * **/
        protected void onPostExecute(String result) {
            // dismiss the dialog once done
             pDialog.dismiss();
            if(result.equalsIgnoreCase("1")){
                //Display Toast
                Toast.makeText( getApplicationContext(),"Sorry!! Incorrect Username or Password",Toast.LENGTH_SHORT ).show();
             }else{
                    Toast.makeText( getApplicationContext(),"Correct Username or Password",Toast.LENGTH_SHORT ).show();
                    Intent i = new Intent(Main.this,MainMenu.class);
                    startActivity(i);
             }

        }

**

Upvotes: 2

Views: 298

Answers (2)

Janmejoy
Janmejoy

Reputation: 2731

private class DownloadFilesTask extends AsyncTask<URL, Integer, Long> {
     protected Long doInBackground(URL... urls) {
         int count = urls.length;
         long totalSize = 0;
         for (int i = 0; i < count; i++) {
             totalSize += Downloader.downloadFile(urls[i]);
             publishProgress((int) ((i / (float) count) * 100));
             // Escape early if cancel() is called
             if (isCancelled()) break;
         }
         return totalSize;
     }

     protected void onProgressUpdate(Integer... progress) {
         setProgressPercent(progress[0]);
     }

     protected void onPostExecute(Long result) {
         showDialog("Downloaded " + result + " bytes");
     }
 }

For more information check this link

Upvotes: 0

jelgh
jelgh

Reputation: 706

Pass a Context to a constructor of this AsyncTask. An AsyncTask does not extend Context itself so you cannot use getApplicationContext()

class phpconnect extends AsyncTask<String, String, String>{

    private final Context mContext;

    public phpconnect(final Context context) {
        mContext = context;
    }


    [...]

    protected void onPostExecute(String result) {
        [...]
        Toast.makeText( mContext,"...",Toast.LENGTH_SHORT ).show();
        [...]
    }

Edit: Also add a NullPointer guard to your if-clause. Something like:

if(result != null && result.equalsIgnoreCase("1")){

Upvotes: 1

Related Questions