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你是否設法得到你的請求或只是錯誤信息的任何迴應。介意分享錯誤消息,以便更容易排除故障。 –