android 安卓改造:400错误的请求响应代码

epfja78i  于 2023-01-28  发布在  Android
关注(0)|答案(2)|浏览(271)

所以我还在学习android开发的过程中,我目前正在开发一个应用程序,它应该会向学生显示他们的成绩。现在我陷入了登录一个收集成绩的服务的困境。为了这个过程,我使用了https://eduo-ocjene-docs.vercel.app/ api(文档是克罗地亚语)。这是curl请求登录的样子:

curl --location --request GET 'https://ocjene.eduo.help/api/login' \--header 'Content-Type: application/json' \--data-raw '{    "username":"ivan.horvat@skole.hr", "password":"ivanovPassword123"}'

以下是我到目前为止尝试的截图
我是这样做的

object ApiModule {

    private const val BASE_URL = "https://ocjene.eduo.help/"

    lateinit var retrofit: EdnevnikApiService

    private val json = Json { ignoreUnknownKeys = true }

    fun initRetrofit() {
        val okhttp = OkHttpClient.Builder().addInterceptor(HttpLoggingInterceptor().apply {
            level = HttpLoggingInterceptor.Level.BODY
        }).build()

        retrofit = Retrofit.Builder().baseUrl(BASE_URL)
            .addConverterFactory(json.asConverterFactory("application/json".toMediaType()))
            .client(okhttp).build().create(EdnevnikApiService::class.java)
    }

}

登录方法

interface EdnevnikApiService {

    @HTTP(method = "get", path = "/api/login", hasBody = true)
    fun login(@Body request: LoginRequest): Call<LoginResponse>

}

这是单击登录按钮时发生的情况

fun onLoginButtonClicked(email: String, password: String) {
    val request = LoginRequest(email, password)
    ApiModule.retrofit.login(request).enqueue(object : Callback<LoginResponse> {
        override fun onResponse(call: Call<LoginResponse>, response: Response<LoginResponse>) {
            loginResultLiveData.value = response.isSuccessful
            val body = response.body()
        }

        override fun onFailure(call: Call<LoginResponse>, t: Throwable) {
            loginResultLiveData.value = false
        }

    })
}

这是kotlin请求和kotlin响应数据类的样子

@kotlinx.serialization.Serializable
data class LoginRequest(
    @SerialName("username") val username: String,
    @SerialName("password") val password: String,
)
@kotlinx.serialization.Serializable
data class LoginResponse(
    @SerialName("LoginSuccessful") val isSuccessful: Boolean,
)

哦,这是我发送请求

时从拦截器得到的结果

guicsvcw

guicsvcw1#

我猜测服务器响应400 Bad Request是由于不支持的方法类型。当我在示例代码中将method = "get"替换为method = "GET"时,我收到:

java.lang.IllegalArgumentException: method GET must not have a request body.

幸运的是,您共享的/login API可以使用POST方法类型,因此您可以尝试使用:

@HTTP(method = "POST", path = "/api/login", hasBody = true,)

我检查了我这边,收到了以下回复:

<-- 200 https://ocjene.eduo.help/api/login (1390ms)
access-control-allow-origin: *
access-control-allow-credentials: true
set-cookie: epicCookie=f69fbd6d4f10b5cc38e038b5da0843b356776c58c4fb32aed24dbcc49026778724bc25e21448c05a29df9f4b5558b254011fb3f8a992710f9901f23c53be5eaadaa799f3f5ac9e18de191bed02ef3e96030b83042ee8392755b03dd785edca6a;
content-type: application/json; charset=utf-8
etag: "bkrbkvg0eo6c"
vary: Accept-Encoding
date: Thu, 10 Nov 2022 03:07:08 GMT
server: Fly/b1863e2e7 (2022-11-09)
via: 2 fly.io
fly-request-id: 01GHFR2T56X9K0GFN3DH1Z9JYV-sin
{"LoginSuccessful":false,"token":"f69fbd6d4f10b5cc38e038b5da0843b356776c58c4fb32aed24dbcc49026778724bc25e21448c05a29df9f4b5558b254011fb3f8a992710f9901f23c53be5eaadaa799f3f5ac9e18de191bed02ef3e96030b83042ee8392755b03dd785edca6a"}
<-- END HTTP (228-byte body)
mnemlml8

mnemlml82#

object ApiModule {
    private const val BASE_URL = "https://ocjene.eduo.help/"
    lateinit var retrofit: EdnevnikApiService
    private val json = Json { ignoreUnknownKeys = true }
    fun initRetrofit() {
        val okhttp = OkHttpClient.Builder().addInterceptor(HttpLoggingInterceptor().apply {
            level = HttpLoggingInterceptor.Level.BODY
        }).build()
        retrofit = Retrofit.Builder().baseUrl(BASE_URL)
            .addConverterFactory(json.asConverterFactory("application/json".toMediaType()))
            .client(okhttp).build().create(EdnevnikApiService::class.java)
    }
}

相关问题