简体   繁体   English

如何从 API 发出需要主体并返回 boolean 的 GET 请求? 安卓/科特林/改造

[英]How to make a GET request from API that requires a Body and returns a boolean? Android/Kotlin/Retrofit

(For privacy purposes, I will be using very general terms) (出于隐私目的,我将使用非常笼统的术语)

Although I can make a GET request that takes in a body and returns a boolean successfully on Postman, as shown in this image Postman Screenshot , I can't get it to work for my Android app.虽然我可以发出接收正文并在 Postman 上成功返回 boolean 的 GET 请求,如图Postman 屏幕截图所示,但我无法让它为我的 Android 应用程序工作。

I have an API interface with the code:我有一个带有代码的 API 接口:

@GET("api/is_correct")
    suspend fun isCorrect(
       @Body email: Email
    ): Response<Boolean>

and a method in my view model as shown below和我认为的方法 model 如下所示

fun checkIfCorrect(input: String) {
        val email = Email(input)
        viewModelScope.launch {
            try {
                val response = RetrofitInstance.api.isCorrect(email)
                Log.e(TAG, response.toString())
            } catch (e: Exception){
                Log.e(TAG, "error")
            }
        }
}

and this data class和这个数据 class

data class Email (
    @SerializedName("email")
    val email: String
)

The log only prints out "error" when I call viewmodel.checkIfCorrect(...)当我调用 viewmodel.checkIfCorrect(...) 时,日志只打印出“错误”

I've basically used the same process for all my other PUT, GET, and POST API calls.我基本上对所有其他 PUT、GET 和 POST API 调用使用了相同的过程。 This is the only one that's causing trouble for me.这是唯一给我带来麻烦的。 I'm guessing it's because the response body for this particular api call isn't wrapped in { } and does not have a format like "result": true, the way other API responses do.我猜这是因为这个特定的 api 调用的响应主体没有包含在 {} 中并且没有像“result”这样的格式:true,其他 API 响应的方式。

How can I fix this issue?我该如何解决这个问题?

I've tried Response, Response, String, and Boolean as the return type for suspend fun isCorrect.我试过 Response、Response、String 和 Boolean 作为 suspend fun isCorrect 的返回类型。 I've also tried using Query("email") email: String and Path("email") email: String as the parameter for fun isCorrect even though my api endpoint does not require additional parameters in the URL, only the body.我也尝试过使用 Query("email") email: String and Path("email") email: String 作为 fun isCorrect 的参数,即使我的 api 端点不需要 URL 中的其他参数,只需要正文。

If the api already exists there is not much you can do but try this as Retrofit may not support GET with body如果 api 已经存在,那么你无能为力,但试试这个,因为 Retrofit 可能不支持带正文的 GET

        /**
         * import okhttp3.OkHttpClient
         * import okhttp3.Request
         * import okhttp3.RequestBody
         * import okhttp3.Response
         */
        val client = OkHttpClient().newBuilder()
            .build()
        val mediaType: MediaType = MediaType.parseMediaType("application/json")
        val body: RequestBody = RequestBody.create(mediaType, "{ \"message\":\"MESSAGE\"}")
        val request: Request = Request.Builder()
            .url("http://localhost:8080/sample/public")
            .method("GET", body)
            .addHeader("Content-Type", "application/json")
            .build()
        val response: Response = client.newCall(request).execute()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM