java.lang.String无法转换为JSONObject

时间:2016-10-31 15:07:34

标签: php android-studio

我将使用register.java通过php文件将数据插入到web数据库中。但是当我键入数据时单击按钮寄存器,数据会插入数据库并且logcat显示下面的2错误.how can我修好了吗?我已经尝试了很多我可以做的但是仍然坚持这个值

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 Button bRegister = (Button) findViewById(R.id.bRegister);

    bRegister.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();


            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");
    Log.w("eeee", response.toString());


                        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();
                    }
                }
            };

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

Register.php

<?php
$con=mysql_connect("mysql.hostinger.my","user","password","mydb");


$name=$_POST["name"];

$password=$_POST["password"];
$username=$_POST["username"];
$statement= mysqli_prepare($con,"INSERT INTO user(name,username,password) VALUES (?,?,?)");
mysqli_stmt_bind_param($statement,"sss",$name,$username,$password);
mysqli_stmt_execute($statement);

$response = array();
$response = ["SUCCESS"];

echo json_encode($response);
    ?>

2错误:

    'D/Volley: [241] BasicNetwork.logSlowRequests: HTTP response for request=<[ ]  http://finalpro.16mb.com/android/Register.php  0x7ef3eea4 NORMAL 1>  [lifetime=3410], [size=843], [rc=200], [retryCount=0]
    W/System.err: org.json.JSONException: Value <br of type java.lang.String cannot be converted to JSONObject

0 个答案:

没有答案