kotlin Retrofit2邮政请求返回500错误,但 Postman 工作良好

ih99xse1  于 2023-02-13  发布在  Kotlin
关注(0)|答案(1)|浏览(148)

我已经尝试了很多解决方案Stackoverflow.但没有工作...请帮助我...
Retrofit 2 POST请求返回500错误。但 Postman 工作得很好。但如果我改变了Jsonobject的属性名,工作得很好。 Postman 也是。详情如下。
我问了服务器团队,但他们说没有问题。所以你能检查一下我的代码是否有问题吗?

接口

'接口登录服务{

@POST("api/v1/login") // POST
fun requestLogin( // Input
    @Body loginData: JsonObject
): Call<LoginResponse> // Output`

登录活动

'类登录活动:AppCompatActivity(){私有后期初始化变量视图绑定:活动登录绑定覆盖创建功能(保存的示例状态:捆绑包?){超级创建(保存的示例状态)视图绑定=活动登录绑定。膨胀(布局膨胀器)设置内容视图(视图绑定。根)

// client
    val clientBuilder = OkHttpClient.Builder()
    val loggingInterceptor = HttpLoggingInterceptor()
    loggingInterceptor.level = HttpLoggingInterceptor.Level.BODY
    clientBuilder.addInterceptor(loggingInterceptor)
    clientBuilder.retryOnConnectionFailure(true)

    // retrofit
    val retrofit = Retrofit.Builder()
        .baseUrl("secret")
        .addConverterFactory(ScalarsConverterFactory.create())
        .addConverterFactory(GsonConverterFactory.create())
        .client(clientBuilder.build()) // client 
        .build()
    val userid =  viewBinding.idt.text.toString()
    val password = viewBinding.pwdt.text.toString()

    val login = JsonObject()
    login.addProperty("userId", userid)
    login.addProperty("password", password)
    val loginService = retrofit.create(LoginService::class.java)

    viewBinding.signinbtn.setOnClickListener {
        loginService.requestLogin(login).enqueue(object: Callback<LoginResponse>{
            override fun onResponse(call: Call<LoginResponse>, response: Response<LoginResponse>) {
                if (response.isSuccessful){
                    val responseData = response.body()
                    if (responseData != null) {
                        Log.d("Retrofit","ResponseCode: ${responseData.code} Message: ${responseData.message}")

                        if (responseData.code == 1000) {
                            val intent = Intent(this@LoginActivity, CalendarActivity::class.java)
                            startActivity(intent)
                        }

                        if (responseData.code != 1000) {
                            cuDialog(viewBinding.root, responseData.message)
                        }

                        if (responseData.code == 1000 && viewBinding.auto.isChecked) {
                        }
                    }
                }
                else {
                    Log.w("Retrofit", "Response Not Successful ${response.code()}")
                }
            }

            override fun onFailure(call: Call<LoginResponse>, t: Throwable) {
                Log.e("Retrofit","Error!",t)
                val dialog = AlertDialog.Builder(this@LoginActivity)
                dialog.setTitle("오류")
                dialog.setMessage("서버와 통신에 실패했습니다.")
                dialog.show()
            }
        })
    }`

数据类(响应)

data class LoginResponse( val isSuccess: Boolean, val code: Int, val message: String, val result: ArrayList<Info>)

错误消息

enter image description here

IN Postman

enter image description here
Postman 工作很好
但更奇怪的是,如果我随机更改属性的名称,它的响应很好,下面是一个例子。
val login = JsonObject() login.addProperty("sdfsfssdfd", userid) login.addProperty("sfsfdsfsd", password)
我更改了这部分。属性名称是随机的。
enter image description here
在AndroidStudio中,运行良好。enter image description here当然, Postman 也运行良好。
为什么如果属性名称是“userId”,不工作?请帮助我..

neskvpey

neskvpey1#

从你的用户名和密码参数是空的。我看到你的错误截图。
尝试记录您的用户ID和密码。

val userid =  viewBinding.idt.text.toString() // I think issue is here
val password = viewBinding.pwdt.text.toString() // I think issue is here 
login.addProperty("userId", userid) 
login.addProperty("password", password)

相关问题