简体   繁体   中英

Android PHP code for checking existing row in MYSql not working

Hi I am developing a new android app in which a login page is there, when user enter the username, the username is checked with the usernames present in the table... if exists then a message has to come like 'username' exists please use another username' ... I created one php file for that and added code in activity... and I added a toast message in android if username is not registered showing the message.. but when I added that message the application is crashibng ' unfortunately application closed' if i remove that .. application will not crash but will stay on the page.. I am adding my php code and activity code please check.. if anyone helps...

Activity

/**
     * Background Async Task to Create new product
     * */
    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();
            String sms = Integer.toString(number);

            // 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) {
                    // successfully created product




                    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";



                }

                protected void onPostExecute(String result)

                {
                    // TODO Auto-generated method stub
                    super.onPostExecute(result);
                                if (result == "false")
                    Toast.makeText(SignUpActivity.this, "User Name already exists. Please choose another user name ", Toast.LENGTH_LONG).show();

                }


            } catch (JSONException e) {
                e.printStackTrace();
            }

            return null;
        }

        /**
         * After completing background task Dismiss the progress dialog
         * **/
        protected void onPostExecute(String file_url) {
            // dismiss the dialog once done
            pDialog.dismiss();
        }

    }





}

PHP code

<?php


// array for JSON response
$response = array();

// check for required fields
if (isset($_POST['name']) && isset($_POST['password']) && isset($_POST['mobile'])) {

    $name = $_POST['name'];
    $password = $_POST['password'];
    $mobile = $_POST['mobile'];

    // include db connect class
    require_once __DIR__ . '/db_connect.php';

    // connecting to db
    $db = new DB_CONNECT();


     $result1 = mysql_query("SELECT name FROM numerol WHERE name = '$name'");




     if (!mysql_num_rows($result1)) 


     {

    // mysql inserting a new row
    $result = mysql_query("INSERT INTO numerol(name, password, mobile) VALUES('$name', '$password', 

'$mobile')");

    // check if row inserted or not
    if ($result) {
        // successfully inserted into database
        $response["success"] = 1;
        $response["message"] = "Product successfully created.";

        // echoing JSON response
        echo json_encode($response);
    } else {
        // failed to insert row
        $response["success"] = 0;
        $response["message"] = "Oops! An error occurred.";

        // echoing JSON response
        echo json_encode($response);
    }
} else {
    // required field is missing
    $response["success"] = 0;
    $response["message"] = "Required field(s) is missing";

    // echoing JSON response
    echo json_encode($response);
}


}

else

{

echo 'User Name already exists, Please choose another username';

}
?>

logcat

