5 Open source libraries that Android developers must know

Source: Internet
Author: User

Abstract: 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.

Serialize String Userjson = new Gson (). ToJson (user);//Deserializeuser 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.

Public interface Retrofitinterface {    //asynchronously with a callback    @GET ("/api/user")    user GetUser (@ Query ("user_id") int userId, callback<user> Callback);    synchronously    @POST ("/api/user/register")    user RegisterUser (@Body user user);} Exampleretrofitinterface retrofitinterface = new Restadapter.builder ()            . Setserver (API. Api_url). Build (). Create (Retrofitinterface.class);//fetch user with ID 2048retrofitinterface.getuser (2048, new Callback<user> () {    @Override public    void Success (user user, Response Response) {    }    @Override Public    void Failure (Retrofiterror retrofiterror) {    }});
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:

public class Networkstatereceiver extends Broadcastreceiver {//Post event If there is no Internet connection Publi        c void OnReceive (context context, Intent Intent) {super.onreceive (context, Intent); if (Intent.getextras ()!=null) {Networkinfo ni= (networkinfo) Intent.getextras (). Get (Connectivitymanager.extra_ne            Twork_info);            if (Ni!=null && ni.getstate () ==networkinfo.state.connected) {//There is Internet connection                } else if (intent. Getbooleanextra (Connectivitymanager.extra_no_connectivity,boolean.false)) { No Internet connection, send network state changed Eventbus.getdefault (). Post (New Networkstatecha            Nged (false));    }}//Eventpublic class Networkstatechanged {private misinternetconnected;    Public Networkstatechanged (Boolean isinternetconnected) {this.misinternetconnected = isinternetconnected; } public boolean isinternetconnected () {       return this.misinternetconnected;        }}public class Homeactivity extends Activity {@Override protected void onCreate (Bundle savedinstancestate) {        Super.oncreate (savedinstancestate);        Setcontentview (R.layout.activity_main); Eventbus.getdefault (). Register (this);        Register Eventbus} @Override protected void OnDestroy () {Super.ondestroy (); Eventbus.getdefault (). Unregister (this); Unregister Eventbus}//method that would be called if someone posts an event networkstatechanged public voi D Oneventmainthread (networkstatechanged event) {if (!event.isinternetconnected ()) {Toast.maketext (this        , "No Internet connection!", Toast.length_short). Show (); }    }}
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:

User.save ();
You can easily replace large SQL statements:

INSERT into Users (nickname, Name, Address, City, PostalCode, country) VALUES (' Batman ', ' Bruce W ', ' Palisades ', ' Gotham ', ' 40000 ', ' USA ');
Get an example of all users:

list<user> users = new Select (). from (User.class). Execute ();
and the corresponding SQL statement is this:

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:

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.

dependencies {    compile ' com.google.code.gson:gson:2.2.4 '    compile ' com.squareup.okhttp:okhttp:1.3.0 '    Compile ' com.squareup.retrofit:retrofit:1.3.0 '    compile ' de.greenrobot:eventbus:2.2.+ '    compile ' com.nostra13.universalimageloader:universal-image-loader:1.9.1 '}   

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.