Android无法读取错误的jsonObject响应

时间:2017-07-16 11:47:12

标签: php android json jsonobjectrequest

    requestQueue = Volley.newRequestQueue(this);




    sign_in_register.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {

            request = new StringRequest(Request.Method.POST, "http://benedict.reconlasertag.sg/user_control.php", new Response.Listener<String>() {
                //response on sever

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

                        String user = mEmailView.getText().toString();
                        String pass = mPasswordView.getText().toString();

                        JSONObject jsonObjectzz = null;

                        jsonObjectzz = new JSONObject(response);
                        System.out.println("@@@@@ here is the final jsonobject" + jsonObjectzz);



                        if (jsonObjectzz.names().get(0).equals("success")) {
                            Toast.makeText(getApplicationContext(), "SUCCESS" + jsonObjectzz.getString("success"), Toast.LENGTH_SHORT).show();

                          Login.username = jsonObjectzz.getString("tryme");
                            startActivity(new Intent(getApplicationContext(), Welcome.class));


                        } else {
                            Toast.makeText(getApplicationContext(), "Error" + jsonObjectzz.getString("error"), Toast.LENGTH_SHORT).show();
                        }


                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                    Login.useremail = mEmailView.getText().toString();


                }

            }, new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError error) {

                }
            }) {
                @Override
                protected Map<String, String> getParams() throws AuthFailureError {
                    HashMap<String, String> hashMap = new HashMap<String, String>();
                    hashMap.put("email", mEmailView.getText().toString());
                    hashMap.put("password", mPasswordView.getText().toString());


                    return hashMap;
                }

            };

            requestQueue.add(request);
        }

    });

以上是我的 LOGIN SYSTEM 的代码文件摘录。 但是,在POSTMAN上测试时,jsonObject的响应与PHP URL给出的响应不匹配。相反,我的Android应用程序上显示的响应显示了在更新新信息之前使用的结果。这对我没有任何意义。

接下来是php文件的摘录

  

class User {

    private $db;
    private $connection;

    function __construct() {
        $this -> db = new DB_Connection();
        $this -> connection = $this->db->getConnection();
    }

    public function does_user_exist($email,$password)
    {
        $query = "Select * from users where email='$email' and password = '$password' ";
        $result = mysqli_query($this->connection, $query);

        //$resultb = mysql_query($query);

        if(mysqli_num_rows($result)>0){

            $success = " There are some rows found";

        $row = $result->fetch_assoc();
                $r = $row["name"];

            //$row = mysql_fetch_assoc($result);

            $json['success'] = ' Welcome To Earth'. $r;
            $json['tryme'] = $r; 



header('Content-type: application/json');
            echo json_encode($json);
            mysqli_close($this -> connection);
        }
        else{
            $query = "Insert into users (email, password) values ( '$email','$password')";
            $inserted = mysqli_query($this -> connection, $query);
            if($inserted == 1 ){
                $json['success'] = ' Acount created';
            }else{
                $json['error'] = ' Wrong password';
            }

header('Content-type: application/json');
            echo json_encode($json);
            mysqli_close($this->connection);
        }

    }

}


$user = new User();
if(isset($_POST['email'],$_POST['password'])) {
    $email = $_POST['email'];
    $password = $_POST['password'];

//  if(isset($_GET["a"])) echo "a is set\n";

    //$name = $_GET['name'];

    if(!empty($email) && !empty($password)){

        $encrypted_password = md5($password);
        $user-> does_user_exist($email,$password);

    }else{
        echo json_encode("you must type both inputs");
    }

}

这是使用POSTMAN测试时给出的结果:

  
    
      

{           &#34;成功&#34;:&#34;欢迎来到EarthBen&#34;,           &#34;设计&#34;:&#34; Ben&#34;       }

    
  

同样,Android提取的结果是System.out.println(&#34; @@@@@这里是最终的jsonobject&#34; + jsonObjectzz)

  
    
      
        

@@@@@这里是最终的jsonobject {&#34;成功&#34;:&#34;欢迎&#34;,&#34; tryme&#34;:null}

      
    
  

1 个答案:

答案 0 :(得分:0)

确定。代码没有任何问题。这是应用程序本身。 我重新安装了应用程序并更新了JSONOBJECT的响应。

一年前我没有遇到过这个问题。 (我最后一次触摸应用程序)。 似乎应用程序没有清除/更新'缓存'。

如果有人知道更好的解决方案。请告诉我。