Retrofit 2.0 Use

Source: Internet
Author: User

I was wondering if I could change the network request from my previous project.

Want to pass retrofit refactoring, because retrofit is perfectly compatible with Rxjava back will be used Rxjava

So

Open a hole and write.

As the architecture of online retrofit 2.0 is described in detail. So I'm just here to provide some of the problems and solutions I've encountered.

To use retrofit for network requests first, you must create a request interface class Apiservice

 Public Interface Apiservice {  /*  @GET ("service/getipinfo.php")    call<getipinforesponse> Getipinfo (@Query ("IP") String IP); */     @GET ("service/getipinfo.php")    Observable<GetIpInfoResponse> getipinfo (@ Query ("IP") String IP);}

For the above code he is divided into two parts, in general

The first part @ he uses annotations, @ indicates the network request that you want to use. Like I'm using a GET request, I'm @get, and then I need to take part of the request URL.

Then the question comes, which part of the URL do I want to put. According to my verification, ① you can put a complete request URL address, this situation when you build retrofit instance class below his. BASEURL ("Yoururl") This method inside the URL string will not function;

② you put a part of the address, but make sure to build the retrofit class. BASEURL ("Yoururl") This your URL is connected to the one you put in the above method. They can be a legitimate

The full URL address. Also, you have to meet the address in the. BASEURL () method when you use this URL parameter to the/end of the location!

Ps: For multiple request parameter notation

  

    @POST ("Server?" )    call <GetLoginJson> Login (@Query ("command") String login,                             @Query ("Posmodel") String Posmodel,                             @Query ("Possn") string possn,                             @Query ("Posmac") string Posmac,                             @ Query ("userid") string userId,                             @Query ("PassWord") string PassWord,                             @Query (" Version ") String version};

The second part is the following interface to provide external methods this method is also divided into two ways, the first one is to return the call type callback, we look at the code (note: This call callback method and the above annotated interface corresponding)

  

Retrofit Mretrofit =Newthe Retrofit.builder ()//retrofit uses a typical Builder model. BASEURL ("Http://ip.taobao.com")//Then write to your URL address
. Addconverterfactory (Gsonconverterfactory.create ())//Create a new Gson converter
. build (); Apiservice Apiservice= Retrofit.create (Apiservice.class); Instantiate Apisservice

Pager<GetIpInfoResponse> call = Apiservice.getipinfo ("63.223.108.42"); Calling interface methods

/** the call into the thread queue, then instantiates his callbacl*/
Call.enqueue (NewCallback<getipinforesponse>() {@Override Public voidOnresponse (response<getipinforesponse>response, Retrofit Retrofit) {Getipinforesponse Getipinforesponse=response.body ();//Take the returned data out and assign it back to the receiving class (this is said below) Mtvcontent.settext (getIpInfoResponse.data.country ); } @Override Public voidonfailure (Throwable t) {Mtvcontent.settext (T.getmessage ()); } });

The above code is the complete use of retrofit process, after reading you will find that I have a class is not posted, it is Getipinforesponse

Why do you have this class, this class is to match the successful value returned after successful communication, and the returned data is related to him, and his parameters and notation are determined by the return value. Let's take a look at the code

 public  class  getipinforesponse extends   Baseresponse {
public Ipinfo data;} public class Ipinfo { public String country; public String country_id; public String area; public String area_id; public String IP;}

We can enter the above URL into the URL to see what is returned, http://ip.taobao.com/service/getIpInfo.php, but if yes it should be {"Code": 1, "Data": "No IP param."}, This no data return should be and not be tested on a handheld machine has a relationship

So his return class parameter is the JSON-returned parameter, so the data here corresponds to the return of the key to the database, and the data parameters such as Ipinfo this class, so retrofit use almost so much; (there should be no missing it)

And then here is the case with Rxjava retrofit his use (note: and Rxjava when Apiservice for the second not annotated method, I here lazy two are placed in a file)

Retrofit Retrofit =NewRetrofit.builder (). BASEURL ("Youurl"). Addconverterfactory (Gsonconverte                        Rfactory.create ()). Addcalladapterfactory (Rxjavacalladapterfactory.create ())                . build (); Apiservice Apiservice= Retrofit.create (Apiservice.class); Apiservice.getipinfo ("63.223.108.42"). Subscribeon (schedulers. IO ()). Observeon (Androidschedulers.mainthread ()). Subscribe (NewSubscriber<getipinforesponse>() {@Override Public voidoncompleted () {} @Override Public voidOnError (Throwable e) {mtvcontent.settext (E.getmessage ()); } @Override Public voidOnNext (Getipinforesponse getipinforesponse) {Mtvcontent.settext (getIpInfoResponse.dat                            A.country); }                        });

For the benefits of joint use with Rxjava, it is easy to control the threading switch

Then use retrofit to make the logic clearer, the code more concise, and the Rxjava combined to reduce the number of secret indentation issues

Retrofit 2.0 Use

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.