11-09 06:12:48.081: I/Choreographer(1792): Skipped 33 frames!  The application may be doing too much work on its main thread.
11-09 06:12:49.091: I/Choreographer(1792): Skipped 30 frames!  The application may be doing too much work on its main thread.
11-09 06:12:49.561: I/Choreographer(1792): Skipped 36 frames!  The application may be doing too much work on its main thread.
11-09 06:12:50.512: I/Choreographer(1792): Skipped 49 frames!  The application may be doing too much work on its main thread.
11-09 06:12:50.660: I/Choreographer(1792): Skipped 34 frames!  The application may be doing too much work on its main thread.
11-09 06:12:51.220: I/Choreographer(1792): Skipped 50 frames!  The application may be doing too much work on its main thread.
11-09 06:12:51.841: I/Choreographer(1792): Skipped 45 frames!  The application may be doing too much work on its main thread.
11-09 06:12:52.561: I/Choreographer(1792): Skipped 78 frames!  The application may be doing too much work on its main thread.
11-09 06:12:52.731: I/Choreographer(1792): Skipped 44 frames!  The application may be doing too much work on its main thread.
11-09 06:12:54.050: I/Choreographer(1792): Skipped 340 frames!  The application may be doing too much work on its main thread.
11-09 06:12:54.230: I/Choreographer(1792): Skipped 47 frames!  The application may be doing too much work on its main thread.
11-09 06:12:54.570: I/Choreographer(1792): Skipped 76 frames!  The application may be doing too much work on its main thread.
11-09 06:12:56.263: I/Choreographer(1792): Skipped 438 frames!  The application may be doing too much work on its main thread.
11-09 06:12:56.570: I/Choreographer(1792): Skipped 70 frames!  The application may be doing too much work on its main thread.
11-09 06:12:57.830: E/WindowManager(1792): Activity com.example.numero.SignUpActivity has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView{40e2e678 V.E..... R.....ID 0,0-456,144} that was originally added here
11-09 06:12:57.830: E/WindowManager(1792): android.view.WindowLeaked: Activity com.example.numero.SignUpActivity has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView{40e2e678 V.E..... R.....ID 0,0-456,144} that was originally added here
11-09 06:12:57.830: E/WindowManager(1792):  at android.view.ViewRootImpl.<init>(ViewRootImpl.java:354)
11-09 06:12:57.830: E/WindowManager(1792):  at android.view.WindowManagerGlobal.addView(WindowManagerGlobal.java:216)
11-09 06:12:57.830: E/WindowManager(1792):  at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:69)
11-09 06:12:57.830: E/WindowManager(1792):  at android.app.Dialog.show(Dialog.java:281)
11-09 06:12:57.830: E/WindowManager(1792):  at com.example.numero.SignUpActivity$CreateNewProduct.onPreExecute(SignUpActivity.java:295)
11-09 06:12:57.830: E/WindowManager(1792):  at android.os.AsyncTask.executeOnExecutor(AsyncTask.java:586)
11-09 06:12:57.830: E/WindowManager(1792):  at android.os.AsyncTask.execute(AsyncTask.java:534)
11-09 06:12:57.830: E/WindowManager(1792):  at com.example.numero.SignUpActivity$1.onClick(SignUpActivity.java:163)
11-09 06:12:57.830: E/WindowManager(1792):  at android.view.View.performClick(View.java:4204)
11-09 06:12:57.830: E/WindowManager(1792):  at android.view.View$PerformClick.run(View.java:17355)
11-09 06:12:57.830: E/WindowManager(1792):  at android.os.Handler.handleCallback(Handler.java:725)
11-09 06:12:57.830: E/WindowManager(1792):  at android.os.Handler.dispatchMessage(Handler.java:92)
11-09 06:12:57.830: E/WindowManager(1792):  at android.os.Looper.loop(Looper.java:137)
11-09 06:12:57.830: E/WindowManager(1792):  at android.app.ActivityThread.main(ActivityThread.java:5041)
11-09 06:12:57.830: E/WindowManager(1792):  at java.lang.reflect.Method.invokeNative(Native Method)
11-09 06:12:57.830: E/WindowManager(1792):  at java.lang.reflect.Method.invoke(Method.java:511)
11-09 06:12:57.830: E/WindowManager(1792):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
11-09 06:12:57.830: E/WindowManager(1792):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
11-09 06:12:57.830: E/WindowManager(1792):  at dalvik.system.NativeStart.main(Native Method)
11-09 06:12:57.861: I/Choreographer(1792): Skipped 92 frames!  The application may be doing too much work on its main thread.
11-09 06:12:58.062: E/ActivityThread(1792): Activity com.example.numero.SignUpActivity has leaked IntentReceiver com.example.numero.SignUpActivity$2@40d4eed0 that was originally registered here. Are you missing a call to unregisterReceiver()?
11-09 06:12:58.062: E/ActivityThread(1792): android.app.IntentReceiverLeaked: Activity com.example.numero.SignUpActivity has leaked IntentReceiver com.example.numero.SignUpActivity$2@40d4eed0 that was originally registered here. Are you missing a call to unregisterReceiver()?
11-09 06:12:58.062: E/ActivityThread(1792):     at android.app.LoadedApk$ReceiverDispatcher.<init>(LoadedApk.java:795)
11-09 06:12:58.062: E/ActivityThread(1792):     at android.app.LoadedApk.getReceiverDispatcher(LoadedApk.java:596)
11-09 06:12:58.062: E/ActivityThread(1792):     at android.app.ContextImpl.registerReceiverInternal(ContextImpl.java:1316)
11-09 06:12:58.062: E/ActivityThread(1792):     at android.app.ContextImpl.registerReceiver(ContextImpl.java:1296)
11-09 06:12:58.062: E/ActivityThread(1792):     at android.app.ContextImpl.registerReceiver(ContextImpl.java:1290)
11-09 06:12:58.062: E/ActivityThread(1792):     at android.content.ContextWrapper.registerReceiver(ContextWrapper.java:423)
11-09 06:12:58.062: E/ActivityThread(1792):     at com.example.numero.SignUpActivity.sendSMS(SignUpActivity.java:205)
11-09 06:12:58.062: E/ActivityThread(1792):     at com.example.numero.SignUpActivity.access$3(SignUpActivity.java:193)
11-09 06:12:58.062: E/ActivityThread(1792):     at com.example.numero.SignUpActivity$1.onClick(SignUpActivity.java:117)
11-09 06:12:58.062: E/ActivityThread(1792):     at android.view.View.performClick(View.java:4204)
11-09 06:12:58.062: E/ActivityThread(1792):     at android.view.View$PerformClick.run(View.java:17355)
11-09 06:12:58.062: E/ActivityThread(1792):     at android.os.Handler.handleCallback(Handler.java:725)
11-09 06:12:58.062: E/ActivityThread(1792):     at android.os.Handler.dispatchMessage(Handler.java:92)
11-09 06:12:58.062: E/ActivityThread(1792):     at android.os.Looper.loop(Looper.java:137)
11-09 06:12:58.062: E/ActivityThread(1792):     at android.app.ActivityThread.main(ActivityThread.java:5041)
11-09 06:12:58.062: E/ActivityThread(1792):     at java.lang.reflect.Method.invokeNative(Native Method)
11-09 06:12:58.062: E/ActivityThread(1792):     at java.lang.reflect.Method.invoke(Method.java:511)
11-09 06:12:58.062: E/ActivityThread(1792):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
11-09 06:12:58.062: E/ActivityThread(1792):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
11-09 06:12:58.062: E/ActivityThread(1792):     at dalvik.system.NativeStart.main(Native Method)
11-09 06:12:58.140: E/ActivityThread(1792): Activity com.example.numero.SignUpActivity has leaked IntentReceiver com.example.numero.SignUpActivity$3@40e35ab0 that was originally registered here. Are you missing a call to unregisterReceiver()?
11-09 06:12:58.140: E/ActivityThread(1792): android.app.IntentReceiverLeaked: Activity com.example.numero.SignUpActivity has leaked IntentReceiver com.example.numero.SignUpActivity$3@40e35ab0 that was originally registered here. Are you missing a call to unregisterReceiver()?
11-09 06:12:58.140: E/ActivityThread(1792):     at android.app.LoadedApk$ReceiverDispatcher.<init>(LoadedApk.java:795)
11-09 06:12:58.140: E/ActivityThread(1792):     at android.app.LoadedApk.getReceiverDispatcher(LoadedApk.java:596)
11-09 06:12:58.140: E/ActivityThread(1792):     at android.app.ContextImpl.registerReceiverInternal(ContextImpl.java:1316)
11-09 06:12:58.140: E/ActivityThread(1792):     at android.app.ContextImpl.registerReceiver(ContextImpl.java:1296)
11-09 06:12:58.140: E/ActivityThread(1792):     at android.app.ContextImpl.registerReceiver(ContextImpl.java:1290)
11-09 06:12:58.140: E/ActivityThread(1792):     at android.content.ContextWrapper.registerReceiver(ContextWrapper.java:423)
11-09 06:12:58.140: E/ActivityThread(1792):     at com.example.numero.SignUpActivity.sendSMS(SignUpActivity.java:235)
11-09 06:12:58.140: E/ActivityThread(1792):     at com.example.numero.SignUpActivity.access$3(SignUpActivity.java:193)
11-09 06:12:58.140: E/ActivityThread(1792):     at com.example.numero.SignUpActivity$1.onClick(SignUpActivity.java:117)
11-09 06:12:58.140: E/ActivityThread(1792):     at android.view.View.performClick(View.java:4204)
11-09 06:12:58.140: E/ActivityThread(1792):     at android.view.View$PerformClick.run(View.java:17355)
11-09 06:12:58.140: E/ActivityThread(1792):     at android.os.Handler.handleCallback(Handler.java:725)
11-09 06:12:58.140: E/ActivityThread(1792):     at android.os.Handler.dispatchMessage(Handler.java:92)
11-09 06:12:58.140: E/ActivityThread(1792):     at android.os.Looper.loop(Looper.java:137)
11-09 06:12:58.140: E/ActivityThread(1792):     at android.app.ActivityThread.main(ActivityThread.java:5041)
11-09 06:12:58.140: E/ActivityThread(1792):     at java.lang.reflect.Method.invokeNative(Native Method)
11-09 06:12:58.140: E/ActivityThread(1792):     at java.lang.reflect.Method.invoke(Method.java:511)
11-09 06:12:58.140: E/ActivityThread(1792):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
11-09 06:12:58.140: E/ActivityThread(1792):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
11-09 06:12:58.140: E/ActivityThread(1792):     at dalvik.system.NativeStart.main(Native Method)
11-09 06:12:58.291: E/ActivityThread(1792): Activity com.example.numero.SignUpActivity has leaked IntentReceiver com.example.numero.SignUpActivity$2@40e357c0 that was originally registered here. Are you missing a call to unregisterReceiver()?
11-09 06:12:58.291: E/ActivityThread(1792): android.app.IntentReceiverLeaked: Activity com.example.numero.SignUpActivity has leaked IntentReceiver com.example.numero.SignUpActivity$2@40e357c0 that was originally registered here. Are you missing a call to unregisterReceiver()?
11-09 06:12:58.291: E/ActivityThread(1792):     at android.app.LoadedApk$ReceiverDispatcher.<init>(LoadedApk.java:795)
11-09 06:12:58.291: E/ActivityThread(1792):     at android.app.LoadedApk.getReceiverDispatcher(LoadedApk.java:596)
11-09 06:12:58.291: E/ActivityThread(1792):     at android.app.ContextImpl.registerReceiverInternal(ContextImpl.java:1316)
11-09 06:12:58.291: E/ActivityThread(1792):     at android.app.ContextImpl.registerReceiver(ContextImpl.java:1296)
11-09 06:12:58.291: E/ActivityThread(1792):     at android.app.ContextImpl.registerReceiver(ContextImpl.java:1290)
11-09 06:12:58.291: E/ActivityThread(1792):     at android.content.ContextWrapper.registerReceiver(ContextWrapper.java:423)
11-09 06:12:58.291: E/ActivityThread(1792):     at com.example.numero.SignUpActivity.sendSMS(SignUpActivity.java:205)
11-09 06:12:58.291: E/ActivityThread(1792):     at com.example.numero.SignUpActivity.access$3(SignUpActivity.java:193)
11-09 06:12:58.291: E/ActivityThread(1792):     at com.example.numero.SignUpActivity$1.onClick(SignUpActivity.java:117)
11-09 06:12:58.291: E/ActivityThread(1792):     at android.view.View.performClick(View.java:4204)
11-09 06:12:58.291: E/ActivityThread(1792):     at android.view.View$PerformClick.run(View.java:17355)
11-09 06:12:58.291: E/ActivityThread(1792):     at android.os.Handler.handleCallback(Handler.java:725)
11-09 06:12:58.291: E/ActivityThread(1792):     at android.os.Handler.dispatchMessage(Handler.java:92)
11-09 06:12:58.291: E/ActivityThread(1792):     at android.os.Looper.loop(Looper.java:137)
11-09 06:12:58.291: E/ActivityThread(1792):     at android.app.ActivityThread.main(ActivityThread.java:5041)
11-09 06:12:58.291: E/ActivityThread(1792):     at java.lang.reflect.Method.invokeNative(Native Method)
11-09 06:12:58.291: E/ActivityThread(1792):     at java.lang.reflect.Method.invoke(Method.java:511)
11-09 06:12:58.291: E/ActivityThread(1792):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
11-09 06:12:58.291: E/ActivityThread(1792):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
11-09 06:12:58.291: E/ActivityThread(1792):     at dalvik.system.NativeStart.main(Native Method)
11-09 06:12:58.350: D/dalvikvm(1792): GC_CONCURRENT freed 480K, 14% free 3822K/4412K, paused 77ms+114ms, total 413ms
11-09 06:12:58.400: E/ActivityThread(1792): Activity com.example.numero.SignUpActivity has leaked IntentReceiver com.example.numero.SignUpActivity$3@40d4f558 that was originally registered here. Are you missing a call to unregisterReceiver()?
11-09 06:12:58.400: E/ActivityThread(1792): android.app.IntentReceiverLeaked: Activity com.example.numero.SignUpActivity has leaked IntentReceiver com.example.numero.SignUpActivity$3@40d4f558 that was originally registered here. Are you missing a call to unregisterReceiver()?
11-09 06:12:58.400: E/ActivityThread(1792):     at android.app.LoadedApk$ReceiverDispatcher.<init>(LoadedApk.java:795)
11-09 06:12:58.400: E/ActivityThread(1792):     at android.app.LoadedApk.getReceiverDispatcher(LoadedApk.java:596)
11-09 06:12:58.400: E/ActivityThread(1792):     at android.app.ContextImpl.registerReceiverInternal(ContextImpl.java:1316)
11-09 06:12:58.400: E/ActivityThread(1792):     at android.app.ContextImpl.registerReceiver(ContextImpl.java:1296)
11-09 06:12:58.400: E/ActivityThread(1792):     at android.app.ContextImpl.registerReceiver(ContextImpl.java:1290)
11-09 06:12:58.400: E/ActivityThread(1792):     at android.content.ContextWrapper.registerReceiver(ContextWrapper.java:423)
11-09 06:12:58.400: E/ActivityThread(1792):     at com.example.numero.SignUpActivity.sendSMS(SignUpActivity.java:235)
11-09 06:12:58.400: E/ActivityThread(1792):     at com.example.numero.SignUpActivity.access$3(SignUpActivity.java:193)
11-09 06:12:58.400: E/ActivityThread(1792):     at com.example.numero.SignUpActivity$1.onClick(SignUpActivity.java:117)
11-09 06:12:58.400: E/ActivityThread(1792):     at android.view.View.performClick(View.java:4204)
11-09 06:12:58.400: E/ActivityThread(1792):     at android.view.View$PerformClick.run(View.java:17355)
11-09 06:12:58.400: E/ActivityThread(1792):     at android.os.Handler.handleCallback(Handler.java:725)
11-09 06:12:58.400: E/ActivityThread(1792):     at android.os.Handler.dispatchMessage(Handler.java:92)
11-09 06:12:58.400: E/ActivityThread(1792):     at android.os.Looper.loop(Looper.java:137)
11-09 06:12:58.400: E/ActivityThread(1792):     at android.app.ActivityThread.main(ActivityThread.java:5041)
11-09 06:12:58.400: E/ActivityThread(1792):     at java.lang.reflect.Method.invokeNative(Native Method)
11-09 06:12:58.400: E/ActivityThread(1792):     at java.lang.reflect.Method.invoke(Method.java:511)
11-09 06:12:58.400: E/ActivityThread(1792):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
11-09 06:12:58.400: E/ActivityThread(1792):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
11-09 06:12:58.400: E/ActivityThread(1792):     at dalvik.system.NativeStart.main(Native Method)
11-09 06:13:08.551: I/Process(1792): Sending signal. PID: 1792 SIG: 9

