0

I'm trying to make a register page for my android app. I have managed to create a login page and I'm now trying to get the register page to work.

I've managed to get the data I'm sending via the register page into the database. I'm using a php script to transfer the data into my mysql database.

But every time I register, it sends the data and gives the echo back saying that the username/mail already exists.

It also shows this error in Java: Invalid use of SingleClientConnManager: connection still allocated.

I've been trying to fix this for hours but can't find anything that helps.

This is my database:

id - primary key AI
mail - unique 
password 
username - unique

This is my php script

<?php
 $hostname_memolink = "localhost";
 $database_memolink = "memolink";
 $username_memolink = "android";
 $password_memolink ="f3.W2TmM}8yO";
 $memolink = mysql_connect($hostname_memolink, $username_memolink, $password_memolink) or trigger_error(mysql_error(), E_USER_ERROR);

 mysql_select_db($database_memolink, $memolink);

 $mail = $_POST['mail'];
 $password = $_POST['password'];
 $username = $_POST['username'];

 if (trim($password) == "" or trim($username) == "" or trim($mail) == "") 
 {
 echo "variables are null";
 exit;
 }
 else
 {
 $resultUsername = mysql_query("SELECT * FROM tbl_user WHERE username='$username'");
 $resultMail = mysql_query("SELECT * FROM tbl_user WHERE mail='$mail'");
 $num_rows_username = mysql_num_rows($resultUsername);
 $num_rows_mail = mysql_num_rows($resultMail);

 if ($num_rows_username >= 1 && $num_rows_mail >= 1) {
  echo "username or email already exists";
  die();
 }
 else
 {
  mysql_query("INSERT INTO tbl_user (id, mail, password, username) VALUES('', '$mail', '$password', '$username')") or die(mysql_error());
  echo "registration complete";
 }
 }
?>

This is my Java method which sends the data to my php file:

    HttpPost httpPost;
StringBuffer buffer;
HttpResponse response;
HttpClient httpClient;
List<NameValuePair> registerData;

public void register(){
    try{

        httpClient = new DefaultHttpClient();
        httpPost = new HttpPost("http://www.memo-link.be/php/register.php");
        registerData = new ArrayList<NameValuePair>(2);
        EditText mailBox = (EditText) findViewById(R.id.mail_box);
        EditText userBox = (EditText) findViewById(R.id.username_box);
        EditText passBox = (EditText) findViewById(R.id.password_box);

        String mail = mailBox.getText().toString();
        String username = userBox.getText().toString();
        String password = passBox.getText().toString();

        registerData.add(new BasicNameValuePair("mail", mail));
        registerData.add(new BasicNameValuePair("username", username));
        registerData.add(new BasicNameValuePair("password", password));
        httpPost.setEntity(new UrlEncodedFormEntity(registerData));

        response = httpClient.execute(httpPost); //send data to Internet

        ResponseHandler<String> responseHandler = new BasicResponseHandler();
        String response = httpClient.execute(httpPost, responseHandler);
        System.out.println("Response : " + response.toString());

        if (response.equalsIgnoreCase("username or email already exists")){
            runOnUiThread(new Runnable(){
                public void run(){
                    dialog.dismiss();
                }
        });
            Vibrator v = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE);
            v.vibrate(250);
            Toast toast = Toast.makeText(RegisterActivity.this, "Username already exists!", Toast.LENGTH_SHORT);
            toast.show();
        }else if(response.toString().equalsIgnoreCase("username or email already exists")){
            runOnUiThread(new Runnable(){
                public void run(){
                    dialog.dismiss();
                }
        });
            Vibrator v = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE);
            v.vibrate(250);
            Toast toast = Toast.makeText(RegisterActivity.this, "Mail address already in use!", Toast.LENGTH_SHORT);
            toast.show();
        }else if(response.toString().equalsIgnoreCase("registration complete")){
            runOnUiThread(new Runnable(){
                public void run(){
                    Toast toast = Toast.makeText(RegisterActivity.this, "Registration Succesfull", Toast.LENGTH_SHORT);
                    toast.show();
                }
            });
            startActivity(new Intent(RegisterActivity.this, DashboardActivity.class));
        }

        runOnUiThread(new Runnable(){
            public void run(){
                dialog.dismiss();
            }
    });

    }catch (Exception e){
        dialog.dismiss();
        System.out.println("Exception : " + e.getMessage());
    }
}   

Thanks in advance

Viperdream
  • 35
  • 5
  • Can you print the values of mail, username and password in your php script and tell what are those values? – SSC Nov 17 '13 at 18:35
  • I'm not sure what you mean, I just did an echo of all the variables in the script and it gave me the same values as the ones I entered on my android app. – Viperdream Nov 17 '13 at 19:07

1 Answers1

1

Please see this, if this might help you. Let us know if you need more help.

Android: Invalid use of SingleClientConnManager: connection still allocated

Also, in your code, you are executing the query twice, which on the first time executes normally and as soon as it gets second query (as your data is already inserted), this satisfies the condition and prints the error message. Hope this will help.

Community
  • 1
  • 1
SSC
  • 2,956
  • 3
  • 27
  • 43
  • You need to consume the response body before you can reuse the connection for another request. You should not only read the response status, but read the response InputStream fully to the last byte whereby you just ignore the read bytes. – SSC Nov 17 '13 at 19:13
  • Well, I've gotten rid of the SingleClientConnManager error, but my php script is still saying the username or email already exists – Viperdream Nov 17 '13 at 19:17
  • Also, see if there is any insertion in the database or not? Just query your database. – SSC Nov 17 '13 at 19:20
  • I'm doing 2 queries to check whether the username or mail address already exists or not. And it does insert the data into the database – Viperdream Nov 17 '13 at 19:22
  • I mean in your android activity code. Here: response = httpClient.execute(httpPost); //send data to Internet ResponseHandler responseHandler = new BasicResponseHandler(); String response = httpClient.execute(httpPost, responseHandler); First response is the instance of HttpResponse, and second one is String , so why are you executing twice here? – SSC Nov 17 '13 at 19:24
  • Oh wow, for some reason I completely missed that. It works now, thanks for pointing that out! – Viperdream Nov 17 '13 at 19:29
  • I am glad this helped, I have updated the answer as well :) Kindly mark it as answer as well as mark it for the vote up :) Thanks – SSC Nov 17 '13 at 19:30