Retrofit is one of the most Popular network request libraries for Android Web requests developed by the Square company.
http://square.github.io/retrofit/
Https://github.com/square/retrofit
The use needs to be introduced:
Compile ' com.squareup.retrofit2:retrofit:2.1.0 ' Compile ' com.squareup.retrofit2:adapter-rxjava:2.1.0 ' Compile ' io.reactivex:rxandroid:1.0.1 ' Compile ' com.squareup.retrofit2:converter-gson:2.1.0 '
Retrofit contains a variety of network request methods, which can be selected according to the reference.
Full path
@GET (url+ "? platform=android&appkey=5a379b5eed8aaae531df5f60b12100cfb6dff2c1&c=member&a= Getdepartments " Call
@Query ("key") String key for the interface key-value pair type parameter
@GET ("app.php") call<userinfo> GetInfo2 (@Query ("platform") String platform, @Query ( "Appkey") string Appkey, @Query ("C") string IP);
@QueryMap If the query parameter is more, then all the parameters can be integrated in a map by @querymap method
@GET ("app.php")call<userinfo> getinfomap (@QueryMap map<string,string> Map);
Note: The same effect is the same as the above get changed to post
@Field ("key") String key for the interface key-value pair type parameter
@FormUrlEncoded @POST ("app.php")call<userinfo> Postfield (@Field ("platform") String BookId, @Field ( "Appkey") string title, @Field ("C") string content);
@FieldMap If the field parameter is more, then all the parameters can be integrated in a map by @fieldmap method
@FormUrlEncoded @post ("app.php")call<userinfo> postinfomap (@FieldMap map< string,string> map);
There are certain requirements for new URL definitions in Retrofit, with different versions and sometimes formats, most of which are more than 2.0, @GET/post () suggested formats do not start with/begin with, such as:
@POST ("/app.php")
Instead: URL at/end of: (Note: Full path is not affected)
http://www.baidu.com/
Code:
Define interface Irequestservice . java
@GET (url+ "app.php?platform=android&appkey=5a379b5eed8aaae531df5f60b12100cfb6dff2c1&c=member&a= Getdepartments " ) Call<UserInfo>getInfo1 (); /*@Query ("Apikey") String Apikey for the interface key-value pair type parameter*/@GET ("app.php" ) Call<UserInfo> GetInfo2 (@Query ("platform") string platform, @Query ("Appkey") string Appkey, @Query ("C") string IP , @Query ("a") String tag); /*@QueryMap If the query parameter is more, then all the parameters can be integrated in a map by @querymap method*/@GET ("app.php" ) Call<UserInfo> getinfomap (@QueryMap map<string,string> Map);
Use
Retrofit Retrofit =NewRetrofit.builder (). BASEURL (URLs). Addconverterfactory (Gsonconverterfactory.create ()). build (); I RequestService Service= Retrofit.create (Irequestservice.class); Pager<DepartmentInfo> call=service.getinfo1 (); Call.enqueue (NewCallback<departmentinfo>() {@Override Public voidOnresponse (call<departmentinfo> call, response<departmentinfo> Response) {departmentinfo info=Response.body (); Mtextview.settext (Info.geterror ()+ "-----" +info.getdata (). Get (0). toString ()); Toast.maketext (Getapplicationcontext (), Info.geterror (), Toast.length_long). Show (); LOG.I ("Tag", Info.getdata (). Get (0). Getdepartname ()); LOG.I ("Tag", Info.getdata (). Get (1). Getdepartname ()); } @Override Public voidOnFailure (call<departmentinfo> call, Throwable t) {}});
Retrofit + Rxjava combined use
Similar to the above, except that it is no longer a call object but a observable object, and an event source in Rxjava.
@GET (url+ "app.php?platform=android&appkey=5a379b5eed8aaae531df5f60b12100cfb6dff2c1&c=member&a= Getdepartments ")Observable<UserInfo> Getrxjava (); @POST ( "app.php" )Observable<UserInfo> getuserfollowingobservable (@QueryMap map< string,string> map);
Use
Rxjavacalladapterfactory Rxadapter =Rxjavacalladapterfactory.createwithscheduler (Schedulers.io ()); Retrofit retrofit11=NewRetrofit.builder (). BASEURL (URLs). Addconverterfactory (Gsonconverterfactory.create ()). Addcalladapterfactory ( Rxadapter). Build (); Irxjavaservice Apiservice= Retrofit11.create (Irxjavaservice.class); Observable<DepartmentInfo> call =Apiservice.getuser (Util.showmap ()); subscription=Call . Subscribeon (Schedulers.io ()). Observeon (Androidschedulers.mainthread ()). Subscribe (NewSubscriber<departmentinfo>() {@Override Public voidoncompleted () {} @Override Public voidOnError (Throwable e) {} @Override Public voidonNext (departmentinfo user) {Toast.maketext (Getapplicationcontext (), User.getdata (). Get (0). Getdepartname (), Toast.length_short). Show (); } });Summarize:
Retrofit the use and understanding of the simple comb over, hoping to help you better understand the use of retrofit.
SOURCE Click to download
The use of Android development Retrofit+rxjava