gpt4 book ai didi

android - 使用消息 "method not allowed here"改造响应代码 405

转载 作者:塔克拉玛干 更新时间:2023-11-02 23:56:44 25 4
gpt4 key购买 nike

我正在使用改造,我有一个帖子请求

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

当我使用此方法并调用响应方法时,响应主体始终为空。

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

奇怪的是,如果我使用 Postman 发送相同的对象,一切正常

这是httpClient拦截器日志

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

现在我正在使用 OkHttp RequestBuilder 并且一切正常,我没有意识到我在上面的例子中遗漏了什么

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

最佳答案

我最好的猜测是您试图发布到错误的 url www.example.com/login// 而不是 www.example.com/login/ . API 对帖子可能有点挑剔。

您可以添加日志记录拦截器,这样您就可以看到您正在发布的内容以及发布到哪个 URL,从而更容易调试。要设置它,您需要将 compile "com.squareup.okhttp3:logging-interceptor:$okhttp_version" 添加到您的 gradle 文件并将您的 retrofit 设置更改为如下内容:

val httpClient = OkHttpClient.Builder().apply {
if (BuildConfig.DEBUG) {
httpClient.addInterceptor(HttpLoggingInterceptor().apply {
level = HttpLoggingInterceptor.Level.BODY
})
}
}

var retrofit = Retrofit.Builder()
.client(httpClient.build())
.baseUrl(BASE_URL)
.addConverterFactory(GsonConverterFactory.create())
.build()

这是我在没有看到错误日志的情况下可以给出的最佳建议。

关于android - 使用消息 "method not allowed here"改造响应代码 405,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40638207/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com