• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Json error: Value <br of type java.lang.String cannot be converted to JSONObject

gmrcs

Lurker
java code
Code:
btn_log.setOnClickListener(new View.OnClickListener(){
    @Override
    public void onClick(View v) {

        final String username=tv_usuario.getText().toString();
        final String password=tv_password.getText().toString();
//51
        Response.Listener<String> responseListener = new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {
                try {
//55
                    JSONObject jsonResponse = new JSONObject(response);
                    boolean success = jsonResponse.getBoolean("success");
                    if(success){
                        String name = jsonResponse.getString("name");
                        int age = jsonResponse.getInt("age");

                        Intent intent = new Intent(MainActivity.this, Usuario.class);
                        intent.putExtra("name", name);
                        intent.putExtra("username", username);
                        intent.putExtra("age", age);
                        intent.putExtra("password", password);

                        MainActivity.this.startActivity(intent);


                    }else{
                        AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
                        builder.setMessage("Error Login")
                                .setNegativeButton("Retry",null)
                                .create().show();
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        };
        LoginRequest loginRequest = new LoginRequest(username, password, responseListener);
        RequestQueue queue = Volley.newRequestQueue(MainActivity.this);
        queue.add(loginRequest);
    }
});

Login
Code:
public class LoginRequest extends StringRequest {
    private static final String LOGIN_REQUEST_URL="http://localhost/Login.php";
    private Map<String,String> params;
    public LoginRequest(String username,String password, Response.Listener<String> listener){
        super(Request.Method.POST, LOGIN_REQUEST_URL,listener, null);
        params = new HashMap<>();
        params.put("username", username);
        params.put("password", password);
    }

    @Override
    public Map<String, String> getParams() {
        return params;
    }

}

here is my php
Code:
hp
    $con = mysqli_connect("localhost", "root", "", "usuarios");

    $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_stone_result($statement);
    mysqli_stmt_bind_result($statement, $userID, $name, $username, $password, $age);

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

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

    echo json_encode($response);

?>

Logcat view
Code:
W/System.err: org.json.JSONException: Value <br of type java.lang.String cannot be converted to JSONObject
W/System.err:     at org.json.JSON.typeMismatch(JSON.java:111)
        at org.json.JSONObject.<init>(JSONObject.java:163)
W/System.err:     at org.json.JSONObject.<init>(JSONObject.java:176)
        at com.example.avance.MainActivity$2$1.onResponse(MainActivity.java:55)
        at com.example.avance.MainActivity$2$1.onResponse(MainActivity.java:51)
        at com.android.volley.toolbox.StringRequest.deliverResponse(StringRequest.java:82)
W/System.err:     at com.android.volley.toolbox.StringRequest.deliverResponse(StringRequest.java:29)
        at com.android.volley.ExecutorDelivery$ResponseDeliveryRunnable.run(ExecutorDelivery.java:102)
W/System.err:     at android.os.Handler.handleCallback(Handler.java:789)
        at android.os.Handler.dispatchMessage(Handler.java:98)
        at android.os.Looper.loop(Looper.java:164)
W/System.err:     at android.app.ActivityThread.main(ActivityThread.java:6944)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:327)
W/System.err:     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1374)
 
Back
Top Bottom