Learning Retrofit_ Persistent Updates

Source: Internet
Author: User

1, Homepage:

Https://github.com/square/retrofit


Note: The premise of using retrofit is * * Server-side code follows the REST specification!!!!! **java better be 1.7 or above, Android2.3 above, square company, and okhttp Open Source Library.

2, Function:

* Very high efficiency

* You can convert the results directly to the Java class

* Mainly used in conjunction with Rxjava

Note: If you add a okhttp when adding dependencies, the underlying application is that Okhttp extensibility is very powerful.

3, configuration:

* Add Retrofit dependency: compile ' com.squareup.retrofit2:retrofit:2.0.2 '

* Add data resolution dependencies, choose according to the actual situation

* gson:compile ' com.squareup.retrofit2:converter-gson:2.0.2 ' < is usually the JSON that is returned, the most commonly used is to add top and bottom >

* jackson:com.squareup.retrofit2:converter-jackson:2.0.2

* moshi:com.squareup.retrofit2:converter-moshi:2.0.2

* protobuf:com.squareup.retrofit2:converter-protobuf:2.0.2

* wire:com.squareup.retrofit2:converter-wire:2.0.2

* Simple xml:com.squareup.retrofit2:converter-simplexml:2.0.2

4, use the steps:

1. Using [http://www.jsonschema2pojo.org/] (http://www.jsonschema2pojo.org/) to create a data model, which is not used in JavaBean as Gsonformat, There is a note for the others.

2. Create rest API Interface < Interface definition A lot of understanding, you can use it later when you look at the right >

5, common annotations:

* Request Method : @GET/@POST/@PUT/@DELETE/@HEAD

* URL processing <url address split >

* @Path-Replacement parameters

@GET ("/group/{id}/users")

Public call<list<user>> grouplist (@Path ("id") int groupId); The top is annotated with {}.

* @Query-Add query Parameters

@GET ("/group/{id}/users")

Public call<list<user>> grouplist (@Path ("id") int groupId, @Query ("sort") String sort);

* @QueryMap-If you have multiple query parameters, place them in map

@GET ("/group/{id}/users")

Public call<list<user>> grouplist (@Path ("id") int groupId, @QueryMap map<string, string> options);

* Sample code:

Public interface Netapi {

@GET ("/users/{user}")

Public call<gitmodel> Getfeed (@Path ("user") String user);

@GET ("/service/getipinfo.php")

Public call<ipmodel> GetWeather (@Query ("City"), String City);

}

Pay attention to the stitching method.

6. Create the retrofit object and initiate the request. Sample code fixed format ():

Building Retrofit Instances

Retrofit Retrofit = new Retrofit.builder ().

BASEURL (API2).

Addconverterfactory (Gsonconverterfactory.create ()).

Build ();


Implementation classes for building interfaces

Ipapi Weatherapi = retrofit.create (Ipapi.class);


Invoke method of interface definition

call<ipmodel> Weathercall = Weatherapi.getweather ("8.8.8.8");


Execute requests asynchronously

Weathercall.enqueue (New callback<ipmodel> () {

@Override

public void Onresponse (call<ipmodel> call, response<ipmodel> Response) {

Ipmodel model = Response.body ();

System.out.println ("Country:" + model.getdata (). Getcountry ());

}

@Override

public void OnFailure (call<ipmodel> call, Throwable t) {

System.out.println (T.tostring ());

}

});

7, Advantages:

Summary: The main difficulty is the API writing and request the three steps

8, Project Source address: Https://github.com/zzggxx/RetrofitDemo

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.