LoginActivity.java
public class LoginActivity extends AppCompatActivity {
private static String LOGIN_URL = "http://192.168.43.231/Login/login.php";
public static final String KEY_USERNAME = "username";
public static final String KEY_PASSWORD = "password";
@Override
protected void onCreate(Bundle savedInstanceState) {
.....
login.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
final String usernameS = username.getText().toString().trim();
final String passwordS = password.getText().toString().trim();
if (usernameS.isEmpty() || passwordS.isEmpty()) {
Toast.makeText(LoginActivity.this, "Enter Valid Username & Password", Toast.LENGTH_SHORT).show();
} else {
StringRequest stringRequest = new StringRequest(Request.Method.POST, LOGIN_URL,
new Response.Listener<String>() {
@Override
public void onResponse(String response) {
try {
JSONObject jsonObject = new JSONObject(response);
boolean responseStatus = jsonObject.getBoolean("success");
if (responseStatus) {
Intent intent = new Intent(LoginActivity.this, MainActivity.class);
LoginActivity.this.startActivity(intent);
} else {
Toast.makeText(LoginActivity.this, "Invalid Username or Password", Toast.LENGTH_SHORT).show();
}
} catch (JSONException e) {
e.printStackTrace();
Toast.makeText(LoginActivity.this,"Error" + e.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> getParams() throws AuthFailureError {
Map<String, String> params = new HashMap<String, String>();
params.put(KEY_USERNAME, usernameS);
params.put(KEY_PASSWORD, passwordS);
return params;
}
};
RequestQueue requestQueue =Volley.newRequestQueue(LoginActivity.this);
requestQueue.add(stringRequest);
}
}
});
}
}
PHP代码
<?php
$con=mysqli_connect("localhost","root","","blast");
$username=$_POST['username']);
$password=$_POST['password']);
$sql=mysqli_prepare($con,"SELECT * from blast_user where username = '$username' and password = '$password'");
mysqli_stmt_bind_param($sql,"sss",$username,$password);
mysqli_stmt_execute($sql);
mysqli_stmt_store_result($sql);
mysqli_stmt_bind_result($sql,$username,$password);
$response=array();
$response["success"]=true;
echo json_encode($response);
>?
答案 0 :(得分:0)
执行此操作-
$username=$_POST['usernameS']);
$password=$_POST['passwordS']);
而不是-
$username=$_POST['username']);
$password=$_POST['password']);