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
- Serialize
- String Userjson = new Gson (). ToJson (user);
- Deserialize
- 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
- 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);
- }
- Example
- Retrofitinterface retrofitinterface = new Restadapter.builder ()
- . Setserver (API. Api_url). Build (). Create (Retrofitinterface. Class);
- Fetch user with ID 2048
- Retrofitinterface.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:
[JS]View Plaincopy
- Public class Networkstatereceiver extends Broadcastreceiver {
- //Post event If there is no Internet connection
- public void OnReceive (context context, Intent Intent) {
- super.onreceive (context, intent);
- if (intent.getextras () =null) {
- Networkinfo ni= (Networkinfo) Intent.getextras (). get (Connectivitymanager.extra_network_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 networkstatechanged (false));
- }
- }
- Event
- Public 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 void 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:
[JS]View Plaincopy
- User.save ();
You can easily replace large SQL statements:
[JS]View Plaincopy
- 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
- list<user> users = new Select (). from (User. Class). Execute ();
and the corresponding SQL statement is this:
[JS]View Plaincopy
- 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
- 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