[英]org.json.JSONException: Value <br of type java.lang.String cannot be converted to JSONObject (Andriod Studio)
I'm trying to get an android app developed in Android studio to send data from a Register user activity to a database. 我正在尝试在Android Studio中开发一个android应用程序,以将数据从Register用户活动发送到数据库。 The idea is once the user types in all the required information and clicks the register button the data should go to a database and the Login activity should re-appear. 这个想法是,一旦用户输入了所有必需的信息并单击注册按钮,数据就应该进入数据库,并且登录活动应该重新出现。 However, when I click on the register button on the Register user activity nothing is happening, no redirection to Login activity or data being sent to database. 但是,当我单击“ 注册”用户活动上的“注册”按钮时,没有任何反应,也没有重定向到“ 登录”活动或将数据发送到数据库。
Below is my Register user activity code: 以下是我的注册用户活动代码:
package com.example.vi5h.split;
import android.content.Intent;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.toolbox.Volley;
import org.json.JSONException;
import org.json.JSONObject;
public class RegisterUserActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_register_user);
final EditText etFullName = (EditText) findViewById(R.id.etFullName);
final EditText etUsername = (EditText) findViewById(R.id.etUsername);
final EditText etPassword = (EditText) findViewById(R.id.etPassword);
final EditText etEmail = (EditText) findViewById(R.id.etEmail);
final Button btnRegister = (Button) findViewById(R.id.btnRegister);
btnRegister.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
final String name = etFullName.getText().toString();
final String username = etUsername.getText().toString();
final String password = etPassword.getText().toString();
final String email = etEmail.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(RegisterUserActivity.this, LoginActivity.class);
RegisterUserActivity.this.startActivity(intent);
}else {
AlertDialog.Builder builder = new AlertDialog.Builder(RegisterUserActivity.this);
builder.setMessage("Register Failed")
.setNegativeButton("Retry", null)
.create()
.show();
}
} catch (JSONException e) {
e.printStackTrace();
}
}
};
RegisterRequest registerRequest = new RegisterRequest(name, username, email, password, responseListener);
RequestQueue queue = Volley.newRequestQueue(RegisterUserActivity.this);
queue.add(registerRequest);
}
});
}
}
Below is my Register request class code: 以下是我的注册请求类代码:
package com.example.vi5h.split;
import com.android.volley.Response;
import com.android.volley.toolbox.StringRequest;
import java.util.HashMap;
import java.util.Map;
public class RegisterRequest extends StringRequest {
private static final String REGISTER_REQUEST_URL = "https://split.000webhostapp.com/Register.php";
private Map<String, String> params;
public RegisterRequest(String name, String username, String email, String password, 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("email", email);
params.put("password", password);
}
@Override
public Map<String, String> getParams() {
return params;
}
}
Below is my PHP script (Register.php): 以下是我的PHP脚本 (Register.php):
<?php
$con = mysqli_connect("localhost", "id2833909_split421", "pass321", "id2833909_splitw");
$name = $_POST["name"];
$email = $_POST["email"];
$username = $_POST["username"];
$password = $_POST["password"];
$statement = mysqli_prepare($con, "INSERT INTO user (name, username, email, password) VALUES (?, ?, ?, ?)");
mysqli_stmt_bind_param($statement, "siss", $name, $username, $email, $password);
mysqli_stmt_execute($statement);
$response = array();
$response["success"] = true;
echo json_encode($response);
?>
And finnally below is the error thrown on Logcat after I tapped the Register button: https://imgur.com/b8x0Su6
最后,下面是我点击“ 注册”按钮后在Logcat上引发的错误: https://imgur.com/b8x0Su6
://imgur.com/b8x0Su6
If you visit https://split.000webhostapp.com/Register.php in a Web browser, you get back broken content. 如果在Web浏览器中访问https://split.000webhostapp.com/Register.php ,您将获得损坏的内容。 It is not a complete Web page, but instead is a bunch of error messages: 它不是一个完整的网页,而是一堆错误消息:
<br />
<b>Notice</b>: Undefined index: name in <b>/storage/ssd5/909/2833909/public_html/Register.php</b> on line <b>4</b><br />
<br />
<b>Notice</b>: Undefined index: email in <b>/storage/ssd5/909/2833909/public_html/Register.php</b> on line <b>5</b><br />
<br />
<b>Notice</b>: Undefined index: username in <b>/storage/ssd5/909/2833909/public_html/Register.php</b> on line <b>6</b><br />
<br />
<b>Notice</b>: Undefined index: password in <b>/storage/ssd5/909/2833909/public_html/Register.php</b> on line <b>7</b><br />
<br />
<b>Warning</b>: mysqli_stmt_bind_param() expects parameter 1 to be mysqli_stmt, boolean given in <b>/storage/ssd5/909/2833909/public_html/Register.php</b> on line <b>9</b><br />
<br />
<b>Warning</b>: mysqli_stmt_execute() expects parameter 1 to be mysqli_stmt, boolean given in <b>/storage/ssd5/909/2833909/public_html/Register.php</b> on line <b>10</b><br />
{"success":true}
The first three characters of that response are <br
. 该响应的前三个字符是<br
。 Those happen to be the first three characters that the JSON parser is complaining about in your error message. 这些恰好是JSON解析器在您的错误消息中抱怨的前三个字符。 So, my guess is that the Web server is sending back the same sort of broken content in response to your Volley request. 因此,我的猜测是Web服务器正在响应您的Volley请求发送回相同类型的损坏内容。
So, take a look at response
and see what you are getting as plain text. 因此,请看一下response
,看看您得到的是纯文本。 Then, fix whatever problem the response
indicates. 然后,修复response
指示的任何问题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.