0
 @Override
public void onClick(View v) {

    if (v == Register) {
        registerUser();
    }
}

private void registerUser() {
    int selectedId = Gender.getCheckedRadioButtonId();
    Selection = (RadioButton) findViewById(selectedId);

    if (Name.getText().toString().equals("")) {
        Name.setError("Enter your name");
        Name.requestFocus();
    } else if (Dob.getText().toString().equals("")) {
        Dob.setError("Enter Date of Birth");
        Dob.requestFocus();
    } else if (Collegeid.getText().toString().equals("")) {
        Collegeid.setError("Enter your CollegeID");
        Collegeid.requestFocus();
    } else if (Education.getSelectedItem().toString().equals("")) {
        Toast.makeText(RegisterFormActivity.this, "choose your education", Toast.LENGTH_SHORT).show();

    } else if (Year.getText().toString().equals("")) {
        Year.setError("Enter Year");
        Year.requestFocus();
    } else if (Address.getText().toString().equals("")) {
        Address.setError("Enter Your Address");
        Address.requestFocus();
    } else if (Pincode.getText().toString().equals("")) {
        Pincode.setError("Enter Pincode");
        Pincode.requestFocus();
    } else if (Mobile.getText().toString().equals("")) {
        Mobile.setError("Enter your Mobile Number");
        Mobile.requestFocus();
    } else if (!validatePhone(Mobile.getText().toString())) {
        Mobile.setError("Invalid Mobile Number");
        Mobile.requestFocus();
    } else if (landline.getText().toString().equals("")) {
        landline.setError("Enter your landline Number");
        landline.requestFocus();
    } else if (Email.getText().toString().equals("")) {
        Email.setError("Enter your email");
        Email.requestFocus();
    } else if (Fbid.getText().toString().equals("")) {
        Fbid.setError("Enter your facebookId");
        Fbid.requestFocus();
    } else if (Occupation.getSelectedItem().toString().equals("")) {
        Toast.makeText(RegisterFormActivity.this, "Fathers occupation", Toast.LENGTH_SHORT).show();

    }
    else if(Selection.getText().toString().equals(""))
     {
       Toast.makeText(RegisterFormActivity.this, "choose your gender", Toast.LENGTH_SHORT).show();
    }
    else if (Business.getText().toString().equals("")) {
        Business.setError("please specify details");
        Business.requestFocus();
    } else if (Shortgoal.getText().toString().equals("")) {
        Shortgoal.setError("Your short term goal");
        Shortgoal.requestFocus();
    } else if (Longgoal.getText().toString().equals("")) {
        Longgoal.setError("Your long term goal");
        Longgoal.requestFocus();
    } else if (Size.getText().toString().equals("")) {
        Size.setError("Enter T-shirt Size");
        Size.requestFocus();
    } else if (Reference.getText().toString().equals("")) {
        Size.setError("Enter Reference name");
        Size.requestFocus();
    }
    else if(Form.getText().toString().equals("")){
        Form.setText("Enquiry Form");
    }

    else {
        String sname = Name.getText().toString().trim();
        String dob = Dob.getText().toString().trim();
        String cid = Collegeid.getText().toString().trim();
        String edu = Education.getSelectedItem().toString();
        String year = Year.getText().toString().trim();
        String addr = Address.getText().toString().trim();
        String pin = Pincode.getText().toString().trim();
        String mob = Mobile.getText().toString().trim();
        String lan = landline.getText().toString().trim();
        String email = Email.getText().toString().trim();
        String fbid = Fbid.getText().toString().trim();
        String focc = Occupation.getSelectedItem().toString();
        String bus= Selection.getText().toString().trim();
        String spe = Business.getText().toString().trim();
        String stgoal = Shortgoal.getText().toString().trim();
        String ltgoal = Longgoal.getText().toString().trim();
        String tsize = Size.getText().toString().trim();
        //long tick_no = id.getText()
         sub = Form.getText().toString().trim();
       // String ref = Reference.getText().toString().trim();
        //   String gender = Selection.getSelected().toString().toLowerCase();
        register(sname, dob, cid, edu, year, addr, pin, mob, lan, email, fbid, focc, bus, spe, stgoal, ltgoal, tsize, ref);
        msg = "<p><b>Name: </b>" + sname + "</p><p><b>Dob: </b>" + dob +
                "</p><p><b>Collegeid: </b>" + cid +"</p><p><b>Education: </b>" + edu +"</p><p><b>Year: </b>" + year +
                "</p><p><b>Address: </b>" + addr +"</p><p><b>Pincode: </b>" + pin +"</p><p><b>Mobile: </b>" + mob +"</p><p><b>landline: </b>" + lan +"</p><p><b>Email: </b>" + email +
                "</p><p><b>Fbid: </b>" + fbid +"</p><p><b>Occupation: </b>" + focc +"</p><p><b>Selection: </b>" + bus +"</p><p><b>Business: </b>" + spe +"</p><p><b>Shortgoal: </b>" + stgoal +
                "</p><p><b>Longgoal: </b>" + ltgoal +"</p><p><b>Size: </b>" + tsize +
                "</p><p><b>Reference: </b>" + Reference.getText().toString();
        Properties props = new Properties();

        props.put("mail.smtp.host", "md-in-13.webhostbox.net");
        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");

        session = Session.getDefaultInstance(props, new Authenticator() {
            protected PasswordAuthentication getPasswordAuthentication() {
                return new PasswordAuthentication("username", "password");

            }
        });
        if(DetectConnection
                .checkInternetConnection(RegisterFormActivity.this)){
            //Toast.makeText(RegisterFormActivity.this, "mailn", Toast.LENGTH_LONG).show();

            RetrieveFeedTask task = new RetrieveFeedTask();
            task.execute();
        }
        else
        {
            Toast.makeText(RegisterFormActivity.this, "No internet connection", Toast.LENGTH_LONG).show();
        }
    }
}

    private void register(String sname, String dob, String cid, String edu, String year, String addr, String pin, String mob, String lan, String email, String fbid, String focc, String bus, String spe, String stgoal, String ltgoal, String tsize, String ref) {
        String urlSuffix = "?sname=" + sname + "&dob=" + dob + "&cid=" + cid + "&edu=" + edu + "&year=" + year + "&email=" + email + "&mob=" + mob + "&addr=" + addr + "&lan=" + lan + "&pin=" + pin + "&fbid=" + fbid + "&focc=" + focc + "&bus=" + bus + "&spe=" + spe + "&stgoal=" + stgoal + "&ltgoal=" + ltgoal + "&tsize=" + tsize + "&ref=" + ref;
        class RegisterUser extends AsyncTask<String, Void, String> {
            ProgressDialog loading;

            @Override
            protected void onPreExecute() {
                super.onPreExecute();
                loading = ProgressDialog.show(RegisterFormActivity.this, "Please Wait", null, true, true);
            }

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

                loading.dismiss();
                if (s.equals("1")) {
                    Toast.makeText(RegisterFormActivity.this, "success", Toast.LENGTH_SHORT).show();
                    Name.setText("");
                    Email.setText("");
                } else if (s.equals("0")) {
                    Toast.makeText(RegisterFormActivity.this, "Something went wrong", Toast.LENGTH_SHORT).show();
                }

              //  Toast.makeText(getApplicationContext(), s, Toast.LENGTH_LONG).show();




           }

            @Override
            protected String doInBackground(String... params) {
                String s = params[0];
                BufferedReader bufferedReader = null;
                try {
                    URL url = new URL(REGISTER_URL + s);
                    HttpURLConnection con = (HttpURLConnection) url.openConnection();
                    bufferedReader = new BufferedReader(new InputStreamReader(con.getInputStream()));
                    String result;
                    result = bufferedReader.readLine();
                    return result;

                } catch (Exception e) {
                    return null;
                }
            }
        }
        RegisterUser ru = new RegisterUser();
        ru.execute(urlSuffix);
    }

