kotlin:retrofit2 获取 404 url 未找到错误
kotlin: retrofit2 getting 404 url not found error
获取响应{协议=http/1.1,代码=404,消息=未找到,url=https://test.test.com/service/one}
url 是正确的,因为邮递员工作正常。
我已尝试调查此错误,但大多数结果都显示 URL 是正确的。并且错误本身是模糊的。
启动它的代码。构建器是一个有效的 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())
}
})
自己解决了。
问题很愚蠢,retrofit2 给出了 404,即使网络服务正在返回一条错误消息。
已添加
implementation 'com.squareup.okhttp3:logging-interceptor:3.12.1'
private val interceptor = HttpLoggingInterceptor().setLevel(HttpLoggingInterceptor.Level.BODY)
private val okHttpClient = OkHttpClient().newBuilder()
.connectTimeout(1, TimeUnit.MINUTES)
.readTimeout(30, TimeUnit.SECONDS)
.writeTimeout(20, TimeUnit.SECONDS)
.addInterceptor(interceptor)
.build()
发现改造发送了一个非常未格式化的字符串
"{ \"all my json filled with \" }"
而不是
{ json }
通过添加
修复了它
.addConverterFactory(ScalarsConverterFactory.create())
到我的服务工厂
对于任何想知道为什么我基本上将 json 创建为字符串而不是使用 JSON 对象的人,因为我与之交谈的服务真的希望它以非常特定的顺序排列JSON 只是不关心它,但它希望它看起来也像 JSON...
获取响应{协议=http/1.1,代码=404,消息=未找到,url=https://test.test.com/service/one}
url 是正确的,因为邮递员工作正常。
我已尝试调查此错误,但大多数结果都显示 URL 是正确的。并且错误本身是模糊的。
启动它的代码。构建器是一个有效的 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())
}
})
自己解决了。
问题很愚蠢,retrofit2 给出了 404,即使网络服务正在返回一条错误消息。
已添加
implementation 'com.squareup.okhttp3:logging-interceptor:3.12.1'
private val interceptor = HttpLoggingInterceptor().setLevel(HttpLoggingInterceptor.Level.BODY)
private val okHttpClient = OkHttpClient().newBuilder()
.connectTimeout(1, TimeUnit.MINUTES)
.readTimeout(30, TimeUnit.SECONDS)
.writeTimeout(20, TimeUnit.SECONDS)
.addInterceptor(interceptor)
.build()
发现改造发送了一个非常未格式化的字符串
"{ \"all my json filled with \" }"
而不是
{ json }
通过添加
修复了它.addConverterFactory(ScalarsConverterFactory.create())
到我的服务工厂
对于任何想知道为什么我基本上将 json 创建为字符串而不是使用 JSON 对象的人,因为我与之交谈的服务真的希望它以非常特定的顺序排列JSON 只是不关心它,但它希望它看起来也像 JSON...