PHP Post正文不为null时,Retrofit为null

时间:2019-10-12 20:19:16

标签: php android post kotlin retrofit2

将站点移至另一台服务器后,我的PHP API停止工作。现在我的api认为通过Retorift发送的post正文为空(尽管不是),例如,如果通过reqbin发送,则一切正常

日志

const addPoint = () => {
  if (!gameOver) {
    p1Score++;    
    if (p1Score === winScore) {
      dis1.classList.add("winner");
      gameOver = true;
    }
    dis1.textContent = p1Score;    
  }
}

PHP API

D/OkHttp: --> POST https://site-url
    Content-Type: application/json; charset=UTF-8
    Content-Length: 55
D/OkHttp: {"login":"*****","password":"******"}
    --> END POST (55-byte body)

D/OkHttp: <-- 200 OK https://site-url (492ms)
    Server: nginx
    Date: Sat, 12 Oct 2019 19:52:40 GMT
    Content-Type: text/html; charset=UTF-8
    Connection: keep-alive
    X-Powered-By: PHP/7.3.10
    Vary: Accept-Encoding
    MS-Author-Via: DAV
    X-Powered-By: PleskLin
D/OkHttp: login/password is null
D/OkHttp: <-- END HTTP (22-byte body)

var_dump(file_get_contents('php:// input'))返回字符串(0)“”

移动改造API

// Converts into a PHP object
$data = json_decode(file_get_contents('php://input'), true);

$login = $data['login'];
$password = $data['password'];

if (isset($_GET['type']) && $_GET['type'] != NULL) {
    switch ($_GET['type']) {
        case "auth":
            if($login != NULL && $password != NULL) {
                emailOrUsername($login, $password, $db);
            } else echo 'login/password is null';
        break;
        case "token":
            if($login != NULL && $password != NULL) {
                compareMailToken($login, $password, $db);
            } else echo 'login/password is null';
        break;
    }
}

LoginBody

interface AuthApi {
    @POST("auth.php?type=auth")
    fun authWithPass(@Body loginBody: LoginBody): Call<AuthResponse>
}

class AuthResponse(
        @SerializedName("type")
        val type: AuthResponseType,
        @SerializedName("user")
        val user: User?,
        @SerializedName("message")
        val message: String)

AuthModel

data class LoginBody(val login: String, val password: String)

RetrofitObject

override fun authWithPass(onFinishedListener: AuthContract.Model.OnFinishedListener, email: String, password: String) {
    val authApi = RetrofitApi.getInstance().create(AuthApi::class.java)

    val loginBody = LoginBody(email, password)

    val authQuery = authApi.authWithPass(loginBody)
    authQuery.enqueue(object : Callback<AuthResponse>{
        override fun onResponse(call: Call<AuthResponse>, response: Response<AuthResponse>) {
            response.body()?.let { body ->
                onFinishedListener.onFinished(body.type, body.user, body.message)
            } ?: onFinishedListener.onFinished()
        }

        override fun onFailure(call: Call<AuthResponse>, t: Throwable) {
            onFinishedListener.onFailure(t)
        }
    })
}

1 个答案:

答案 0 :(得分:0)

这是由于重定向(例如,从www.url到url等)引起的,这就是我的php://input为空的原因。