5 Open source libraries that Android developers must know

Source: Internet
Author: User
Tags sqlite database eventbus

Http://www.csdn.net/article/2014-06-16/2820224-top-5-android-libraries

In the past time, Android development has gradually matured. And no matter how many Android-related development tools appear, the vast pool of open-source libraries We use every day is always essential. Here, I'd like to introduce you to the five Android libraries that are most popular among developers in this hard-to-do family.

Android development has evolved over time, with Android-related development tools emerging. However, in the face of a variety of new things, do not forget those we use every day a large number of open source libraries. Here, I'd like to introduce you to the five Android libraries that are most popular among developers in this hard-to-do family. It is hoped that by understanding them, you will be able to contribute to your development efforts.


1. Gson

Gson is a Java class library provided by Google for mapping between Java objects and JSON data. can be used to convert a Java object to a corresponding JSON representation, or to convert a JSON string to an equivalent Java object. If you're dealing with the API, it's going to be something you often need. The reason we mainly use JSON is that the lightweight JSON is much simpler compared to XML.

[JS]View Plaincopy
    1. Serialize
    2. String Userjson = new Gson (). ToJson (user);
    3. Deserialize
    4. User user = new Gson (). Fromjson (Userjson, user.   Class);
2. RETROFIT

As described on its website, "Retrofit your rest API into Java interface", retrofit the data returned by the rest API into Java objects for ease of operation and is a good solution for organizing API calls in your project. Both the request method and the relative URL are annotated, making the code more concise. With annotations, you can easily add a request body, manipulate a URL or header file, and add query parameters. In addition, each function can be defined as synchronous or asynchronous, and the function with the return value is executed synchronously, and the async function has no return value and the last argument is a callback object.

[JS]View Plaincopy
  1. Public interface Retrofitinterface {
  2. //Asynchronously with a callback
  3. @GET ("/api/user")
  4. User GetUser (@Query ("user_id") int userId, callback<user> Callback);
  5. //synchronously
  6. @POST ("/api/user/register")
  7. User RegisterUser (@Body user user);
  8. }
  9. Example
  10. Retrofitinterface retrofitinterface = new Restadapter.builder ()
  11. . Setserver (API. Api_url). Build (). Create (Retrofitinterface.   Class);
  12. Fetch user with ID 2048
  13. Retrofitinterface.getuser (2048, new callback<user> () {
  14. @Override
  15. public void Success (user user, Response Response) {
  16. }
  17. @Override
  18. public void failure (Retrofiterror retrofiterror) {
  19. }
  20. });
Retrofit uses Gson by default, so no custom parsing is required, and other converters are supported.

3. Eventbus

Eventbus is a library for simplifying communication between parts of an application. such as sending a message from an activity to a running service, or simply interacting between fragments. The example used below is how to notify an activity if the network connection is lost:

[JS]View Plaincopy
  1. Public class Networkstatereceiver extends Broadcastreceiver {
  2. //Post event If there is no Internet connection
  3. public void OnReceive (context context, Intent Intent) {
  4. super.onreceive (context, intent);
  5. if (intent.getextras () =null) {
  6. Networkinfo ni= (Networkinfo) Intent.getextras (). get (Connectivitymanager.extra_network_info);
  7. if (ni!=null && ni.getstate () ==networkinfo.state.connected) {
  8. //There is Internet connection
  9. } Else if (intent
  10. . Getbooleanextra (Connectivitymanager.extra_no_connectivity,boolean.false)) {
  11. //No Internet connection, send network state changed
  12. Eventbus.getdefault (). Post (new networkstatechanged (false));
  13. }
  14. }
  15. Event
  16. Public class Networkstatechanged {
  17. private misinternetconnected;
  18. Public networkstatechanged (boolean isinternetconnected) {
  19. this.misinternetconnected = isinternetconnected;
  20. }
  21. Public Boolean isinternetconnected () {
  22. return this.misinternetconnected;
  23. }
  24. }
  25. Public class Homeactivity extends Activity {
  26. @Override
  27. protected void OnCreate (Bundle savedinstancestate) {
  28. super.oncreate (savedinstancestate);
  29. Setcontentview (R.layout.activity_main);
  30. Eventbus.getdefault (). Register (this); //Register Eventbus
  31. }
  32. @Override
  33. protected void OnDestroy () {
  34. Super.ondestroy ();
  35. Eventbus.getdefault (). Unregister (this); //unregister Eventbus
  36. }
  37. //method that would be called if someone posts an event networkstatechanged
  38. public void Oneventmainthread (Networkstatechanged event) {
  39. if (!event.isinternetconnected ()) {
  40. Toast.maketext (This, "No Internet connection!", Toast.length_short). Show ();
  41. }
  42. }
  43. }
4. Activeandroid

Activeandroid is a lightweight ORM (Object Relational mapping) that allows you to save and retrieve SQLite database records without having to write a separate SQL statement. Each database record is wrapped neatly into a class of methods, such as Delete () and save ().

Objects that extend the Activeandroid model can be stored in a database, such as:

[JS]View Plaincopy
    1. User.save ();
You can easily replace large SQL statements:

[JS]View Plaincopy
    1. INSERT into Users (nickname, Name, Address, City, PostalCode, country) VALUES (' Batman ',' Bruce W ',' Palisades 2  1 ',' Gotham ',' 40000 ',' USA ');
Get an example of all users:

[JS]View Plaincopy
    1. list<user> users = new Select (). from (User.   Class). Execute ();
and the corresponding SQL statement is this:

[JS]View Plaincopy
    1. SELECT nickname, Name, Address, City, PostalCode, country from Users;

Activeandroid is a great way to remove a lot of boilerplate code that works with databases. There are, of course, other open source solutions, such as Greendao and Ormlite.

5. UNIVERSAL IMAGE LOADER

UIL is an open source project designed to provide a reusable instrument for loading, caching, and displaying asynchronous images. It's very simple to use:

[JS]View Plaincopy
    1. Imageloader.displayimage (Imageuri, ImageView);

Although Picasso has a better API, it lacks customization. With the UIL Builder, you can configure almost all (the most important of which is that Picasso fails when large images are crawled and cached).

A good open source library will make your development easier and faster, while popular libraries are usually well tested and easy to use. In most cases, you can easily import them from Maven into your Android studio project. Add them to the Build.gradle file for the dependency. And after synchronization, you will be able to implement them well in your application.

5 Open source libraries that Android developers must know

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.