【发布时间】:2019-10-01 18:32:15
【问题描述】:
得到响应{protocol=http/1.1, code=404, message=Not Found, url=https://test.test.com/service/one}
网址正确,因为邮递员工作正常。
我已尝试调查此错误,但大多数返回的 URL 都是正确的。并且错误本身是模糊的。
启动它的代码。 builder 是一个有效的 json 字符串。我已经在邮递员中对其进行了测试。
CoroutineScope(Dispatchers.Default).launch {
val call = submitService.submitCarton(builder.toString())
Log.d("submit", "begining")
withContext(Dispatchers.Main) {
if (call.isSuccessful) {
Log.d("submit",call.body() as String)
} else {
Log.d("submit", "else....")
}
}
}
服务工厂:
fun makeSubmitService() : SubmitService{
val url = "https://test.test.com/service/"
return Retrofit.Builder().baseUrl(url)
.client(okHttpClient).addConverterFactory(GsonConverterFactory.create())
.build().create(SubmitService::class.java)
}
界面:
interface SubmitService {
@POST("one")
suspend fun submitCarton(@Body json: String): Response<myModel>
}
预期的结果是一个 json 响应,但我没有走那么远。
编辑:我创建了一个 okhttpclient 并做了一个请求手册,我收到一条消息 200 ok。
我的测试代码
val JSON = MediaType.parse("application/json; charset=utf-8")
val client = OkHttpClient()
val body = "some json"
val requestBody = RequestBody.create(JSON, body)
val request = Request.Builder()
.url("https://test.test.com/service/one")
.post(requestBody)
.build()
client.newCall(request).enqueue(object : Callback {
override fun onFailure(request: Request, e: IOException) {
Log.e("test", e.toString())
}
@Throws(IOException::class)
override fun onResponse(response: Response) {
Log.d("test", response.toString())
}
})
【问题讨论】: