gpt4 book ai didi

java - 如何解决java.lang.String类型的org.json.JSONException : Value <! DOCTYPE无法转换为JSONObject

转载 作者:太空宇宙 更新时间:2023-11-04 12:13:08 25 4
gpt4 key购买 nike

我目前正在开发一个注册登录应用程序。我面临这个问题,但找不到任何可以解决该问题的解决方案。有人对此有任何解决方案吗?我们将非常感谢您的帮助。

RegisterActivity.java

public class RegisterActivity extends AppCompatActivity {
AlertDialog.Builder builder;

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

builder = new AlertDialog.Builder(RegisterActivity.this);
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 etAge = (EditText) findViewById(R.id.etAge);
final EditText etEmail = (EditText) findViewById(R.id.etEmail);
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();
final int age = Integer.parseInt(etAge.getText().toString());
final String email = etEmail.getText().toString();

if (name.equals("") || username.equals("") || password.equals("") || age == 0 || email.equals("")) {
builder.setTitle("Something Went Wrong");
builder.setMessage("Please fill in all the fileds").setPositiveButton("OK", null).create().show();

} else {

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 {
builder = new AlertDialog.Builder(RegisterActivity.this);
builder.setMessage("Registration Failed").setNegativeButton("Retry", null).create().show();

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


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


}
}

});
}

注册.php

   <?php

$servername = "mysql6.000webhost.com";
$username = "a5124128_user";
$password = "paddwoed4896";
$dbname = "a5124128_account";


$con = mysqli_connect($servername, $username, $password, $dbname) or die(mysql_error());

$db_selected = mysql_selectdb($con, $dbname);

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

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

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

echo json_encode($response);

?>

以下是logcat

    09-25 15:41:50.697 17259-17259/com.example.chang.e_ktm W/System.err: org.json.JSONException: Value <!DOCTYPE of type java.lang.String cannot be converted to JSONObject
09-25 15:41:50.698 17259-17259/com.example.chang.e_ktm W/System.err: at org.json.JSON.typeMismatch(JSON.java:111)
09-25 15:41:50.698 17259-17259/com.example.chang.e_ktm W/System.err: at org.json.JSONObject.<init>(JSONObject.java:160)
09-25 15:41:50.698 17259-17259/com.example.chang.e_ktm W/System.err: at org.json.JSONObject.<init>(JSONObject.java:173)
09-25 15:41:50.698 17259-17259/com.example.chang.e_ktm W/System.err: at com.example.chang.e_ktm.RegisterActivity$1$1.onResponse(RegisterActivity.java:78)
09-25 15:41:50.698 17259-17259/com.example.chang.e_ktm W/System.err: at com.example.chang.e_ktm.RegisterActivity$1$1.onResponse(RegisterActivity.java:73)
09-25 15:41:50.699 17259-17259/com.example.chang.e_ktm W/System.err: at com.android.volley.toolbox.StringRequest.deliverResponse(StringRequest.java:67)
09-25 15:41:50.699 17259-17259/com.example.chang.e_ktm W/System.err: at com.android.volley.toolbox.StringRequest.deliverResponse(StringRequest.java:30)
09-25 15:41:50.699 17259-17259/com.example.chang.e_ktm W/System.err: at com.android.volley.ExecutorDelivery$ResponseDeliveryRunnable.run(ExecutorDelivery.java:99)
09-25 15:41:50.699 17259-17259/com.example.chang.e_ktm W/System.err: at android.os.Handler.handleCallback(Handler.java:815)
09-25 15:41:50.699 17259-17259/com.example.chang.e_ktm W/System.err: at android.os.Handler.dispatchMessage(Handler.java:104)
09-25 15:41:50.699 17259-17259/com.example.chang.e_ktm W/System.err: at android.os.Looper.loop(Looper.java:210)
09-25 15:41:50.699 17259-17259/com.example.chang.e_ktm W/System.err: at android.app.ActivityThread.main(ActivityThread.java:5830)
09-25 15:41:50.699 17259-17259/com.example.chang.e_ktm W/System.err: at java.lang.reflect.Method.invoke(Native Method)
09-25 15:41:50.699 17259-17259/com.example.chang.e_ktm W/System.err: at java.lang.reflect.Method.invoke(Method.java:372)
09-25 15:41:50.699 17259-17259/com.example.chang.e_ktm W/System.err: at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1113)
09-25 15:41:50.699 17259-17259/com.example.chang.e_ktm W/System.err: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:879)

有人可以帮我解决这个问题吗?问题似乎发生在response.listener

postman 结果

/image/4dBM6.png

最佳答案

您没有从 php api 获得正确的响应。因此,使用任何 REST 客户端(如 postman )检查 api 一次。

关于java - 如何解决java.lang.String类型的org.json.JSONException : Value &lt;! DOCTYPE无法转换为JSONObject,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39684601/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com