0

I'm trying to create a login page based on the tutorial below: https://www.youtube.com/watch?v=lHBfyQgkiAA

I followed all the steps and changed somethings, I do not need a register button so I changed my PHP to:

    <?php
  $con = mysqli_connect("my_host", "my_user", "my_password", "my_database");

$username = $_POST["username"];
$password = $_POST["password"];

$statement = mysqli_prepare($con, "SELECT * FROM user WHERE username = ? AND password = ?");
mysqli_stmt_bind_param($statement, "ss", $username, $password);
mysqli_stmt_execute($statement);

mysqli_stmt_store_result($statement);
mysqli_stmt_bind_result($statement, $userID, $name, $username, $password);

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

while(mysqli_stmt_fetch($statement)){
    $response["success"] = true;  
    $response["name"] = $name;
    $response["username"] = $username;
    $response["password"] = $password;
}

echo json_encode($response);
?>

I'm receiving the error in this method:

public void onResponse(String response) {
                    try {
                        JSONObject jsonResponse = new JSONObject(response);
                        boolean success = jsonResponse.getBoolean("success");

                        if (success) {
                            String name = jsonResponse.getString("name");

                            Intent intent = new Intent(LoginActivity.this, WelcomeActivity.class);
                            intent.putExtra("name", name);
                            intent.putExtra("username", username);
                            LoginActivity.this.startActivity(intent);
                        } else {
                            AlertDialog.Builder builder = new AlertDialog.Builder(LoginActivity.this);
                            builder.setMessage("Login Failed")
                                    .setNegativeButton("Retry", null)
                                    .create()
                                    .show();
                        }

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

            LoginRequest loginRequest = new LoginRequest(username, password, responseListener);
            RequestQueue queue = Volley.newRequestQueue(LoginActivity.this);
            queue.add(loginRequest);
        }

I'm receiving the following error:

07-18 15:58:34.729 29396-29402/com.example.arthurf.tcc.app W/art: Suspending all threads took: 10.852ms
07-18 15:58:34.887 29396-29396/com.example.arthurf.tcc.app W/System.err: org.json.JSONException: Value <br><table of type java.lang.String cannot be converted to JSONObject
07-18 15:58:34.887 29396-29396/com.example.arthurf.tcc.app W/System.err:     at org.json.JSON.typeMismatch(JSON.java:111)
07-18 15:58:34.887 29396-29396/com.example.arthurf.tcc.app W/System.err:     at org.json.JSONObject.<init>(JSONObject.java:160)
07-18 15:58:34.887 29396-29396/com.example.arthurf.tcc.app W/System.err:     at org.json.JSONObject.<init>(JSONObject.java:173)
07-18 15:58:34.887 29396-29396/com.example.arthurf.tcc.app W/System.err:     at com.example.arthurf.tcc.app.LoginActivity$1$1.onResponse(LoginActivity.java:40)
07-18 15:58:34.887 29396-29396/com.example.arthurf.tcc.app W/System.err:     at com.example.arthurf.tcc.app.LoginActivity$1$1.onResponse(LoginActivity.java:36)
07-18 15:58:34.887 29396-29396/com.example.arthurf.tcc.app W/System.err:     at com.android.volley.toolbox.StringRequest.deliverResponse(StringRequest.java:60)
07-18 15:58:34.887 29396-29396/com.example.arthurf.tcc.app W/System.err:     at com.android.volley.toolbox.StringRequest.deliverResponse(StringRequest.java:30)
07-18 15:58:34.887 29396-29396/com.example.arthurf.tcc.app W/System.err:     at com.android.volley.ExecutorDelivery$ResponseDeliveryRunnable.run(ExecutorDelivery.java:99)
07-18 15:58:34.887 29396-29396/com.example.arthurf.tcc.app W/System.err:     at android.os.Handler.handleCallback(Handler.java:739)
07-18 15:58:34.887 29396-29396/com.example.arthurf.tcc.app W/System.err:     at android.os.Handler.dispatchMessage(Handler.java:95)
07-18 15:58:34.887 29396-29396/com.example.arthurf.tcc.app W/System.err:     at android.os.Looper.loop(Looper.java:148)
07-18 15:58:34.887 29396-29396/com.example.arthurf.tcc.app W/System.err:     at android.app.ActivityThread.main(ActivityThread.java:5417)
07-18 15:58:34.887 29396-29396/com.example.arthurf.tcc.app W/System.err:     at java.lang.reflect.Method.invoke(Native Method)
07-18 15:58:34.887 29396-29396/com.example.arthurf.tcc.app W/System.err:     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
07-18 15:58:34.887 29396-29396/com.example.arthurf.tcc.app W/System.err:     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)

This is my repository on Git: https://github.com/ArthurFranchetto/TCC.git

Any help will be welcome!

Thank you

  • **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 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 Jul 18 '16 at 21:17

1 Answers1

0

I think your problem is the response of your API call. First line of error reporting say that you have HTML in your response.

System.err: org.json.JSONException: Value <br><table of type java.lang.String cannot be converted to JSONObject

First thing, you can add header() to specify mime type

header('Content-Type: application/json');
echo json_encode($response);

Next, Is your API is on real tld or on localhost ? How do you test your Android App, with your phone or with an AVD ?

If url is on local machine, try it in android Browser to make sure that url is reachable.

Else your url is reachable on internet, you need to try your API with a cURL command (replace with your params) :

curl -X POST -F 'username=login' -F 'password=something' http://domain.tld/post-to-me.php

try to test response from cURL, if your json is valid with jsonlint.com

Rebangm
  • 76
  • 5