【问题标题】:PHP Post body is null with Retrofit when it's not null当 PHP Post body 不为 null 时,它是 null 和 Retrofit
【发布时间】:2020-02-09 23:28:32
【问题描述】:

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

日志

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)

PHP 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;
    }
}

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

移动改造 API

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)

登录正文

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

授权模型

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)
        }
    })
}

改造对象

object RetrofitApi {
    private var BASE_URL = "https://site-url/"
    private var retrofit: Retrofit? = null

    fun getInstance(): Retrofit {
        if (retrofit == null) {
            val gson = GsonBuilder()
                    .setLenient()
                    .create()

            val logging = HttpLoggingInterceptor()
            // set your desired log level
            logging.level = HttpLoggingInterceptor.Level.BODY
            val httpClient = OkHttpClient.Builder().apply {
                connectTimeout(30, TimeUnit.SECONDS)
                readTimeout(30, TimeUnit.SECONDS)
                addInterceptor(logging)
            }

            retrofit = Retrofit.Builder()
                    .baseUrl(BASE_URL)
                    .addConverterFactory(GsonConverterFactory.create(gson))
                    .client(httpClient.build())
                    .build()
        }
        return retrofit!!
    }
}

【问题讨论】:

    标签: php android post kotlin retrofit2


    【解决方案1】:

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

    【讨论】:

      猜你喜欢
      • 2016-12-30
      • 1970-01-01
      • 2012-05-01
      • 2019-11-29
      • 2017-08-18
      • 1970-01-01
      • 2022-12-05
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多