0

My requirement:-

In My Android project I am using javamail to connect to gmail .. So,I want When I click the button,A progress dialog will start and in the background it will try to connect to gmail..If it the username and password is correct,means it connects to gmail..then will go to the next activity..otherwise it will dissmiss the progress dialog and show a toast --"something is wrong"

So I have this:--

 button1.setOnClickListener(new OnClickListener() {

                    @Override
            public void onClick(View v) {


                     props = new Properties();
                    props.put("mail.smtp.host", "smtp.gmail.com");
                    props.put("mail.smtp.socketFactory.port", "465");
                    props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
                    props.put("mail.smtp.auth", "true");
                    props.put("mail.smtp.port", "465");
                    pdialog = ProgressDialog.show(context, "", "Please wait...", true);

                                    session = Session.getDefaultInstance(props, new Authenticator() {
                        protected PasswordAuthentication getPasswordAuthentication() {
                            return new PasswordAuthentication(username,password);
                        }
                    });
                    RetreiveFeedTask task = new RetreiveFeedTask();
                    task.execute();
            }
        });
    }

    class RetreiveFeedTask extends AsyncTask<String, Void, String> {



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


            try{


                 Transport transport = session.getTransport("smtp");
                    // Enter your correct gmail UserID and Password
                    transport.connect("smtp.gmail.com", username, password);

            } catch(Exception e) {
                //e.printStackTrace();

                Toast.makeText(context, "something is wrong",  Toast.LENGTH_SHORT).show();
            }
            return null;
        }
          @Override
            protected void onProgressUpdate(Void... result) {                        
              pdialog.show();                         
            }
        @Override
        protected void onPostExecute(String result) {

            pdialog.dismiss();

            iget = new Intent(context,NextScreen.class);
            startActivity(iget);


        }
    }

But the problem is:--if it connects to gmail id then it it goes to the next screen... If I put wrong username/password then it crashes..where Is the problem in my code??

user_apr
  • 719
  • 2
  • 10
  • 27

2 Answers2

2

Toast in doInBackground() is the problem.

You can't make any UI operations in doInBackground()

Return some flag(like everythingOk=true/false) to onPostExecute() & depending on the flag show the Toast.

class RetreiveFeedTask extends AsyncTask<String, Void, Boolean> {

   boolean everythingOk=false;


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

        try{
             everythingOk=true;
             Transport transport = session.getTransport("smtp");
             transport.connect("smtp.gmail.com", username, password);

        } catch(Exception e) {

          everythingOk=false;
        }
        return everythingOk;
    }

    @Override
    protected void onProgressUpdate(Void... result) {                        
          pdialog.show();                         
     }

    @Override
    protected void onPostExecute(Boolean result) {    

      pdialog.dismiss();       
      if(result==true){
      iget = new Intent(context,NextScreen.class);
                startActivity(iget);       
      }
      else{
         Toast.makeText(context, "something is worng",  Toast.LENGTH_SHORT).show();
      }          
   }
}
Don Chakkappan
  • 7,397
  • 5
  • 44
  • 59
1

Try this:

runOnUiThread(new Runnable()
  {
     Toast.makeText(context, "something is worng",Toast.LENGTH_SHORT).show();
  })

Similar question:Can i have an example of displaying a toast using runOnUiThread.

Community
  • 1
  • 1
Hems
  • 1
  • 8