尝试捕获的JSONObject不起作用

时间:2018-09-13 08:05:00

标签: java php android

我基本上是在做一个登录注册的android项目。我的注册工作足够好,但是try catch中的代码无法正常工作。因此,当我单击“注册”按钮时,用户注册可以一直发生。

以下是我在Java中的注册函数:

private void registerUser() {
        final String username = register_Username.getEditText().getText().toString();
        final String email = register_Email.getEditText().getText().toString();
        final String password = register_Password.getEditText().getText().toString();

        dialog.setMessage("Registering User...");
        dialog.show();
        StringRequest request = new StringRequest(Request.Method.POST, Constants.URL_REGISTER,
                new Response.Listener<String>() {
                    @Override
                    public void onResponse(String response) {
                        dialog.dismiss();   
                        try {
                            JSONObject jsonObject= new JSONObject(response);                        
                            Toast.makeText(MainActivity.this,jsonObject.getString("message"),Toast.LENGTH_SHORT).show();  
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }
                },
                new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError error) {
                        dialog.hide();
                        Toast.makeText(MainActivity.this,"Error",Toast.LENGTH_SHORT).show();
                    }
                }){
            @Override
            protected Map<String, String> getParams() throws AuthFailureError {
                        Map<String,String> params = new HashMap<>();
                        params.put("username",username);
                        params.put("email",email);
                        params.put("password",password);
                        return params;
            }
        };

        RequestHandler.getInstance(this).addToRequestQueue(request);
    }

这是我注册的php代码:

<?php

require_once '../includes/DBoperations.php';
$response=  array();

if ($_SERVER['REQUEST_METHOD']=='POST') {

    if (isset($_POST['username']) && isset($_POST['password']) && isset($_POST['email'])) {
        $db_connect= new DBoperations();
        if ($db_connect->createUser($_POST['username'],$_POST['password'],$_POST['email'])) {
            $response['error'] =false;
            $response['message']= "User registered successfully!";
        } else {
            $response['error'] =true;
            $response['message']= "User registered failed!";
        }       

    } else{
        $response['error'] =true;
        $response['message']= "Required fields are missing!";
    }
} else {
    $response['error'] =true;
    $response['message']= "Invalid Request";
}

echo json_encode($response);

?>

“错误”或“消息”不能被烘烤。我该怎么办?

2 个答案:

答案 0 :(得分:2)

利用函数JSON.parse()方法将字符串解析为JSONObject。然后遍历到JSONObject中的“消息”数据。

答案 1 :(得分:0)

我通过使用trim()从字段中获取字符串解决了这个问题。

final String username = register_Username.getEditText().getText().toString().trim();
final String email = register_Email.getEditText().getText().toString().trim();
final String password = register_Password.getEditText().getText().toString().trim();

希望这会有所帮助!