Network
- Network library with the ability to induce dependency on the request queues.
- Operations are seperated from requests, you can do multiple requests and time consuming codes in a single operation.
- You can use this library for any type of time consuming operations and not necessarily network requests.
There are two main classes: NetworkOperaion
and NetworkOperator
. You get the singleton instance of NetworkOperator
and then post NetworkOperations
on it to execute them.
Usage
Add the dependency:
dependencies {
compile 'com.yashoid:network:2.1.1'
}
Following description is out dated. And will be updated soon.
Network Operator
There are three queues for running the operations.UI content Is the queue with the most importance. As long as there is some operation on this queue. No other operations will be executed.
User action This queue has next level of importance. As long as there is some operation on this queue. No background operation will be executed.
Background Operations on this queue will only be executed if there are no other operations left to be execuuted.
If some operation is being executed on a lower importance queue and some new operation is sent to a more important queue, the new operation will be executed in parallel with the current operation(s). After the less important operation(s) is finished, the less important queue(s) will wait for the more important one to get free.
Network Operation
Each operation has a type that identifies the operation's queue and a priority. Operations with higher priority in a queue will be executed before operations with a lower priority.
Network operations structure is very similar to an AsyncTask
. operate()
will be called on a thread and onOperationFinished()
will be called on the Main Thread. publishProgress
and onProgressUpdate()
are exactly similar to AsyncTask
.
Network Request
There are subclasses of NetworkRequest
class to perform API calls. Using network requests is optional.
Available network requests
- JsonObjectRequest
- JsonArrayRequest
- JsonReaderRequest
- FileRequest
- StringRequest
There are useful methods to set the body for the request. Using them you can set different types of objects as the body that includes: JsonObject, JsonArray, String, InputStream, File. Calling setBody()
will automatically change the request method to POST.
In addition you can set the method of the request which you can use to set non-standard methods (HttpUrlRequest normally throws a runtime exception). Also there is a method to directly access the underlying HttpUrlRequest
for other uses like setting request headers.
Attention Getting request's response code is valid only after you have executed the request. In case you are using JsonReaderRequest
remember to call disconnect()
after you have finished using the JsonReader
. For other requests disconnect is called by default.