我一直在解决这个问题已有一段时间了,目前我正试图将我的php脚本转换为PDO,但是当通过isset()运行时,$ _POST总是给我null。我在android中的代码可以与其他脚本正常工作,但是当我将每个项目上的$ _POST更改为$ _REQUEST时,该脚本仅在邮递员中有效,并且链接看起来也有所不同。还值得一提的是,onResponse和onFailure不会接收/打印任何消息。
邮递员网址
http://123.456.7.89/Database/register.php?username=asdfasdf&password=lj4ioidfoislkjsf&email=jalenpauloiudf&tece=true&profilePicPath=aksdfjlakjdsfl&tokenId=as89gwbeg98sd
当前网址
http://123.456.7.89/Database/register.php
脚本
<?php
$servername = "localhost";
$dbUsername = "root";
$dbPassword = "password";
$dbname = "Database";
try {
$conn = new PDO("mysql:host=$servername;dbname=$dbname", $dbUsername, $dbPassword);
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$username = $_POST['username'];
$password = $_POST['password'];
$email = $_POST['email'];
$termsOfService = $_POST['termsOfService'];
$profilePicPath = $_POST['profilePicPath'];
$tokenId = $_POST['tokenId'];
$userId = uniqId();
//converting data
$termsOfService = $termsOfService ? 'true' : 'false';
//Username check
$emailForQuery = "%" . $_POST['email'] . "%";
$userIdForQuery = "%" . $userId . "%";
$usernameCheckQuery = $conn->prepare("SELECT email, userId FROM users WHERE email LIKE ? OR userId LIKE ? LIMIT 1");
$usernameCheckQuery->execute([$emailForQuery, $userIdForQuery]);
$usernameCheckQuery->fetch();
if ($usernameCheckQuery->rowCount() == 0) {//username doesnt exist
$usernameCheckQuery->closeCursor();
$hashedPassword = password_hash($password, PASSWORD_DEFAULT);
$query = $conn->prepare("INSERT INTO users(userId, username, password, email, termsOfService, profilePicPath, tokenId)
VALUES(?, ?, ?, ?, ?, ?, ?)");
$query->execute([$userId, $username, $hashedPassword, $email, $termsOfService, $profilePicPath, $tokenId]);
$query->closeCursor();
header('Content-type:application/json;charset=utf-8');
$resArray = ['success' => true, 'userId' => $userId];
echo json_encode($resArray);
} else { //username exists
$usernameCheckQuery->closeCursor();
connectionError("username already exists");
}
} catch(PDOException $e) {
connectionError();
}
function connectionError($message = "database connection error") { //sending response
header('Content-type:application/json;charset=utf-8');
$resArray = ['success' => false, 'message' => $message];
echo json_encode($resArray);
$conn = null;
}
?>
Android帖子数据
@FormUrlEncoded
@POST("register.php")
fun signUpUser(@Field("username") username: String,
@Field("password") password: String,
@Field("email") email: String,
@Field("termsOfService") termsOfService: Boolean,
@Field("profilePicPath") profilePicPath: String,
@Field("tokenId") tokenId: String): Call<ResponseLoginOrSignUp>
我的猜测是它与网址有关,但随后响应仍未发送。
如果需要任何其他代码,请告诉我,请加上添加。
答案!!! 这是我的结论,我的域名安全性不佳,需要将ssl添加到我的托管域以及自定义域中。对于ssl,我使用cloudflare来寻找免费ssl的任何人,因为花了我一段时间才找到合适的网站。还将android studio中显示的网址中的http切换为https
答案 0 :(得分:0)
在android清单中使用 android:usesCleartextTraffic =“ true”
答案 1 :(得分:0)
首先,尝试将Http
更改为https
然后我建议使用android volley发布请求
https://developer.android.com/training/volley[Android齐射示例] 1