In this I have made the connection with db. I need to display a alert or toast in postExecute by checking 1 to display success and 0 for display error. My form is successfully storing in db but I'm not able to show any success or error after the progress loading finished.

チーズパン
  • 2,752
  • 8
  • 42
  • 63
Sibi R
  • 3
  • 2
  • 1
    Show more code. There is no call to the function to say what is going on – Nabin Feb 10 '16 at 09:38
  • 1
    you are returning bufferedReader.readline() in doInBackground which will be passed to PostExecute. is it 0 or 1? – Pooya Feb 10 '16 at 09:41

3 Answers3

1

Your dialog is finished means loading.dismiss(); in onPostExecute has been called. I think the string passed to onPostExecute is containing null or something which is neither '1' nor '0'. That's why the toast is not shown. Try replacing the else if part with else.

@Override
protected void onPostExecute(String s) {
    if (s.equals("1")) {
        Toast.makeText(RegisterFormActivity.this, "success", Toast.LENGTH_SHORT).show();
        Name.setText("");
        Email.setText("");
    } else {
        Toast.makeText(RegisterFormActivity.this, "Something went wrong", Toast.LENGTH_SHORT).show();
    }

    loading.dismiss();
    //  Toast.makeText(getApplicationContext(), s, Toast.LENGTH_LONG).show();
}
Reaz Murshed
  • 23,691
  • 13
  • 78
  • 98
0

You can do it like this

public void showToast(final String toast)
{
    runOnUiThread(new Runnable() {
        public void run()
        {
            Toast.makeText(MyActivity.this, toast, Toast.LENGTH_SHORT).show();
        }
    });
}

See here

Community
  • 1
  • 1
Rasel
  • 5,488
  • 3
  • 30
  • 39
-1

Call super.onPostExecute(s) at the beginning of the OnPostExecute()

  @Override
        protected void onPostExecute(String s) {
            super.onPostExecute(s);
            if (s.equals("1")) {
                Toast.makeText(RegisterFormActivity.this, "success", Toast.LENGTH_SHORT).show();
                Name.setText("");
                Email.setText("");
            } else if (s.equals("0")) {
                Toast.makeText(RegisterFormActivity.this, "Something went wrong", Toast.LENGTH_SHORT).show();
            }

            loading.dismiss();
          //  Toast.makeText(getApplicationContext(), s, Toast.LENGTH_LONG).show();
       }
Dhaval Parmar
  • 18,812
  • 8
  • 82
  • 177
Nick Isaacs
  • 172
  • 11
  • i am getting Something went wrong toast only but my form is successfully stored in db. is there any changes in my code. – Sibi R Feb 10 '16 at 10:30
  • Sorry, the answer was kind of stupid. A similar thing had worked for me before. As for the error, as the better answer mentioned, you are probably getting a null from the AsyncTask. Try adding con.setRequestProperty("Accept-Charset", "UTF-8"); after url.openConnection() – Nick Isaacs Feb 10 '16 at 17:09