2

I am trying to make a basic register/login application in Android Studio. I am using php and a web server. I have two php files, Register.php and Login.php so I want to register a user and store the entered information in the mysql server. When I enter the information in the android app and press register button I get this error :

JSONException: Value <html><body><script of type java.lang.String cannot be converted to JSONObject

so here is my register.php file

<?php

    $con = mysqli_connect("raps.byethost7.com", "****", "****", "****");

    $name = $_POST["name"];
    $last_name = $_POST["last_name"];
    $e_mail = $_POST["e_mail"];
    $password = $_POST["password"];
    $statement = mysqli_prepare($con, "INSERT INTO user (name, last_name, e_mail, password) VALUES (?, ?, ?, ?)");
    mysqli_stmt_bind_param($statement, "siss", $name, $last_name, $e_mail, $password);
    mysqli_stmt_execute($statement);

    $response = array();
    $response["success"] = true;  

    echo json_encode($response);
?>

and here is the java file of the activity:

public class SignupActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_signup);
        Toolbar myToolbar = (Toolbar) findViewById(R.id.my_toolbar);
        setSupportActionBar(myToolbar);


        final EditText ETname = (EditText) findViewById(R.id.name);
        final EditText ETlast_name = (EditText) findViewById(R.id.last_name);
        final EditText ETe_mail = (EditText) findViewById(R.id.e_mail);
        final EditText ETpassword = (EditText) findViewById(R.id.password);

        final Button bSignup = (Button) findViewById(R.id.signupButton);


        bSignup.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                final String name = ETname.getText().toString();
                final String last_name = ETlast_name.getText().toString();
                final String e_mail = ETe_mail.getText().toString();
                final String password = ETpassword.getText().toString();

                Response.Listener<String> responseListener = new Response.Listener<String>()
                {
                    @Override
                    public void onResponse(String response) {

                        try {
                            JSONObject jsonResponse = new JSONObject (response);
                            boolean success = jsonResponse.getBoolean("success");

                            if(success){
                                Intent intent = new Intent(SignupActivity.this,LoginActivity.class);
                                SignupActivity.this.startActivity(intent);
                            }
                            else {
                                AlertDialog.Builder builder = new AlertDialog.Builder(SignupActivity.this);
                                builder.setMessage("Register Failed!").
                                        setNegativeButton("Retry",null).create().show();
                            }

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


                    }
                };

                RegisterRequest registerRequest = new RegisterRequest(name,last_name,e_mail, password,responseListener );
                RequestQueue queue = Volley.newRequestQueue(SignupActivity.this);
                queue.add(registerRequest);
            }
        });


    }


}

I have been working on this for hours so please help me.

4
  • possible duplicate of: stackoverflow.com/questions/10267910/… ? Commented May 9, 2016 at 5:54
  • Is it something about the server side or the java code in android studio Commented May 9, 2016 at 6:05
  • server side issue, php returning html tags instead of json Commented May 9, 2016 at 7:02
  • 1
    Never store plain text passwords! Please use PHP's built-in functions to handle password security. If you're using a PHP version less than 5.5 you can use the password_hash() compatibility pack. Make sure that you don't escape passwords or use any other cleansing mechanism on them before hashing. Doing so changes the password and causes unnecessary additional coding. Commented May 9, 2016 at 12:33

1 Answer 1

2

Your PHP script throwing exception and return HTML to client and client not able to parse html. Exception occur here:
mysqli_stmt_bind_param($statement, "siss", $name, $last_name, $e_mail, $password);

You are passing parameter 'i' for string value. You should replace 'i' with 's' Now your bind statement will be mysqli_stmt_bind_param($statement, "ssss", $name, $last_name, $e_mail, $password);

Also handle connection fail and return success with false status. For more details visit

http://www.w3schools.com/php/php_mysql_prepared_statements.asp

http://php.net/manual/en/mysqli.quickstart.prepared-statements.php

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.