In asyncTask you cannot call Toast in doInBackground function... you can show your toast in onPostExecute(String result) so your code should be like this.

if (success == 1) {
                // successfully created product




                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";
            }
            protected void onPostExecute(String result) {
        // TODO Auto-generated method stub
        super.onPostExecute(result);
                    pDialog.dismiss();
                    if (result == "false")
        Toast.makeText(SignUpActivity.this, "User Name already exists. Please choose another user name ", Toast.LENGTH_LONG).show();

    }

In doInBackgound you just process your data but you can't change the UI of the activity. if you want to change the UI you either do it in onPreExecute() or in onPostExecute(String result)

EDIT!!

ahhhhhhhhh please remover this block

protected void onPostExecute(String file_url) {
        // dismiss the dialog once done
        pDialog.dismiss();
    }

and replace it with part of my code

which is

protected void onPostExecute(String result) {
    // TODO Auto-generated method stub
    super.onPostExecute(result);
       pDialog.dismiss();
                if (result == "false")
    Toast.makeText(SignUpActivity.this, "User Name already exists. Please choose another user name ", Toast.LENGTH_LONG).show();

this should be out of the doInBackground method and inside the asynctask

so it all will look like this:

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();
        String sms = Integer.toString(number);

        // 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) {
                // successfully created product




                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);
   pDialog.dismiss();
            if (result == "false")
