开发者社区> 问答> 正文

注册错误!org.json JSONException类型java.lang.String的值不能转

我是android编程的新手。我在虚拟屏幕上的Json输出有问题。但是,当我检查我的计算机时,register.php它看起来还不错,因为结果已输入到数据库中,但屏幕的输出却显示了另一件事-它显示的错误是

注册错误!org.json JSONException类型java.lang.String的值不能转换为JSONObject。

我究竟做错了什么?

这是我的Java代码RegisterActivity.java

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

        loading = findViewById(R.id.loading);
        full_name = findViewById(R.id.full_name);
        name = findViewById(R.id.name);
        phone = findViewById(R.id.phone);
        email = findViewById(R.id.email);
        address = findViewById(R.id.address);
        sch_name = findViewById(R.id.sch_name);
        lga = findViewById(R.id.lga);
        password = findViewById(R.id.password);
        c_password = findViewById(R.id.c_password);
        btn_regist = findViewById(R.id.btn_regist);


        btn_regist.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                Regist();
            }
            });
        }
        private void Regist() {
            loading.setVisibility(View.VISIBLE);
            btn_regist.setVisibility(View.GONE);

            final String full_name = this.full_name.getText().toString().trim();
            final String name = this.name.getText().toString().trim();
            final String phone = this.phone.getText().toString().trim();
            final String email = this.email.getText().toString().trim();
            final String address = this.address.getText().toString().trim();
            final String sch_name = this.sch_name.getText().toString().trim();
            final String lga = this.lga.getText().toString().trim();
            final String password = this.password.getText().toString().trim();


            StringRequest stringRequest = new StringRequest(Request.Method.POST, URL_REGIST,
                    new Response.Listener<String>() {

                        @Override
                        public void onResponse(String response) {
                            try{

                                JSONObject jsonObject = new JSONObject(response);
                                String success = jsonObject.getString("success");
                                if(success.equals("1")){
                                    Toast.makeText(RegisterActivity.this, "Registration Successful!", Toast.LENGTH_SHORT).show();
                                }

                        } catch (JSONException e)  {

                            e.printStackTrace();
                            Toast.makeText(RegisterActivity.this, "Registration Error! " + e.toString(), Toast.LENGTH_SHORT).show();
                            loading.setVisibility(View.GONE);
                            btn_regist.setVisibility(View.VISIBLE);
                        }
                    }

},
                    new Response.ErrorListener() {

                        @Override
                        public void onErrorResponse(VolleyError error) {
                            Toast.makeText(RegisterActivity.this, "Registration Error! " + error.toString(), Toast.LENGTH_SHORT).show();
                            loading.setVisibility(View.GONE);
                            btn_regist.setVisibility(View.VISIBLE);

                        }
                    })
            {
                @Override
                protected Map<String, String> getParams() throws AuthFailureError {
                    Map<String, String> params = new HashMap<>();
                    params.put("full_name",full_name);
                    params.put("name",name);
                    params.put("phone",phone);
                    params.put("email",email);
                    params.put("address",address);
                    params.put("sch_name",sch_name);
                    params.put("lga",lga);
                    params.put("password",password);

                    return params;

                }

            };
            RequestQueue requestQueue = Volley.newRequestQueue(this);
            requestQueue.add(stringRequest);
        }
    }

我的register.php是

$full_name = $_POST['full_name'];
$name = $_POST['name'];
$phone = $_POST['phone'];
$email = $_POST['email'];
$address = $_POST['address'];
$sch_name = $_POST['sch_name'];
$lga = $_POST['lga'];
$password = $_POST['password'];

$password = password_hash($password, PASSWORD_DEFAULT);
require_once 'connect.php';

$sql = "INSERT INTO users_table (full_name,name,phone,email,address,sch_name,lga,password) VALUES ('$full_name', '$name','$phone', '$email', '$address', '$sch_name', '$lga', '$password')";
if(mysqli_query($conn, $sql) ) {

    $result["success"] = "1";
    $result["message"] = "success";


     json_encode($result);
    mysqli_close($conn);

}else{
    $result["success"] = "0";
    $result["message"] = "error";

     json_encode($result);
    mysqli_close($conn);

}

}

?>

这是Logcat代码

