Ion is an Android asynchronous network and image loading library, and the elegant API greatly simplifies network operation.
Address: Https://github.com/koush/ion
Characteristics:
Asynchronous Download:
Easy-to-use streaming API
-
Automatically cancels operations when the calling Activity finishes
-
Manages invocation back onto the UI thread
-
All operations return a future and can cancelled
HTTP Post/put:
Transparent usage of HTTP features and optimizations:
-
SPDY and HTTP/2
-
caching
-
gzip /deflate Compression
-
connection pooling/reuse via HTTP connection:keep-alive
-
Uses the Best/stablest Connection from a server if it has multiple IP addres SES
-
cookies
view headers
grouping and cancellation of requests
Download Progress Callbacks
supports file:/, HTTP (s):/, and content:/ URIs
Request level logging and profiling
Support For proxy Servers like Charles Proxy to does request analysis
based on NIO and Androidasync
ability to use the self Signed certificates
Example: Https://github.com/koush/ion/tree/master/ion-sample
Installation:
Jar Mode:
Maven:
<dependency> <groupId>com.koushikdutta.ion</groupId> <artifactId>ion</artifactId> & Lt;version>2,</version></dependency>
Gradle:
dependencies {compile ' com.koushikdutta.ion:ion:2.+ '}
Use:Get JSON
Ion.with (context). Load ("Http://example.com/thing.json"). Asjsonobject (). Setcallback (New futurecallback< Jsonobject> () {@Override public void oncompleted (Exception e, jsonobject result) {//does stuff with the RE Sult or Error}});
Post JSON and read JSON
Jsonobject json = new Jsonobject (); Json.addproperty ("foo", "Bar"); Ion.with (context). Load ("Http://example.com/post"). Setjsonobjectbody (JSON). Asjsonobject (). Setcallback (New Futurecallback<jsonobject> () {@Override public void oncompleted (Exception e, jsonobject result) {//Do Stuff with the result or error}});
Post application/x-www-form-urlencoded and read a String
Ion.with (GetContext ()). Load ("Https://koush.clockworkmod.com/test/echo"). Setbodyparameter ("Goop", "NoOp"). Setbodyparameter ("foo", "Bar"). Asstring (). Setcallback (...)
Post Multipart/form-data and read JSON with an upload progress bar
Ion.with (GetContext ()). Load ("Https://koush.clockworkmod.com/test/echo"). Uploadprogressbar (Uploadprogressbar). Setmultipartparameter ("Goop", "NoOp"). Setmultipartfile ("Filename.zip", New File ("/sdcard/filename.zip")). Asjsonobject (). Setcallback (...)
Download a File with a progress bar
Ion.with (context). Load ("Http://example.com/really-big-file.zip")// have a progressbar get updated automatically with the percent.progressbar (ProgressBar)// and a progressdialog.progressdialog (ProgressDialog)// can also use a custom Callback.progress (New progresscallback () {@Override public void OnProgress (int downloaded, int total) { System.out.println (" + downloaded + " / " + total"); }}). Write (New file ("/sdcard/really-big-file.zip")). Setcallback (New futurecallback<file> () { @Override public void oncompleted (Exception e, file file) { // download done ... // do stuff with the file or error }}); Setting Headers
Ion.with (context). Load ("Http://example.com/test.txt")//Set the Header.setheader ("foo", "Bar"). Asstring (). Setcallback (...)
Load an image to an ImageView
This is the "long" by-do build of an ImageView request ... it allows to set headers, etc. Ion.with (context). Load ("Http://example.com/image.png"). Withbitmap (). Placeholder (r.drawable.placeholder_image). Error (R.drawable.error_image). Animateload (spinanimation). Animatein (fadeinanimation). Intoimageview (ImageView); But for brevity, use the ImageView specific builder ... Ion.with (ImageView). Placeholder (r.drawable.placeholder_image). Error (R.drawable.error_image). Animateload ( spinanimation). Animatein (fadeinanimation). Load ("Http://example.com/image.png");
Ion Image Loading API features
-
Disk and Memory caching
-
Bitmaps held via weak references so memory is managed very effeciently
-
ListView Adapter Recycling Support
-
Bitmap transformations via the. Transform (Transform)
-
Animate loading and loaded ImageView states
-
Deepzoom for extremely large images
Futures
All operations return a custom-to-be-allows you-specify a callback that runs on completion.
future<string> String = ion.with (context). Load ("Http://example.com/string.txt"). Asstring (); future<jsonobject> JSON = ion.with (context). Load ("Http://example.com/json.json"). Asjsonobject (); future<file> file = Ion.with (context). Load ("Http://example.com/file.zip"). Write (New file ("/sdcard/file.zip") ); future<bitmap> Bitmap = ion.with (context). Load ("Http://example.com/image.png"). Intoimageview (ImageView);
Cancelling requests
Futures can cancelled by calling. Cancel ():
Bitmap.cancel (); Json.cancel ();
Blocking on Requests
All Futures has a future.get () method that waits for the result of the request, by blocking if necessary.
Jsonobject JSON = ion.with (context). Load ("Http://example.com/thing.json"). Asjsonobject (). get ();
Seamlessly use your own Java classes with Gson (using Gson seamlessly with Pojo):
public static class Tweet {public String ID; public String text; public String photo; public void Gettweets () throws Exception {Ion.with (context). Load ("Http://example.com/api/tweets"). As (new Typet Oken<list<tweet>> () {}). Setcallback (New futurecallback<list<tweet>> () {@Override public void oncompleted (Exception e, list<tweet> tweets) {//Chirp chirp}});
Logging
Wondering why your app is slow? Ion lets both global and request level logging.
To enable it globally:
Ion.getdefault (GetContext ()). Configure (). Setlogging ("Mylogs", log.debug);
Or to enable it on just a single request:
Ion.with (context). Load ("Http://example.com/thing.json"). Setlogging ("Mylogs", Log.debug). Asjsonobject ();
Request groups requests Combination
by default, Ion automatically places all requests to a group with all of the other requests created by that Activity or Service. Using the cancelall (Activity) call, all requests still pending can be easily cancelled:
future<jsonobject> Json1 = Ion.with (activity, "Http://example.com/test.json"). Asjsonobject (); future<jsonobject> Json2 = Ion.with (activity, "Http://example.com/test2.json"). Asjsonobject (); Later. In [email protected] void OnStop () {super.onstop (); Ion.getdefault (activity). Cancelall (activity);}
groups to allow for ease cancellation of requests in that group later:
Custom group easy to manage:
Object jsongroup = new object (); Object imagegroup = new object (); future<jsonobject> json1 = ion.with (activity). Load ("Http://example.com/test.json")/ / tag in a custom group.group (Jsongroup). Asjsonobject (); Future<JsonObject > json2 = ion.with (activity). Load ("Http://example.com/test2.json")// use the Same custom group as the other json request.group (JsonGroup). AsJsonObject () ; future<bitmap> image1 = ion.with (activity). Load ("http://example.com/test.png")// for this image request, use a different group for Images.group (ImageGroup). Intoimageview (imageView1); future<bitmap> image2 = ion.with (activity). Load ("Http://example.com/test2.png")// same imagegroup as before.group ( ImageGroup). Intoimageview (IMAGEVIEW2); // later... to cancel only image downloads:ion.getdefault (activity). Cancelall (ImageGroup);Proxy Servers (like Charles proxy)
Proxy all Requestsion.getdefault (context). Configure (). Proxy ("MyComputer", 8888); Or ... to proxy specific requestsion.with (context). Load ("http://example.com/proxied.html"). Proxy ("MyComputer", 8888). GetString ();
viewing Received Headers
responsefuture , which Grant access to Response Properties via the Response object. responsefuture contains both header and request results
Ion.with (GetContext ()). Load ("Http://example.com/test.txt"). Asstring (). Withresponse (). Setcallback (New Futurecallback<response<string>> () {@Override public void oncompleted (Exception e, RESPONSE<STRING&G T Result) {//Print the response code, IE, System.out.println (result.getheaders (). code ()); Print the String that is downloaded System.out.println (Result.getresult ()); }});
Android ion Asynchronous network and image loading greatly simplifies network development highly recommended