Detailed explanation of Android network request framework retrofit

Introduction:

Retro fit is a framework for Android network requests developed by square. The bottom layer of retro fit 2 is implemented based on okhttp. Okhttp has been officially recognized by Google, and a large number of apps use okhttp for network requests. This article uses retrofit version 2.0.0 for example demonstration.

Retrofit can be used for get, post, put, delete and other request methods.

Synchronization request: it needs to be completed in the child thread, which will block the main thread.

Response response = call.execute().body();

Asynchronous request: the request result is called back in the main thread, and the UI can be updated in the onresponse() callback method.

call.enqueue(Callback callback)

Use steps:

(1) Create a project and add jars:

(2) Create a service request interface. The specific code is as follows

Explanation:

@The get annotation indicates the get request and @ query indicates the request parameters, which will be spliced after the URL in the form of key = value (@ query annotation parameter name is key and the value passed in the call is value)

@The post annotation indicates a post request. The @ formurlencoded annotation will automatically set the type of the request parameter to application / x-www-form-urlencoded. The @ formurlencoded annotation cannot be used for get requests@ The field annotation stores every request parameter in the request body. You can also add the encoded parameter, which is of boolean type. The specific usage is: @ field (value = "password", encoded = true) string PWD. If the encoded parameter is true, the key value pair will be encoded, that is, Chinese and special characters will be encoded and converted

(3) Create a retrofit object

Explanation:

The baseurl () method formulates the fixed absolute address of the network request, generally including the request protocol (such as HTTP), domain name or IP address, and port number. When creating a retrofit instance, if addconverterfactory (gsonconverterfactory. Create()) is not configured, the JSON string will be recalled, and the entity object will be recalled if configured.

Supported JSON parsing libraries:

Gson: compile ‘com.squareup.retrofit2:converter-gson:2.0.1' Jackson: compile ‘com.squareup.retrofit2:converter-jackson:2.0.1' Moshi: compile ‘com.squareup.retrofit2:converter-moshi:2.0.1' Protobuf: compile ‘com.squareup.retrofit2:converter-protobuf:2.0.1' Wire: compile ‘com.squareup.retrofit2:converter-wire:2.0.1' Simple XML: compile ‘ com.squareup.retrofit2:converter-simplexml:2.0.1' Scalars (primitives,@R_121_ 2419@ed ,and String): compile ‘com.squareup.retrofit2:converter-scalars:2.0.1'

(4) Call the request method and get the call instance

(5) Use the call instance to complete synchronous or asynchronous requests

Request method:

(1) Get request:

Get request returns JSON string:

Entity object returned by get request:

(2) Post send form:

Results received by the server:

(3) File upload:

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
分享
二维码
< <上一篇
下一篇>>