How to detect upload/download transfer rate in Android?

后端 未结 3 1464
借酒劲吻你
借酒劲吻你 2020-12-30 17:20

I am working on an app which uploads a large amount of data. I want to determine the transfer rate of the upload, to show in a notification.

  • One post suggests
3条回答
  •  -上瘾入骨i
    2020-12-30 17:58

    What you're trying to determine is the transfer rate of the bytes being uploaded over your HTTP Client. Obviously, this depends on the HTTP client you're using.

    There's no out-of-the-box solution which applies to all HTTP clients used on Android. The Android SDK does not provide any methods for you to determine the transfer rate of a particular upload.

    Fortunately, you're using OKHttp and there is a relatively straight-forward way to do this. You're going to have to implement a custom RequestBody, and observe the bytes being written to the buffer when the request is in flight.

    There's a 'recipe' for doing this on the OkHttp Github: https://github.com/square/okhttp/blob/master/samples/guide/src/main/java/okhttp3/recipes/Progress.java

    You could also refer to this StackOverflow question dealing with the exact same topic: Tracking progress of multipart file upload using OKHTTP

    Another here: OKHTTP 3 Tracking Multipart upload progress

提交回复
热议问题