How to make retrofit accept a download progress callback for each request?

Jesse Shi :

Background:
I'm trying to migrate my old downloading code with a progress callback from AsyncTask and HttpUrlConnection in Android to using retrofit. However, I noticed retrofit/okhttp doesn't provide such callback by default.
I've done a lot searching, and find some approach, including the official solution by okhttp, which is done by interceptors and a custom ResponseBody. However, none of them actually meets my needs.
What I want:
The problem of this code is that the callback is actually attached to the interceptor, which makes it attached to the okhttp client, while what I need is applying different callback objects for each request.(different android ui element change etc.)
What I've tried:
For what I understand, a single request is marked by a single retrofit Call interface instance, So if I want to implement a per-request callback, it has to be attached to the Call instance or the RequestBody instance.
Unfortunately, I didn't find any way to get the retrofit Call instance inside okhttp interceptor. Neither can I attach a custom callback object to it.
As for the custom RequestBody approach, it seems feasible. But if it comes to GET request, it's not available again because no RequestBody can be set using retrofit.
Is there any elegant way to achieve that? And by "elegant", I mean I prefer not to maintain a request -> callback map myself in any form, which may even cause memory leak if not with very much caution.

Jesse Wilson :

You might be able to find something with @Tag and/or Invocation. Tag lets you define a parameter on your Retrofit request that will be available in interceptors. Invocation is similar, but it doesn't need an extra paramter.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=26577&siteId=1