当前位置:网站首页>[Third Party framework] retrofit2 (1) of network request framework -- Getting Started Guide
[Third Party framework] retrofit2 (1) of network request framework -- Getting Started Guide
2022-06-25 16:00:00 【Favorite grapes】
List of articles
Retrofit2 Use steps to summarize
- stay
build.gradleI quoteRetrofitlibrary - Definition Interface ApiService, Inside, the configuration information of a request is declared through annotations or parameters
- establish Retrofit object , Set it up
CallAdapterAndConverter - adopt Retrofit object Of
createMethod , Create an instance of the interfaceapiService - Method of calling interface instance , Initiate request .
1、 stay build.gradle I quote Retrofit library
// app-module
// Retrofit library
implementation "com.squareup.retrofit2:retrofit:2.9.0"
// gson-Converter
implementation "com.squareup.retrofit2:converter-gson:2.9.0"
// If the Library Download fails due to network reasons , You can use alicloud maven Mirror image , At the project level build.gradle Add the following
repositories {
google()
......
maven { url 'https://maven.aliyun.com/repository/jcenter' }
}
After adding dependency , Click on sync Take the Kula to the local .
2、 Definition ApiService
stay Retrofit in , You need to use the interface (interface) To define each request method .
PS: A name can be any name , Here the author adopts ApiSerivce, You can differentiate according to business needs , such as ApiDetails、ApiUser wait .
public interface ApiService {
@GET("users/{user}/repos") // Define the request method ,{user} Will be replaced by the parameter value passed in by the method
Call<List<Repo>> listRepos(@Path("user") String user);
}
3、 establish Retrofit object 、 establish API example , Initiate request
Because in the project , Basically a Retroft The object can , So here we use the singleton method to create Retrofit object , Of course , If there is a business need to create multiple Retrofit Objects can be , For example, when there are multiple BaseURL When
PS: BaseURL It can be roughly understood as domain name , Most of the App They only visit their own domain names , So there's only one BaseUrl, But it does not rule out the need for multiple domain names , Different Api Under different domain names .
Be careful ,BaseURL Must be / It's the end , Otherwise, it will report a mistake .
public class ApiServiceManager {
private volatile static ApiServiceManager apiServiceManger = null;
private Retrofit retrofit;
private ApiService apiService;
private ApiServiceManager() {
//⒈ establish Retrofit example
retrofit = new Retrofit.Builder()
.baseUrl("https://api.github.com/")
// take Json The result is converted to the corresponding object
.addConverterFactory(GsonConverterFactory.create())
.build();
// 2. establish ApiService example
apiService = retrofit.create(ApiService.class);
}
public void listRepos(Callback<List<Repo>> callback) {
Call<List<Repo>> repos = apiService.listRepos("octocat");
// 3. Initiate request , Two ways :execute, Don't switch threads , Execute on current thread .
// enqueue, Switching thread , The background to perform .
// Execute the network request in the current thread
// Response<List<Repo>> result = repos.execute();
// Out of step request , Generally, the following methods are used
repos.enqueue(callback);
}
}
边栏推荐
- Interviewer: your resume says you are proficient in mysql, so you say cluster / Union / overlay index, table return, index push down
- TFIDF and BM25
- Problems caused by using ApplicationContext to render layout
- 揭秘GaussDB(for Redis):全面对比Codis
- Geographic location data storage scheme - redis Geo
- Golang open source streaming media audio and video network transmission service -lal
- Continuously improve the overall performance of adaoracle Oracle Oracle
- How to convert a recorded DOM to a video file
- Analysis of the concept of metacosmic system
- 基于神经标签搜索,中科院&微软亚研零样本多语言抽取式摘要入选ACL 2022
猜你喜欢

VectorDraw Developer Framework 10.1001 Crack

Free books! AI across the Internet paints old photos. Here is a detailed tutorial!

揭秘GaussDB(for Redis):全面对比Codis

Based on neural tag search, the multilingual abstracts of zero samples of Chinese Academy of Sciences and Microsoft Asiatic research were selected into ACL 2022

商城风格也可以很多变,DIY了解一下!
What is session? How is it different from cookies?
How to convert a recorded DOM to a video file

The style of the mall can also change a lot. DIY can learn about it!

Optimization of lazyagg query rewriting in parsing data warehouse

合宙Air32F103CBT6開發板上手報告
随机推荐
合宙Air32F103CBT6開發板上手報告
NFT元宇宙发展能做什么?
B站付费视频使up主掉粉过万
[golang] leetcode intermediate - find the first and last position of an element in a sorted array & Merge interval
[issue 24] one year experience of golang to develop futu
Multithreading, parallelism, concurrency, thread safety
Golang open source streaming media audio and video network transmission service -lal
有哪些新手程序员不知道的小技巧?
Mapbox map - inconsistent coordinate system when docking GIS layers?
Traversal and branch judgment of JS (case on June 24, 2022)
leetcode-8. 字符串转换整数 (atoi)
10款超牛Vim插件,爱不释手了
不要小看了积分商城,它的作用可以很大!
QC, QA, IPQC, JQE, DQA, SQE, DQC, MQC, IQC, FQC, OQC
什么是NFT数字藏品?
Client development (electron) system level API usage
Client development (electron) data store
Consumer and producer cases of inter thread synchronization (condition variable)
Webgl and webgpu comparison [4] - uniform
将一个文件写入到另一个文件的标记位置