0

getting this error in my app

W/System.err: org.json.JSONException: Value br of type java.lang.String cannot be converted to JSONObject

this is my RegisterActivity.java

public class RegisterActivity extends AppCompatActivity {

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

    final EditText etName=(EditText) findViewById(R.id.etname);
    final EditText etUsername=(EditText) findViewById(R.id.etusername);
    final EditText etPassword=(EditText) findViewById(R.id.etpassword);
    final EditText etCPassword=(EditText) findViewById(R.id.etcpassword);
    final EditText etEmail=(EditText) findViewById(R.id.etemail);
    final EditText etContact=(EditText) findViewById(R.id.etcontact);
    final EditText etAge=(EditText) findViewById(R.id.etage);
    final Button btnRegister=(Button) findViewById(R.id.btnregister);

    btnRegister.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            final String name=etName.getText().toString();
            final String username=etUsername.getText().toString();
            final String password=etPassword.getText().toString();
            final String email=etEmail.getText().toString();
            final int age=Integer.parseInt(etAge.getText().toString());
            final int contact=Integer.parseInt(etContact.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(RegisterActivity.this,LoginActivity.class);
                            RegisterActivity.this.startActivity(intent);
                        }else{
                            AlertDialog.Builder builder= new AlertDialog.Builder(RegisterActivity.this);
                            builder.setMessage("Register Failed")
                                    .setNegativeButton("Retry",null)
                                    .create()
                                    .show();
                        }

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

                        AlertDialog.Builder build= new AlertDialog.Builder(RegisterActivity.this);
                        build.setMessage("JSON Failed")
                                .setNegativeButton("Retry",null)
                                .create()
                                .show();
                    }
                }
            };

            RegisterRequest registerRequest = new RegisterRequest(name,username,password,email,contact,age,responseListener);
            RequestQueue queue = Volley.newRequestQueue(RegisterActivity.this);
            queue.add(registerRequest);
        }
    });
}
}

this is RegistryRequest.java

public class RegisterRequest extends StringRequest{

private static final String REGISTER_REQUEST_URL="https://smplycode.000webhostapp.com/Registerft.php";
private Map<String, String> params;


public RegisterRequest(String name,String username,String password,String email,int contact, int age, Response.Listener<String> listener) {
    super(Method.POST, REGISTER_REQUEST_URL, listener, null);
    params = new HashMap<>();
    params.put("name",name);
    params.put("username",username);
    params.put("password",password);
    params.put("email",email);
    params.put("contact",contact+"");
    params.put("age",age+"");
    }

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

this is my registerft.php file

<?php
    $con = mysqli_connect("localhost", "id20401_mydbname", "mydbpaord", "id2041_mydme");

    $name = $_POST["name"];
    $username = $_POST["username"];
    $password = $_POST["password"];
    $email = $_POST["email"];
    $contact = $_POST["contact"];
    $age = $_POST["age"];

    $statement = mysqli_prepare($con, "INSERT INTO usersft (name, username, password, email, contact, age) VALUES (?, ?, ?, ?, ?, ?)");
    mysqli_stmt_bind_param($statement, "ssssii", $name, $username,$password,$email,$contact, $age);
    mysqli_stmt_execute($statement);

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

    echo json_encode($response);
?>

i watched this tutorial on youtube this code working on that tutorial.

Getting this when printing response string as an alert

if anyone want to help please give me your email id i am doing project i want to make an app on c++ learning for new comers :)

Sumit Yadav
  • 97
  • 1
  • 11

2 Answers2

0

First print out the string right before JSONObject jsonResponse= new JSONObject(response); so you actually know if it's in the right format or not. Br is an html tag for line break so that leads me to think that your response is HTML and not JSON

Jamal H
  • 934
  • 7
  • 23
  • check my other answer for what i am getting when printing response String – Sumit Yadav Jun 28 '17 at 16:24
  • @SumitYadav There are html warnings in that string, messing up the JSON. You can't expect to parse a string that contains html to json. you'll either need to code a parser to get rid of the html or find a way to configure your app to not send the warnings – Jamal H Jun 28 '17 at 16:30
0

I'll first try to collect the different answers and comments:

If you make the POST-Call you do in RegistryRequest.java outside your application (e.g. using curl -i -X POST --data "name=name&username=username&password=password&email=email&contact=123456789&age=21" https://smplycode.000webhostapp.com/Registerft.php) you get an HTML page with an error message (as posted by others):

<br />
<b>Warning</b>:  mysqli_stmt_bind_param() expects parameter 1 to be mysqli_stmt, boolean given in <b>/storage/ssd1/901/2049901/public_html/Registerft.php</b> on line <b>12</b><br />
<br />
<b>Warning</b>:  mysqli_stmt_execute() expects parameter 1 to be mysqli_stmt, boolean given in <b>/storage/ssd1/901/2049901/public_html/Registerft.php</b> on line <b>13</b><br />

Interpreting this as JSON fails at the very first HTML tag (<br />) - thats what the error message is about.

Now let's try to analyze the php error message, i.e. find the root cause:

Creating the db connection or building the statement fails and returns a boolean false that is not a prepared statement - therefor the error on mysqli_stmt_bind_param(). See mysqli_fetch_array()/mysqli_fetch_assoc()/mysqli_fetch_row() expects parameter 1 to be resource or mysqli_result, boolean given for details.

Please make sure your db query works and your php page returns a valid JSON.

sruetti
  • 532
  • 2
  • 7