Android json检查用户名可用性

时间:2014-05-08 07:07:51

标签: android json android-edittext

在我的Android应用程序中,我需要在用户输入用户名时检查用户名的可用性。我知道如何通过单击按钮输入用户名和密码后这样做...但我想在textview中显示它。就像用户正在进入乔治一样。然后,如果george已经注册,geroge需要在textview中以红色显示,否则为绿色。 我给了我用来检查buttonclick上的用户名的代码

    private ProgressDialog pDialog;
        JSONParser jsonParser = new JSONParser();

  private static String url_create_data = "http://example.com/app/create_data1.php";


// JSON Node names
    private static final String TAG_SUCCESS = "success";    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.signup_xm); 


                    @Override
                    public void onClick(View v) {
                        // TODO Auto-generated method stub

                        if (isNetworkAvailable(getBaseContext()))
                        {

                        String name=editTextUserName.getText().toString();
                        String password=editTextPassword.getText().toString();
                        String confirmPassword=editTextConfirmPassword.getText().toString();

                        String phoneNo = editMobileNumber.getText().toString();
                        //String sms = Integer.toString(number);


                        //Intent intent = new Intent(SignUpActivity.this, RegisterActivity.class);

                        //intent.putExtra("number", sms + "");
                        //startActivity(intent);            

                        //new CreateNewProduct().execute();                         

                        // check if any of the fields are vacant
                        if(name.equals("")||password.equals("")||confirmPassword.equals("") || phoneNo.equals(""))
                        {
                                Toast.makeText(getApplicationContext(), "Field Vaccant", Toast.LENGTH_LONG).show();
                                return;
                        }
                        // check if both password matches
                        if(!password.equals(confirmPassword))
                        {
                            Toast.makeText(getApplicationContext(), "Password does not match", Toast.LENGTH_LONG).show();
                            return;
                        }
                        else
                        {
                            new CreateNewProduct().execute();


                         }


class CreateNewProduct extends AsyncTask<String, String, String> {

        /**
         * Before starting background thread Show Progress Dialog
         * */
        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            pDialog = new ProgressDialog(SignUpActivity.this);
            pDialog.setMessage("Creating a new account..");
            pDialog.setIndeterminate(false);
            pDialog.setCancelable(true);
            pDialog.show();
        }

        /**
         * Creating product
         * */
        protected String doInBackground(String... args) {

            String name = editTextUserName.getText().toString();
            String password = editTextPassword.getText().toString();
            String mobile = editMobileNumber.getText().toString();      

            // Building Parameters
            List<NameValuePair> params = new ArrayList<NameValuePair>();
            params.add(new BasicNameValuePair("name", name));
            params.add(new BasicNameValuePair("password", password));
            params.add(new BasicNameValuePair("mobile", mobile));

            // getting JSON Object
            // Note that create product url accepts POST method
            JSONObject json = jsonParser.makeHttpRequest(url_create_data,
                    "POST", params);

            // check log cat fro response
            Log.d("Create Response", json.toString());

            // check for success tag
            try {
                int success = json.getInt(TAG_SUCCESS);

                if (success == 1) {

Intent i = new Intent(SignUpActivity.this, RegisterActivity.class);             
                    i.putExtra("number", sms + "");
                    startActivity(i);                   
                    //closing this screen
                    finish();
                } else {
                    // failed to create product
                    return "false";
                }       

            } catch (JSONException e) {
                e.printStackTrace();
            }
            return null;
        }
        /**
         * After completing background task Dismiss the progress dialog
         * **/

        protected void onPostExecute(String result)

        {           // TODO Auto-generated method stub
            super.onPostExecute(result);
                        if (result == "false"){

                            AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(
                                    context);

                                // set title
                                alertDialogBuilder.setTitle("Username already Exists...!");

                                // set dialog message
                                alertDialogBuilder
                                    .setMessage("Select another Username. Click 'Ok' to continue.")
                                    .setCancelable(false)

                                    .setNegativeButton("OK",new DialogInterface.OnClickListener() {
                                        public void onClick(DialogInterface dialog,int id) {
                                            // if this button is clicked, just close
                                            // the dialog box and do nothing
                                            dialog.cancel();
                                        }
                                    });

                                    // create alert dialog
                                    AlertDialog alertDialog = alertDialogBuilder.create();

                                    // show it
                                    alertDialog.show(); } 



           // Toast.makeText(SignUpActivity.this, "User Name already exists. Please choose another user name ", Toast.LENGTH_LONG).show();
                        pDialog.dismiss();
        }
    }

2 个答案:

答案 0 :(得分:2)

这对移动设备来说并不常见。如果你想这样做,那么你每次用户输入或删除任何字符时都必须调用你的api。您可以使用TextWatcher执行此操作。请遵循此link

注意:您不应该这样做,因为每次用户更改任何内容时都会调用api。这将导致更多地使用设备的电池以及更多的服务器负载。

如果用户名不可用,则您可以使用编辑文本 setTextColor 属性以红色显示。

答案 1 :(得分:-1)

如果要以编程方式更改textview的颜色

使用text.setTextColor(Color.rgb(255,0,0));你可以设置rgb text.setTextColor(Color.parseColor(“#FF0000”));用于从十六进制值中解析颜色

相关问题