11-28 11:07:25.168 3258-3258/? E/libprocessgroup: failed to make and chown /acct/uid_10061: Read-only file system
11-28 11:07:25.168 3258-3258/? W/Zygote: createProcessGroup failed, kernel missing CONFIG_CGROUP_CPUACCT?
11-28 11:07:25.168 3258-3258/? I/art: Not late-enabling -Xcheck:jni (already on)
11-28 11:07:25.194 3258-3266/? E/art: Failed writing handshake bytes (-1 of 14): Broken pipe
11-28 11:07:25.194 3258-3266/? I/art: Debugger is no longer active
11-28 11:07:25.323 3258-3258/? W/art: Before Android 4.1, method android.graphics.PorterDuffColorFilter androidx.vectordrawable.graphics.drawable.VectorDrawableCompat.updateTintFilter(android.graphics.PorterDuffColorFilter, android.content.res.ColorStateList, android.graphics.PorterDuff$Mode) would have incorrectly overridden the package-private method in android.graphics.drawable.Drawable
11-28 11:07:25.421 3258-3258/? I/art: Rejecting re-init on previously-failed class java.lang.Class<androidx.core.view.ViewCompat$2>
11-28 11:07:25.423 3258-3258/? I/art: Rejecting re-init on previously-failed class java.lang.Class<androidx.core.view.ViewCompat$2>
11-28 11:07:25.616 3258-3258/? I/TextInputLayout: EditText added is not a TextInputEditText. Please switch to using that class instead.
11-28 11:07:25.627 3258-3258/? I/TextInputLayout: EditText added is not a TextInputEditText. Please switch to using that class instead.
11-28 11:07:25.657 3258-3278/? D/OpenGLRenderer: Use EGL_SWAP_BEHAVIOR_PRESERVED: true
11-28 11:07:25.661 3258-3258/? D/Atlas: Validating map...
11-28 11:07:25.713 3258-3278/? I/OpenGLRenderer: Initialized EGL, version 1.4
11-28 11:07:25.714 3258-3278/? W/OpenGLRenderer: Failed to choose config with EGL_SWAP_BEHAVIOR_PRESERVED, retrying without...
11-28 11:07:29.625 3258-3278/com.cdscomputer.registerlogin D/EGL_emulation: eglCreateContext: 0xae834b80: maj 2 min 0 rcv 2
11-28 11:07:29.637 3258-3278/com.cdscomputer.registerlogin D/EGL_emulation: eglMakeCurrent: 0xae834b80: ver 2 0 (tinfo 0xae839380)
11-28 11:07:29.650 3258-3278/com.cdscomputer.registerlogin D/OpenGLRenderer: Enabling debug mode 0
11-28 11:07:29.685 3258-3278/com.cdscomputer.registerlogin D/EGL_emulation: eglMakeCurrent: 0xae834b80: ver 2 0 (tinfo 0xae839380)
11-28 11:07:29.747 3258-3258/com.cdscomputer.registerlogin I/Choreographer: Skipped 243 frames!  The application may be doing too much work on its main thread.
11-28 11:07:37.488 3258-3258/com.cdscomputer.registerlogin I/TextInputLayout: EditText added is not a TextInputEditText. Please switch to using that class instead.
11-28 11:07:37.495 3258-3258/com.cdscomputer.registerlogin I/TextInputLayout: EditText added is not a TextInputEditText. Please switch to using that class instead.
11-28 11:07:37.502 3258-3258/com.cdscomputer.registerlogin I/TextInputLayout: EditText added is not a TextInputEditText. Please switch to using that class instead.
11-28 11:07:37.518 3258-3258/com.cdscomputer.registerlogin I/TextInputLayout: EditText added is not a TextInputEditText. Please switch to using that class instead.
11-28 11:07:37.531 3258-3258/com.cdscomputer.registerlogin I/TextInputLayout: EditText added is not a TextInputEditText. Please switch to using that class instead.
11-28 11:07:37.545 3258-3258/com.cdscomputer.registerlogin I/TextInputLayout: EditText added is not a TextInputEditText. Please switch to using that class instead.
11-28 11:07:37.559 3258-3258/com.cdscomputer.registerlogin I/TextInputLayout: EditText added is not a TextInputEditText. Please switch to using that class instead.
11-28 11:07:37.574 3258-3258/com.cdscomputer.registerlogin I/TextInputLayout: EditText added is not a TextInputEditText. Please switch to using that class instead.
11-28 11:07:37.597 3258-3258/com.cdscomputer.registerlogin I/TextInputLayout: EditText added is not a TextInputEditText. Please switch to using that class instead.
11-28 11:07:37.670 3258-3278/com.cdscomputer.registerlogin D/EGL_emulation: eglMakeCurrent: 0xae834b80: ver 2 0 (tinfo 0xae839380)
11-28 11:07:38.074 3258-3278/com.cdscomputer.registerlogin D/EGL_emulation: eglMakeCurrent: 0xae834b80: ver 2 0 (tinfo 0xae839380)
11-28 11:07:42.407 3258-3258/com.cdscomputer.registerlogin I/Choreographer: Skipped 260 frames!  The application may be doing too much work on its main thread.
11-28 11:07:42.435 3258-3278/com.cdscomputer.registerlogin D/EGL_emulation: eglMakeCurrent: 0xae834b80: ver 2 0 (tinfo 0xae839380)
11-28 11:07:54.461 3258-3258/com.cdscomputer.registerlogin W/System.err: org.json.JSONException: Value < of type java.lang.String cannot be converted to JSONObject
11-28 11:07:54.461 3258-3258/com.cdscomputer.registerlogin W/System.err:     at org.json.JSON.typeMismatch(JSON.java:111)
11-28 11:07:54.462 3258-3258/com.cdscomputer.registerlogin W/System.err:     at org.json.JSONObject.<init>(JSONObject.java:160)
11-28 11:07:54.463 3258-3258/com.cdscomputer.registerlogin W/System.err:     at org.json.JSONObject.<init>(JSONObject.java:173)
11-28 11:07:54.463 3258-3258/com.cdscomputer.registerlogin W/System.err:     at com.cdscomputer.registerlogin.RegisterActivity$2.onResponse(RegisterActivity.java:82)
11-28 11:07:54.463 3258-3258/com.cdscomputer.registerlogin W/System.err:     at com.cdscomputer.registerlogin.RegisterActivity$2.onResponse(RegisterActivity.java:76)
11-28 11:07:54.463 3258-3258/com.cdscomputer.registerlogin W/System.err:     at com.android.volley.toolbox.StringRequest.deliverResponse(StringRequest.java:82)
11-28 11:07:54.463 3258-3258/com.cdscomputer.registerlogin W/System.err:     at com.android.volley.toolbox.StringRequest.deliverResponse(StringRequest.java:29)
11-28 11:07:54.463 3258-3258/com.cdscomputer.registerlogin W/System.err:     at com.android.volley.ExecutorDelivery$ResponseDeliveryRunnable.run(ExecutorDelivery.java:102)
11-28 11:07:54.463 3258-3258/com.cdscomputer.registerlogin W/System.err:     at android.os.Handler.handleCallback(Handler.java:739)
11-28 11:07:54.463 3258-3258/com.cdscomputer.registerlogin W/System.err:     at android.os.Handler.dispatchMessage(Handler.java:95)
11-28 11:07:54.463 3258-3258/com.cdscomputer.registerlogin W/System.err:     at android.os.Looper.loop(Looper.java:135)
11-28 11:07:54.463 3258-3258/com.cdscomputer.registerlogin W/System.err:     at android.app.ActivityThread.main(ActivityThread.java:5254)
11-28 11:07:54.463 3258-3258/com.cdscomputer.registerlogin W/System.err:     at java.lang.reflect.Method.invoke(Native Method)
11-28 11:07:54.463 3258-3258/com.cdscomputer.registerlogin W/System.err:     at java.lang.reflect.Method.invoke(Method.java:372)
11-28 11:07:54.463 3258-3258/com.cdscomputer.registerlogin W/System.err:     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
11-28 11:07:54.463 3258-3258/com.cdscomputer.registerlogin W/System.err:     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
11-28 11:07:54.557 3258-3278/com.cdscomputer.registerlogin D/EGL_emulation: eglMakeCurrent: 0xae834b80: ver 2 0 (tinfo 0xae839380)
11-28 11:07:54.579 3258-3278/com.cdscomputer.registerlogin V/RenderScript: 0xb41ad000 Launching thread(s), CPUs 2
11-28 11:07:55.005 3258-3278/com.cdscomputer.registerlogin D/EGL_emulation: eglMakeCurrent: 0xae834b80: ver 2 0 (tinfo 0xae839380)
11-28 11:07:56.487 3258-3278/com.cdscomputer.registerlogin D/EGL_emulation: eglMakeCurrent: 0xae834b80: ver 2 0 (tinfo 0xae839380)

它有效,但是如果我更改此方法,则不是正确的方法

if(success.equals("1")){
                                        Toast.makeText(RegisterActivity.this, "Registration Successful!", Toast.LENGTH_SHORT).show();
                                    }

                            } catch (JSONException e)  {

                                e.printStackTrace();
                                Toast.makeText(RegisterActivity.this, //Change this code  to the below code-> "Registration Error! " + e.toString(), Toast.LENGTH_SHORT).show(); ->to
"Registration Successful!", Toast.LENGTH_SHORT).show();

                            }
                        }

    },

展开
收起
Puppet 2019-11-30 13:26:18 1053 0
0 条回答
写回答
取消 提交回答
问答排行榜
最热
最新

相关电子书

更多
Spring Cloud Alibaba - 重新定义 Java Cloud-Native 立即下载
The Reactive Cloud Native Arch 立即下载
JAVA开发手册1.5.0 立即下载