0
我在我的android應用程序中使用Volley庫。我正在嘗試創建註冊功能。一切似乎工作正常,我可以註冊,但我無法顯示任何響應從PHP文件作爲敬酒在Android上。吐司甚至沒有出現。也許我應該先告訴你我的代碼:作爲吐司文本顯示排列反應
Response.Listener<String> listener = new Response.Listener<String>() {
@Override
public void onResponse(String response) {
try {
JSONObject jsonResponse = new JSONObject(response);
String result = jsonResponse.getString(response);
if(result == "Registered!") {
Toast.makeText(RegActivity.this, result, Toast.LENGTH_LONG).show();
} else {
Toast.makeText(getApplicationContext(),"Could not register!", Toast.LENGTH_LONG).show();
}
} catch (JSONException e) {
e.printStackTrace();
}
}
};
RegRequest regRequest = new RegRequest(username, password, email, listener);
RequestQueue queue = Volley.newRequestQueue(RegActivity.this);
queue.add(regRequest);
這裏是我的regRequest類:
public class RegRequest extends StringRequest {
private static String regURL = "http://chicpiq.com/android/android_register.php";
private Map<String, String> params;
public RegRequest(String username, String password, String email, Response.Listener<String> listener) {
super (Method.POST, regURL, listener, null);
params = new HashMap<>();
params.put("username", username);
params.put("password", password);
params.put("email", email);
}
@Override
public Map<String, String> getParams() {
return params;
}
}
這裏是我的PHP:
require "../database/database.php";
$username = mysqli_real_escape_string($database, $_POST['username']);
$password = mysqli_real_escape_string($database, $_POST['password']);
$email = mysqli_real_escape_string($database, $_POST['email']);
$joindate = date("y.m.d");
$encryptpass = password_hash($password, PASSWORD_DEFAULT);
$register = mysqli_query($database, "INSERT INTO users (username, password, email, joindate) VALUES ('$username', '$encryptpass', '$email', '$joindate')");
mysqli_execute($register);
$response = "Registered!";
echo json_encode($response);
正如我所說,我有註冊沒有問題,我可以做到這一點。我的問題是我想更改PHP代碼來檢查用戶是否存在等,我想顯示其他消息。我似乎無法顯示來自PHP的回覆作爲吐司。有沒有人有任何想法? Cheers