首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >当Post主体为null时,Retrofit为null

当Post主体为null时,Retrofit为null
EN

Stack Overflow用户
提问于 2019-10-12 20:19:16
回答 1查看 525关注 0票数 0

在将站点移动到另一台服务器后,我的php停止工作。现在,我的api认为,通过Retorift发送的post主体是空的(尽管这不是),如果通过reqbin发送,则所有东西都可以工作。

日志

代码语言:javascript
复制
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)

代码语言:javascript
复制
// 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

代码语言:javascript
复制
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)

LoginBody

代码语言:javascript
复制
data class LoginBody(val login: String, val password: String)

AuthModel

代码语言:javascript
复制
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)
        }
    })
}

RetrofitObject

代码语言:javascript
复制
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!!
    }
}
EN

回答 1

Stack Overflow用户

发布于 2019-10-27 07:19:16

这是由重定向(从www.url重定向到url等)造成的,这就是为什么我的php://input是空的。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/58358138

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档