Asynchronous HTTP Request, queuing requests

2019-03-31 18:39发布

I have developed for appstore before and I used ASIHTTPRequest. Is there something similar for Android?

Am looking at making a few HTTP requests, which are queued and manageable. This is what I need to do:

  1. Set-up a queue of lets say 6 http requests.
  2. Once the queue is setup, execute it with each request giving me a success/failure result.
  3. If one request fails, I have the option to cancel/clear the whole queue.
  4. If all the requests in the queue are successful, I can have another callback for the whole queue's success event.
  5. And,each request can have a retry count; meaning; I can say each request can retry n number of time before they can return a failure.

ASIHTTPRequest for IOS was pretty good at handling this. Is there anyway I can do this with Android? Most of my content returned is XML and no binary data.

Any feedback is much appreciated.

1条回答
爷、活的狠高调
2楼-- · 2019-03-31 19:24

What you describe is not overly complicated to build.

  1. Extend java.util.Queue
  2. Create custom "listener" interface which handles all the events you need to track.
  3. Custom request objects that allow a "listener" to be set.
  4. Your custom queue will have a thread to process items in it's own queue.
  5. Depending on your exact requirements, you may also need an additional interface/listener to pass back the final response to the object that made the initial request.

When adding an object to the queue, your queue would set itself as the "listener" on the custom request object.

When the events happen in the request object, the event methods will be called on the listener object...in the case the queue...which can react appropriately.

I suspect something like ASIHTTPRequest does not really exist in Java/Android, because it's not as difficult a problem to solve as on iOS. I could implement the above objects into a functional component in 20 minutes or less. Of course, if you've not done something like this before, it may take you much longer.

查看更多
登录 后发表回答