Skip to content
Advertisement

$_POST request works with postman but not in android app

I’ve been on this problem for a while now currently trying to convert my php scripts to PDO but the $_POST are always giving me null when ran through isset(). My code in android works fine with my other scripts but this one only works in postman when I change the $_POST to $_REQUEST on each item and the links also look different. Its also worth mentioning that onResponse and onFailure are not receiving/printing any messages to.

Postman URL

http://123.456.7.89/Database/register.php?username=asdfasdf&password=lj4ioidfoislkjsf&email=jalenpauloiudf&tece=true&profilePicPath=aksdfjlakjdsfl&tokenId=as89gwbeg98sd

Current URL

http://123.456.7.89/Database/register.php

Script

<?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 post data

    @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>

My guess is it has to do with the urls but then again the response isn’t sending.

If any additional code is needed please let me know and ill add it, thanks.

ANSWER!!! Here’s my conclusion, I had a domain that wasn’t properly secure and needed to add ssl to my hosting domain as well as a custom domain. For the ssl I used cloudflare for anyone looking for free ssl since it took me a while to find a proper site. Also switch http to https in the url showcased in android studio

Advertisement

Answer

First, try changing the Http to https

Then I recommend using android volley to post requests

https://developer.android.com/training/volley[Android volley example]1

User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement