android kotlin凌空慢请求副本获取请求

发布于 2025-01-22 14:57:21 字数 1235 浏览 0 评论 0原文

我使用排球框架向我的烧瓶API提出请求。这是我用来做到这一点的语法:

val jsonObjectRequest =
                JsonObjectRequest(Request.Method.GET,  url, null, { response ->
                    val spiScanApiResponse = response["success"] as String

                    if (spiScanApiResponse == "true") {
                        Toast.makeText(this, "SCANS SENT WITH SUCCESS!", Toast.LENGTH_LONG).show()
                        finish()
                    } else {
                        Toast.makeText(
                            this,
                            "SCANS WERE NOT SENT!",
                            Toast.LENGTH_LONG
                        )
                            .show()                            
                    }
                },
                    {

                    }
                )
            queue.add(jsonObjectRequest)

我得到了这个“凌空:[111] networkutility.logslowrequests:request request =< [] http:// myurl 0xbeaaf2f6 http响应= 114],[rc = 200],[retrycount = 1]”,这正在重复get请求,并将数据发送两倍到我的数据库。

从我阅读的内容来看,这是一个重试 /超时的问题,但是我找不到任何可以帮助使用Kotlin的东西,我得到的所有可能的答案都在Java中,我在Google上找不到有关Retrypolicy或超时的Kotlin版本的任何内容。

有没有一种方法可以使用凌空进行这项工作?我不想将所有请求切换到另一个框架,因为这是唯一不起作用的请求。

谢谢你!

I use the Volley framework to make GET requests to my Flask API. This is the syntax I use to do that:

val jsonObjectRequest =
                JsonObjectRequest(Request.Method.GET,  url, null, { response ->
                    val spiScanApiResponse = response["success"] as String

                    if (spiScanApiResponse == "true") {
                        Toast.makeText(this, "SCANS SENT WITH SUCCESS!", Toast.LENGTH_LONG).show()
                        finish()
                    } else {
                        Toast.makeText(
                            this,
                            "SCANS WERE NOT SENT!",
                            Toast.LENGTH_LONG
                        )
                            .show()                            
                    }
                },
                    {

                    }
                )
            queue.add(jsonObjectRequest)

I am getting this "Volley: [111] NetworkUtility.logSlowRequests: HTTP response for request=<[ ] https://myUrl 0xbeaaf2f6 NORMAL 1> [lifetime=6596], [size=114], [rc=200], [retryCount=1]" and this is duplicating the GET request and sends twice the data to my database.

From what I read it's a RetryPolicy / Timeout issue, but I can't find anything to help using Kotlin, all the possible answers I get are in Java and I can't find anything on Google about Kotlin Version of RetryPolicy or Timeout.

Is there a way to make this work using Volley? I wouldn't want to switch all my requests to another framework as this is the only request that does not work.

Thank you!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

把人绕傻吧 2025-01-29 14:57:21

我终于弄清楚了,这是在Kotlin中:

不再需要重复的请求,我必须设置JSONOBJECTREQUEST.RETRYPOLICY.RETRYPOLICY在将其添加到这样的队列之前:

jsonObjectRequest.retryPolicy = 
    DefaultRetryPolicy(
        0,
        DefaultRetryPolicy.DEFAULT_MAX_RETRIES,
        DefaultRetryPolicy.DEFAULT_BACKOFF_MULT
    )
queue.add(jsonObjectRequest)

就是这样。
现在它就像魅力一样!
我希望这也会对他人有所帮助。

I finally figured it out and here it is how, in Kotlin:

to no more have duplicated requests I had to set the jsonObjectRequest.retryPolicy before adding it to the queue like this:

jsonObjectRequest.retryPolicy = 
    DefaultRetryPolicy(
        0,
        DefaultRetryPolicy.DEFAULT_MAX_RETRIES,
        DefaultRetryPolicy.DEFAULT_BACKOFF_MULT
    )
queue.add(jsonObjectRequest)

And that's it.
Now it works like a charm!
I hope this will help others too.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文