br无法转换为jsonarray

br无法转换为jsonarray

本文介绍了解析数据时出错org.json.jsonexception:java.lang.string类型的值< br无法转换为jsonarray的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在注册页面中注册用户但是收到错误,我已经转换了我需要的所有细节但仍然无效,可能问题来自php代码。插入不起作用。 />


我尝试过:



这是注册活动

公共类注册扩展AppCompatActivity {

EditText name1,etpolicyNumber,etPassword;
Button bRegister;


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


name1 =(EditText)findViewById(R.id.name);
etpolicyNumber =(EditText)findViewById(R.id.policy_number1);
etPassword =(EditText)findViewById(R.id.password1);
bRegister =(Button)findViewById(R.id.btnregister);

bRegister.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v){
String name = name1.getText()。 toString();
String policyNumber = etpolicyNumber.getText()。toString();
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);
if(success){

Intent intent = new Intent(Register.this,MainActivity.class);
startActivity(in帐篷);
Toast.makeText(Register.this,Registration Successful,Toast.LENGTH_SHORT).show();
} else {
AlertDialog.Builder builder = new AlertDialog.Builder(Register.this);
builder.setMessage(Register Failed)
.setNegativeButton(Retry,null)
.create()
.show();

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

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

}
}





这是RegisterRequest java



 public class RegisterRequest extends StringRequest {
private static final String REGISTER_REQUEST_URL =http://beigecare.ga/Register.php;
private Map< String,String> PARAMS;

public RegisterRequest(String name,String policyNumber,String password,Response.Listener< String> listener){
super(Method.POST,REGISTER_REQUEST_URL,listener,null);
params = new HashMap<>();
params.put(name,name);
params.put(policyNumber,policyNumber);
params.put(密码,密码);


}
@Override
公共地图< String,String> getParams(){
return params;
}
}





这是注册php代码

<?php 
$ con = mysqli_connect(mysql.hostinger.in,u608965245_user,Engmann1,u608965245_data);

$ name = $ _POST [name];
$ policynumber = $ _POST [policyNumber];
$ password = $ _POST [password];
$ statement = mysqli_prepare($ con,INSERT INTO user(name,policynumber,password)VALUES(?,?,?));
mysqli_stmt_bind_param($ statement,siss,$ name,$ policynumber,$ password);
mysqli_stmt_execute($ statement);

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


echo json_encode($ response);
?>



请帮我纠正错误

解决方案




I am trying to register a user in the registration page but getting error, i have converted every details i need to string but still not working,maybe the problem is from the php code.the insert doesn't work.

What I have tried:

this is the Registration activity

public class Register extends AppCompatActivity {

    EditText name1, etpolicyNumber, etPassword;
    Button bRegister;


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


        name1 = (EditText) findViewById(R.id.name);
        etpolicyNumber = (EditText) findViewById(R.id.policy_number1);
        etPassword = (EditText) findViewById(R.id.password1);
        bRegister = (Button) findViewById(R.id.btnregister);

        bRegister.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String name = name1.getText().toString();
                String policyNumber = etpolicyNumber.getText().toString();
                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");
                            if (success) {

                                Intent intent = new Intent(Register.this, MainActivity.class);
                                startActivity(intent);
                                Toast.makeText(Register.this, "Registration Successful", Toast.LENGTH_SHORT).show();
                            } else {
                                AlertDialog.Builder builder = new AlertDialog.Builder(Register.this);
                                builder.setMessage("Register Failed")
                                        .setNegativeButton("Retry", null)
                                        .create()
                                        .show();

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

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

    }
}



this is the RegisterRequest java

public class RegisterRequest extends StringRequest {
    private static final String REGISTER_REQUEST_URL = "http://beigecare.ga/Register.php";
    private Map<String, String> params;

    public RegisterRequest(String name, String policyNumber, String password, Response.Listener<String> listener){
        super(Method.POST, REGISTER_REQUEST_URL, listener, null);
        params = new HashMap<>();
        params.put("name", name);
        params.put("policyNumber", policyNumber);
        params.put("password", password);


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



this is the register php code

<?php
    $con = mysqli_connect("mysql.hostinger.in", "u608965245_user", "Engmann1", "u608965245_data");

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

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


    echo json_encode($response);
?>


Please help me correctn it

解决方案




这篇关于解析数据时出错org.json.jsonexception:java.lang.string类型的值&lt; br无法转换为jsonarray的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-02 18:10