如何验证服务器的电子邮件

时间:2017-01-12 08:29:31

标签: android email-validation

我是android的新手。我只是有一个文本字段,用户输入他们的电子邮件地址。我有一个在我的后端验证的电子邮件列表。当用户输入电子邮件时。我应该首先检查电子邮件是否已经过验证在后端或不在后端。如果验证电子邮件,则用户无法访问下一个活动。

public class Ongoing extends Fragment {


    Button proceed;
    EditText email;
    TextView surveyTitle;
    String success;

    private static final String url="http://192.168.0.123/survey/public/api/verifyemail";

    public Ongoing() {
        // Required empty public constructor
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View v=inflater.inflate(R.layout.fragment_ongoing, container, false);
        surveyTitle= (TextView) v.findViewById(R.id.surveyTitle);
        email = (EditText) v.findViewById(R.id.email);
        proceed= (Button) v.findViewById(R.id.proceed);
        proceed.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(isConnected()) {

                    String emailAddress = email.getText().toString().trim();
                    String emailPattern = "[a-zA-Z0-9._-]+@[a-z]+\\.+[a-z]+";

                    if (emailAddress.matches("")) {

                        Toast.makeText(getContext(), "Please fill up all the fields", Toast.LENGTH_LONG).show();

                    } else {
                        if (emailAddress.matches(emailPattern)) {
                            new HttpAsyncTask().execute("http://192.168.0.123/survey/public/api/verifyemail");
                        } else
                            Toast.makeText(getContext(), "Invalid Email Address", Toast.LENGTH_LONG).show();
                    }
                }
            else {

                Toast.makeText(getContext(), "Please check your internet connection", Toast.LENGTH_LONG).show();
            }
            }
        });
        return v;
    }

    private class HttpAsyncTask extends AsyncTask<String, Void, String> {
        @Override
        protected String doInBackground(String... urls) {

            List<NameValuePair> params = new ArrayList<NameValuePair>();
            params.add(new BasicNameValuePair("email", email.getText().toString()));
            JSONParser jParser = new JSONParser();
            JSONObject json = jParser.makeHttpRequest(url, "POST", params);
            try {
                success = json.getString("success");
            } catch (JSONException e) {
                e.printStackTrace();
            }
            return null;
        }
        @Override
        protected void onPostExecute(String result) {
            if(success.equals("Email verified. Please proceed with survey"))
                Toast.makeText(getContext(), "Wait For a moment", Toast.LENGTH_LONG).show();
        }
    }

    public boolean isConnected(){
        ConnectivityManager connMgr = (ConnectivityManager) getActivity().getSystemService(Activity.CONNECTIVITY_SERVICE);
        NetworkInfo networkInfo = connMgr.getActiveNetworkInfo();
        if (networkInfo != null && networkInfo.isConnected())
            return true;
        else
            return false;
    }

}

有人请帮助我。

1 个答案:

答案 0 :(得分:0)

您已编码,

try {
     success = json.getString("success");
} catch (JSONException e) {
     e.printStackTrace();
}

所以你保持成功的关键。根据逻辑使其成为两个值之一。如果发现/验证的电子邮件将发送,

  

&#34;成功&#34; =&#34;成功&#34;

如果没有找到/验证,

  

&#34;成功&#34; =&#34;错误&#34; //错误或任何字符串消息,以识别未找到的用户电子邮件。

如果您仅获得成功,则更改活动