Home  >  Q&A  >  body text

android - 点击button后开启分线程网络请求

最近一个项目涉及到好多点击button进行开分线程网络请求,但是问题来了,如果button不做处理,用户不断的去点击button,就会有一堆线程创建,应用卡死,或者在没网络的时候也一直去点击button。有没有好的处理方法,求教!

伊谢尔伦伊谢尔伦2714 days ago623

reply all(8)I'll reply

  • ringa_lee

    ringa_lee2017-04-17 17:54:47

    You can set the time interval between two clicks. If it is less than a certain period of time, the second click will not respond

    reply
    0
  • 迷茫

    迷茫2017-04-17 17:54:47

    1. First determine whether there is a network. If the network is unavailable, return directly
    2. Filter repeated clicks
    3. If you hope that new requests will not be triggered before the previous request is returned, you can set a status flag

    reply
    0
  • 黄舟

    黄舟2017-04-17 17:54:47

    When the user clicks the button for the first time, a friendly prompt interface is given: such as diaolg, which prompts the user to wait for data to be returned.
    Otherwise, in terms of user experience, there is no response when I click the button, and I will keep clicking.
    Hope it helps you.

    reply
    0
  • 高洛峰

    高洛峰2017-04-17 17:54:47

    1. Use thread pool to limit the maximum number of threads.

    2. You cannot initiate network requests for the same URL at the same time

    3. Caching

    Volley can do all the above

    reply
    0
  • 大家讲道理

    大家讲道理2017-04-17 17:54:47

    As mentioned above, a network framework must be used in this situation. Volley is indeed very suitable. I recommend the Volley source code analysis I wrote. I hope it will be helpful to you: https://github.com/wangzhengy...

    reply
    0
  • 巴扎黑

    巴扎黑2017-04-17 17:54:47

    After initiating the request, the Click event of the Button is blocked and restored after the request is completed.

    reply
    0
  • 大家讲道理

    大家讲道理2017-04-17 17:54:47

    After clicking the button, the button will be made non-clickable. If possible, the dialog being requested will pop up. After requesting the data, the dialog will be canceled and the button will be made clickable.

    reply
    0
  • 巴扎黑

    巴扎黑2017-04-17 17:54:47

    Write a judgment. Set a flag, TAG = false; if (!TAG) {//execute}
    When turned on, TAG = true;

    reply
    0
  • Cancelreply