I am using retrofit and I have a post request

interface NetworkApi {

@Headers("Content-Type: application/json")
@POST("/")
fun startLoginRequest(@Body loginModel : LoginModel) : Call<BaseResponseModel>

class Factory {
    var retrofit = Retrofit.Builder()
            .baseUrl(BASE_URL)
            .addConverterFactory(GsonConverterFactory.create())
            .build()

    companion object{
        fun getApi (): NetworkApi {
            val serverApi = NetworkApi.Factory()
            val api = serverApi.retrofit.create(NetworkApi::class.java)
            return api
        }
    }
}

}

当我使用此方法和On Response方法调用时,Response Body始终为空.

fun startLoginRequest(){
    val api = NetworkApi.Factory.getApi()
    val request = api.startLoginRequest(loginRequestModel)

    request.enqueue(object : Callback<BaseResponseModel>{
        override fun onFailure(call: Call<BaseResponseModel>?, t: Throwable?) {

        }

        override fun onResponse(call: Call<BaseResponseModel>?, response: Response<BaseResponseModel>?) {
            //here response.body is null
        }
    })
}

Strange thing is that, if I will send the same object using Postman everything works fine

这是httpClient侦听器日志(log)

--> POST http://myExampleHost.net/ http/1.1
Content-Type: application/json

Content-Length: 95
Authorization: auth-value
--> END POST (95-byte body)
<-- 405 Method Not Allowed http://myExampleHost.net/ (198ms)
Allow: GET, HEAD, OPTIONS, TRACE, COPY, PROPFIND, LOCK, UNLOCK
Content-Type: text/html
Server: Microsoft-IIS/7.5
X-Powered-By: ASP.NET
X-Powered-By-Plesk: PleskWin
Date: Thu, 17 Nov 2016 08:04:57 GMT
Content-Length: 1107
<HTML>
<HEAD>
<TITLE>405 Method Not Allowed</TITLE>
<BASE href="/error_docs/"><!--[if lte IE 6]></BASE><![endif]-->
</HEAD>
<BODY>
<H1>Method Not Allowed</H1>
The HTTP verb used to access this page is not allowed.<P>
<HR>
<ADDRESS>
Web Server at example address
</ADDRESS>
</BODY>
</HTML>

For now I'm using OkHttp RequestBuilder and everything works fine, I don't realize what I'm missing in the above example

val client = OkHttpClient()
    val JSON = MediaType.parse("application/json; charset=utf-8")
    val body = RequestBody.create(JSON,json)
    val request1 = Request.Builder()
            .url("http:example.com")
            .addHeader("content-type", "application/json")
            .method("POST", body)
            .build()

    client.newCall(request1).enqueue(object : okhttp3.Callback{
        override fun onFailure(call: okhttp3.Call?, e: IOException?) {

        }

        override fun onResponse(call: okhttp3.Call?, response: okhttp3.Response?) {
            response?.body()?.string()?.print("RETROFIT response")
        }
    })

推荐答案

I found the answer, the problem was url, my baseUrl were http//www.example.com/url1/url2/...../service and @POST("/")

when I saw the POST endpoint it was http//www.example.com/ and I don't know why

I just changed this urls baseUrl = http//www.example.com/ and @POST("url1/url2/...../service")

Kotlin相关问答推荐

编译后的JavaFX应用程序立即以静默方式崩溃

&x是T&q;和&q;(x为?T)!=空(&Q;)?

在Spring Boot应用程序中使用网络请求功能将关键字挂起作为冗余

Kotlin 中命名构造函数的惯用方式

Kotlin中用于调用常量名称的枚举类方法之间的区别

将 java Optional 转换为 Kotlin Arrow Option

Kotlin SAM/功能接口抛出 AbstractMethodError

在 kotlin 中模拟伴随对象函数

Kotlin RxJava 可空的错误

Kotlin 协程中的 Dispatchers.Main 和 Dispatchers.Default 有什么区别?

如何修复 ViewPager2 中的Design assumption violated错误?

如何在顶级函数中使用 koin 注入依赖项

将协同路由调用放在存储库或ViewModel中哪个更好?

取消信号上的kotlin流量采集

kotlin RecyclerView分页

在Kotlin中为Android编写库会有开销吗?

如何让数据类在Kotlin中实现接口/扩展超类属性?

在 Kotlin 函数上使用 Mokito anyObject() 时,指定为非 null 的参数为 null

使用 Moshi/Retrofit2 访问深度嵌套的 JSON 数组

Android room DAO 接口不适用于继承