需要在onPostExecute中显示toast

时间:2016-02-10 09:35:00

标签: android

 @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);
    }

在此我已经与db建立了联系。我需要在postExecute中显示警报或Toast,方法是选中1显示成功,0显示错误。我的表单已成功存储在db中,但在加载完成后我无法显示任何成功或错误。

3 个答案:

答案 0 :(得分:1)

您的对话框已完成,表示已调用loading.dismiss();中的onPostExecute。我认为传递给onPostExecute的字符串包含null或者既不是'1'也不是'0'的字符串。这就是为什么烤面包没有显示出来的原因。尝试使用else if替换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();
}

答案 1 :(得分:0)

你可以这样做

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

See here

答案 2 :(得分:-1)

super.onPostExecute(s)

的开头致电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();
       }