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.

gunescelil
  • 176
  • 17
  • possible duplicate of: http://stackoverflow.com/questions/10267910/jsonexception-value-of-type-java-lang-string-cannot-be-converted-to-jsonobject ? – pes502 May 09 '16 at 05:54
  • Is it something about the server side or the java code in android studio – gunescelil May 09 '16 at 06:05
  • server side issue, php returning html tags instead of json – Nilabja May 09 '16 at 07:02
  • 1
    **Never store plain text passwords!** Please use PHP's [built-in functions](http://jayblanchard.net/proper_password_hashing_with_PHP.html) to handle password security. If you're using a PHP version less than 5.5 you can use the `password_hash()` [compatibility pack](https://github.com/ircmaxell/password_compat). Make sure that you [don't escape passwords](http://stackoverflow.com/q/36628418/1011527) or use any other cleansing mechanism on them before hashing. Doing so *changes* the password and causes unnecessary additional coding. – Jay Blanchard May 09 '16 at 12:33

1 Answers1

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

USKMobility
  • 5,814
  • 2
  • 23
  • 31