Screen Registration - Repeat users

Asked

Viewed 75 times

0

I have a post code, which makes the registration of my users, but they can repeat themselves, I would like help to realize a code so that users can not repeat, but I’m new to Android and do not know how to do it, my code:

public class onbuttonclickHttpPost  extends AsyncTask<String, Void, String> {

            protected void onPreExecute() {
            }


            protected String doInBackground(String... arg0) {


                try {


                    URL url = new URL("http://192.168.1.207/api/v2/bookdemo/_table/cad_users");

                    JSONObject postDataParams = new JSONObject();


                    HttpURLConnection conn = (HttpURLConnection) url.openConnection();
                    conn.setRequestProperty("Content-type", "application/json");
                    conn.setRequestProperty("Accept", "application/json");
                    conn.setRequestProperty("X-DreamFactory-Api-Key", "XXXXXXXX");
                    conn.setRequestProperty("X-DreamFactory-Session-Token", "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJzdWIiOjE0LCJ1c2VyX2lkIjoxNCwiZW1haWwiOiJ0aGlhZ28uY2FtYXJnb0Bldm9sdXRpb25pdC5jb20uYnIiLCJmb3JldmVyIjpmYWxzZSwiaXNzXXXXXXXXmp0aSXXXXXXXXX.hiid9z55BD_THe-oJc6WpTropgVDE7gtw2m9TKayo-I");
                    conn.setRequestProperty("Authorization", "Basic XXXXX");
                    conn.setRequestMethod("POST");
                    conn.setReadTimeout(15000 /* milliseconds */);
                    conn.setConnectTimeout(15000 /* milliseconds */);
                    conn.setDoInput(true);
                    conn.setDoOutput(true);

                    postDataParams.put("tx_name", mName);
                    postDataParams.put("tx_nickname", mNick);
                    postDataParams.put("dt_nascimento", mData);
                    postDataParams.put("tp_sexo", mSexo);
                    postDataParams.put("nu_cellphone", mNumber);
                    postDataParams.put("password", mPassword);
                    postDataParams.put("tx_email", mEmail);
                    Log.e("resource", postDataParams.toString());


                    JSONObject resource = new JSONObject();
                    JSONArray array = new JSONArray();
                    array.put(postDataParams);
                    resource.put("resource", array);

                    System.out.println(resource.toString());



                    conn.connect();

                    OutputStream os = conn.getOutputStream();
                    BufferedWriter writer = new BufferedWriter(
                            new OutputStreamWriter(os, "UTF-8"));
                    //writer.write(getPostDataString(postDataParams));
                    writer.write(resource.toString());

                    writer.flush();
                    writer.close();
                    os.close();

                    int responseCode = conn.getResponseCode();





                    if (responseCode == HttpsURLConnection.HTTP_OK) {
                        Intent intent = new Intent(PostCadastro.this, LoginActivity.class);
                        startActivity(intent);


                        BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream()));
                        StringBuffer sb = new StringBuffer("");
                        String line = "";



                        while ((line = in.readLine()) != null) {

                            sb.append(line);
                            break;
                        }

                        in.close();
                        return sb.toString();

                    } else {
                        return new String("false : " + responseCode);
                    }
                } catch (Exception e) {
                    return new String("Exception: " + e.getMessage());
                }
            }

            @Override
            protected void onPostExecute(String result) {
                Toast.makeText(getApplicationContext(), result,
                        Toast.LENGTH_LONG).show();

            }
        }
    private boolean hasContent(EditText et) {

        boolean bHasContent = false;

        if (et.getText().toString().trim().length() > 0) {
            // Got content
            bHasContent = true;
        }
        return bHasContent;
    }
}
  • I believe that this repeated user validation is done in the back end, in Java vc will not be able to control this without making a request in the API before

1 answer

2

You need to do this back-end validation:

It would be necessary to create a search engine to be executed when the user clicked on the button register, then, before you perform the Insert in the database, you would make a select to validate whether there is already this name in the table.

If it exists then you will need to return a message to the user on the registration screen. If there is no you register it successfully.

This mechanism can only be implemented in the back end.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.