Toast.makeText(SignUpActivity.this, "User Name already exists. Please choose another user name ", Toast.LENGTH_LONG).show();

}

}

This is not an answer, I have found some issues, so I have rewritten your php file. All the responses returned as json object except last message, may be the reason for your android app closing issue.

                // array for JSON response
            $response = array();

            // check for required fields
                    if (isset($_POST['name']) && isset($_POST['password']) && isset($_POST['mobile'])) {

                            $name = $_POST['name'];
                            $password = $_POST['password'];
                            $mobile = $_POST['mobile'];

                            // include db connect class
                            require_once __DIR__ . '/db_connect.php';

                            // connecting to db
                            $db = new DB_CONNECT();


                             $result1 = mysql_query("SELECT name FROM numerol WHERE name = '$name'");                                   

                             if (mysql_num_rows($result1)==0) {

                                    // mysql inserting a new row
                                    $result = mysql_query("INSERT INTO numerol(name, password, mobile) VALUES('$name', '$password', '$mobile')");

                                    // check if row inserted or not
                                    if ($result) {
                                        // successfully inserted into database
                                        $response["success"] = 1;
                                        $response["message"] = "Product successfully created.";                                 

                                    } else {
                                        // failed to insert row
                                        $response["success"] = 0;
                                        $response["message"] = "Oops! An error occurred.";                                  

                                    }
                                } else {
                                    // required field is missing
                                    $response["success"] = 0;
                                    $response["message"] = "User Name already exists, Please choose another username";


                                }


                        }else{          
                            $response["success"] = 0;
                            $response["message"] = "Required field(s) is missing";      


                        }

                        echo json_encode($response);

Note Use mysqli_* functions instead of mysql_* functions (deprecated)

Try this:

/**
 * Background Async Task to Create new product
 * */
class CreateNewProduct extends AsyncTask<String, String, String> {
    int success = 0;
    /**
     * 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();
        String sms = Integer.toString(number);

        // 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 {
             success = json.getInt(TAG_SUCCESS);               


        } catch (JSONException e) {
            e.printStackTrace();
        }

        return null;
    }

    /**
     * After completing background task Dismiss the progress dialog
     * **/
    protected void onPostExecute(String file_url) {
        if (success == 1) {
            // successfully created product




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

            i.putExtra("number", sms + "");
            startActivity(i);

            //closing this screen
            //finish();
        } else {
            // failed to create product
            Toast.makeText(SignUpActivity.this, "User Name already exists. Please choose another user name ", Toast.LENGTH_LONG).show();


        }
        // dismiss the dialog once done         
        pDialog.dismiss();
    }

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM