为什么我的Volley请求返回0?

时间:2015-12-25 20:34:36

标签: java php android android-volley

正如标题所示,我的Volley onResponse脚本遇到了一些麻烦。在脚本中,我将POST数据传递给我的PHP脚本,然后返回0或1的值,但只返回0.我还将包含我的PHP脚本,虽然我确定这不是问题

private static final String REGISTER_URL = "http://192.168.0.7/login.php";

public static final String KEY_EMAIL = "email";
public static final String KEY_PASSWORD = "password";

private EditText loginEmail;
private EditText loginPassword;

private Button loginButton;

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

    TextView registerLink = (TextView) findViewById(R.id.registerLink);

    registerLink.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            startActivity(new Intent(LoginActivity.this, RegisterActivity.class));
        }
    });

    loginEmail = (EditText) findViewById(R.id.loginEmail);
    loginPassword = (EditText) findViewById(R.id.loginPassword);

    loginButton = (Button) findViewById(R.id.loginButton);

    loginButton.setOnClickListener(this);
}

public void loginUser() {
    final String password = loginPassword.getText().toString().trim();
    final String email = loginEmail.getText().toString().trim();

    final StringRequest stringRequest = new StringRequest(Request.Method.POST, REGISTER_URL,
            new Response.Listener<String>() {
                @Override
                public void onResponse(String response) {
                    Toast.makeText(LoginActivity.this,response.toString(),Toast.LENGTH_LONG).show();
                }
            },
            new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError error) {
                    Toast.makeText(LoginActivity.this,error.toString(),Toast.LENGTH_LONG).show();
                }
            }){
        @Override
        protected Map<String,String> getBody(){
            Map<String,String> body = new HashMap<String, String>();
            body.put(KEY_PASSWORD,password);
            body.put(KEY_EMAIL,email);
            return body;
        }

    };

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

@Override
public void onClick(View v) {
    if(v == loginButton){
        loginUser();
    }
}

}

这是PHP脚本。

if($_SERVER['REQUEST_METHOD']=='POST'){
$email = $_POST['email'];
$password = $_POST['password'];

require_once('dbConnect.php');

$query = "SELECT * FROM users WHERE email = '$email' AND password = '$password'";
$data = mysqli_query($con, $query) or die('Error');

if (mysqli_num_rows($data) == 1) {
    $response = 1;
    echo(json_encode($response));
} else {
    $response = 0;
    echo(json_encode($response));
}

}

0 个答案:

没有答案