Detailed explanation of Android retro fit

I'm late to learn about retrofit, which is now a very popular network request framework for Android. I haven't learned about retrofit before, but recently, the company's new project uses retrofit, rxjava and okhttp for encapsulation, which is very simple to use, increases the beauty of the code and reduces the coupling. This is a great framework, especially when the three are used together.

brief introduction

Retrofit is a framework developed by square for Android network requests. It has been updated to version 2.3. The biggest feature of retrofit is the use of runtime annotations to provide functionality.

Use of retrofit

In fact, the use of retrofit is still very simple, and the logical idea is relatively clear, so it is easy for developers to get started.

Add dependency

Add the following dependencies in the build.gradle file under dependencies:

In addition to the retrofit dependency, the gson dependency is also added to parse the JSON data obtained from the request.

Define the data set used

This is the set of returned results, written using kotlin

Required content collection class

request

You need an interface and write the requested method

Call request

The above is the get request method of retrofit. For a get request, you need to add a get annotation before the request method to indicate that it is a get request. Similarly, if it is a post request, you need a post annotation.

@POST

For a post request, you need to add a @ post annotation and use the @ formurlencoded annotation to indicate that this is a form. Use the @ field annotation to pass in the parameters required by the form.

@Body

@The body annotation is a request method for post, such as the JSON format for transmitting data

@Path

Use @ path to dynamically configure the URL address.

@Query

@Query is to dynamically specify query criteria

@Query is a single query condition, but if it is multiple conditions, it is not applicable, so @ querymap annotation is required@ Querymap is to dynamically specify query criteria groups.

File upload

The @ part annotation is used to represent the upload of a single file, while the @ partmap annotation is similar to the upload of a single file, but uses the map set to encapsulate the uploaded file, that is, multi file upload.

In addition to the above comments, there is @ header, which means adding a message header. When HTTP requests, in order to prevent attacks, filter unsafe access and add special encrypted access to ensure security, some special message processing needs to be carried in the message header, and adding a message header can be realized by using @ header in retrofix.

From the above explanation of retrofix practice and annotations, we know that using retrofix can be very simple, and the code logic is relatively clear. It provides a lot of annotations for developers to use, simplifying the code.

The above is the whole content of this article. I hope it will be helpful to your study, and I hope you can support programming tips.

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>