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

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

ACoder

Lurker
Here's my php

Code:
<?php

if ($_SERVER['REQUEST_METHOD']=='POST') {

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

require_once 'connect.php';

$sql = "SELECT * FROM students WHERE username='$username' AND password='$password'";

$response = mysqli_query($conn, $sql);

$result = array();
$result['login'] = array();


if(mysqli_num_rows($response) > 0){
$rs = mysqli_fetch_array($response);

$index['name'] = $rs[1];
$index['username'] = $rs[3];

array_push($result['login'], $index);

$result['success'] = "1";
$result['message'] = "success";

header('Content-Type: application/json');
echo json_encode($result);
mysqli_close($conn);

}else{
$result['success'] = "0";
$result['message'] = "error";
echo json_encode($result);

mysqli_close($conn);
}

}

?>
 
Last edited by a moderator:
Yes, but the problem can't be diagnosed from the information you've given. We need to see:-

- Your Java code
- The JSON string received by the code
- The full stack trace from the Logcat view

Please use [code][/code] tags for all the above.
 
I am having the same problem, getting errors of "org.json.JSONException: Value <br of type java.lang.String cannot be converted to JSONObject"
I just wanted to do a simple Registration system using Android Studio, PHP and MySQL.
I do not know what went wrong here... Please help me :)

connect.php
PHP:
<?php

$conn = mysqli_connect("localhost", "root", "", "fyp1" );

?>
register.php
PHP:
<?php

if ($_SERVER['REQUEST_METHOD']=='POST'){
 
    $name = $_POST['name'];
    $email = $_POST['email'];
    $_password = $_POST['password'];
 
    $_password = password_hash($password, PASSWORD_DEFAULT);
 
    require_once 'connect.php';
 
    $sql = "INSERT INTO users3 (name, email, password) VALUES ('$name', '$email', '$password')";
 
    if (mysqli_query($conn, $sql)) {
        $result["success"] = "1";
        $result["message"] = "success";
    
        echo json_encode($result);
        mysqli_close($conn);
    }else{
        $result["error"] = "0";
        $result["message"] = "error";
    
        echo json_encode($result);
        mysqli_close($conn);
    }
 
}

?>
RegisterActivity.java
Java:
public class RegisterActivity extends AppCompatActivity {

    private EditText name, email, password, c_password;
    private Button btn_regist;
    private ProgressBar loading;
    private static String URL_REGIST = "http://10.144.184.30/fyp1/android3/register.php";


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register);

        loading = findViewById(R.id.loading);
        name = findViewById(R.id.name);
        email = findViewById(R.id.email);
        password = findViewById(R.id.password);
        c_password = findViewById(R.id.c_password);
        btn_regist = findViewById(R.id.btn_regist);

        btn_regist.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Regist();
            }
        });
    }

    private void Regist() {

        loading.setVisibility(View.VISIBLE);
        btn_regist.setVisibility(View.GONE);

        final String name = this.name.getText().toString().trim();
        final String email = this.email.getText().toString().trim();
        final String password = this.password.getText().toString().trim();
        //final String c_password = this.c_password.getText().toString().trim();

        StringRequest stringRequest = new StringRequest(Request.Method.POST, URL_REGIST,
                new Response.Listener<String>() {
                    @Override
                    public void onResponse(String response) {
                        try{
                            JSONObject jsonObject = new JSONObject(response);
                            String success = jsonObject.getString("success");

                            if (success.equals("1")){
                                Toast.makeText(RegisterActivity.this, "Register Success!", Toast.LENGTH_SHORT).show();

                            }

                        }catch (JSONException e){
                            e.printStackTrace();
                            Toast.makeText(RegisterActivity.this, "Register Error! " + e.toString(), Toast.LENGTH_SHORT).show();
                            loading.setVisibility(View.GONE);
                            btn_regist.setVisibility(View.VISIBLE);
                        }
                    }

                },
                new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError error) {
                        Toast.makeText(RegisterActivity.this, "Register Error2! " + error.toString(), Toast.LENGTH_SHORT).show();
                        loading.setVisibility(View.GONE);
                        btn_regist.setVisibility(View.VISIBLE);
                    }
                })
        {
            @Override
            protected Map<String, String> getParams() throws AuthFailureError {
                Map<String, String> params = new HashMap<>();
                params.put("name", name);
                params.put("email", email);
                params.put("password", password);

                return params;
            }
        };

        RequestQueue requestQueue = Volley.newRequestQueue(this);
        requestQueue.add(stringRequest);

    }
}
 
Last edited:
I do not know what went wrong here... Please help me :)

These problems are caused by an error in the format of the received JSON data. It's impossible to answer the question without seeing what exactly is in the data.
You can help yourself massively by running the app in debug mode, and placing a breakpoint on line 44 of the above code fragment. What's the value of parameter 'response'. I'm guessing it's not a valid JSON data structure.
For anyone to provide assistance here we need to see at least:-

a) The JSON string

and also

b) Stack trace from the Logcat view
 
Back
Top Bottom