2
public void volley(){
String url = "http://percyteng.com/get_users_details.php";
StringRequest postRequest = new StringRequest(Request.Method.POST, url,
new Response.Listener<String>() {
@Override
public void onResponse(String response) {
try {
JSONObject jsonResponse = new JSONObject(response).getJSONObject("user");
String useremail = jsonResponse.getString("useremail"),
password = jsonResponse.getString("password");
alert(useremail,password);
} catch (JSONException e) {
e.printStackTrace();
}
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
error.printStackTrace();
}
}
) {
@Override
protected Map<String, String> getParams()
{
Map<String, String> params = new HashMap<>();
// the POST parameters:
params.put("useremail", etUsername.getText().toString());
params.put("password", etPassword.getText().toString());
return params;
}
};
Volley.newRequestQueue(this).add(postRequest);
这里是凌空的连接代码,我在我的Android的Java登录分类。我用警报来测试,发现这条线路并不真正起作用。
JSONObject jsonResponse = new JSONObject(response).getJSONObject("user");
该网址应该是正确的,下面是我的PHP代码。使用凌空连接到PHP获取JSONObject的响应
`$response = array();`
// include db connect class
require_once __DIR__ . '/db_connect.php';
try{
// connecting to db
$con = new DB_CONNECT();
$db = $con->connect();
echo $_POST["useremail"];
// check for post data
if (isset($_POST["useremail"]) && isset($_POST["password"])) {
$email = $_POST["useremail"];
$password = $_POST["password"];
// $useremail = "[email protected]";
// $password= "coolpig123";
// echo "shit";
// get a product from products table using PDO prepared statement
$result = $db->prepare("SELECT * FROM user WHERE useremail = :useremail AND password = :password");
echo "shit";
$result->bindParam(":useremail", $useremail, PDO::PARAM_STR, 30);
$result->bindParam(":password", $password, PDO::PARAM_STR, 25);
// bind the values individually
//execute will execute db command with binded variables. Becaise we
// binded first, the parameters for eceute() can be empty
$result->execute();
if (!empty($result)) {
// check for empty result
if ($result->rowCount() > 0) {
$result = $result->fetch();
$user = array();
$user["useremail"] = $result["useremail"];
$user["password"] = $result["password"];
// success
$response["success"] = 1;
// user node
$response["user"] = array();
array_push($response["user"], $user);
// echoing JSON response
echo json_encode($response);
} else {
// no product found
$response["success"] = 0;
$response["message"] = "No user found";
// echo no users JSON
echo json_encode($response);
}
} else {
// no product found
$response["success"] = 0;
$response["message"] = "Empty result";
// echo no users JSON
echo json_encode($response);
}
} else {
// required field is missing
$response["success"] = 0;
$response["message"] = "Required field(s) is missing";
// echoing JSON response
echo json_encode($response);
}
} catch(PDOException $e){
print "Sorry, a database error occurred. Please try again later.\n";
print $e->getMessage();
}
?>
我通过输入参数之前测试了php代码,并成功连接到我的服务器上的mysql。所以我想应该不会有我的PHP代码的问题。
此外,另一个猜测问题是,我没有做一些基本的sudo apt-get ....在我的服务器的Android和PHP连接,因为这个问题事先发生。但是,我不知道我需要安装什么。
我编辑的代码 –
其实我有mRequestQueue.add(postRequest);在这个方法的底部 –
@PercyTeng你是否设法得到你的请求或只是错误信息的任何回应。介意分享错误消息,以便更容易排除故障。 –