当前位置:网站首页>Retrofit easy to use
Retrofit easy to use
2022-06-13 02:40:00 【And white】
rely on
https://square.github.io/retrofit/
https://github.com/square/retrofit
//retrofit
implementation'com.squareup.retrofit2:retrofit:2.9.0'
implementation 'com.squareup.okhttp3:okhttp:3.14.9'
implementation'com.squareup.retrofit2:converter-gson:2.9.0'
Easy to use
Interface
@GET("url")
fun getEvent(@Query("id") info:String):Call<EventBean>
To request
var retrofit = Retrofit.Builder()
.addConverterFactory(GsonConverterFactory.create())
.baseUrl(Constant.iP)
.client(client)
.build()
.create(RetrofitInterface::class.java);
retrofit.enqueue(object:retrofit2.Callback<EventBean>{
override fun onFailure(call: Call<EventBean>, t: Throwable) {
LogUtil.e(t.message)
}
override fun onResponse(call: Call<EventBean>, response: Response<EventBean>) {
LogUtil.e(response.body()?.data?.objs.toString())
response.body()?.data?.objs?.let {
adapter.addData(it) }
}
})
add rxjava Use
//Rxjava Remember in gradle Inside plus
implementation 'com.squareup.retrofit2:adapter-rxjava2:2.3.0'
implementation 'io.reactivex.rxjava2:rxjava:2.1.6'
implementation 'io.reactivex.rxjava2:rxandroid:2.0.1'
Interface
@GET("url")
fun getReportedEvent(@Query("id") info:String):Observable<EventBean>// Be careful not to make mistakes in the guide bag
To request
// Here you can extract static global objects
var retrofit = Retrofit.Builder()
.addConverterFactory(GsonConverterFactory.create())
.addCallAdapterFactory(RxJava2CallAdapterFactory.create())/// use RxJava
.baseUrl(Constant.iP)
.build()
.create(RetrofitInterface::class.java);
retrofit.subscribeOn(Schedulers.io())// Specify the network request at io In the background thread
.observeOn(AndroidSchedulers.mainThread())// Appoint observer The callback is UI In the main thread
.subscribe(object : Observer<EventBean> {
override fun onSubscribe(d: Disposable) {
}
override fun onNext(t: EventBean) {
LogUtil.e(Gson().toJson(t!!))
t?.data.objs?.let {
adapter.addData(it) }
}
override fun onError(e: Throwable) {
}
override fun onComplete() {
}
})
Add global request parameters
class CommonInterceptor(
private val paramKey: String,
private val paramValue: String, private val token: String
) : Interceptor {
override fun intercept(chain: Interceptor.Chain): Response {
val orderRequest = chain.request()
val addQueryParameter = orderRequest.url().newBuilder()
.scheme(orderRequest.url().scheme())
.host(orderRequest.url().host())
.addQueryParameter(paramKey, paramValue)
val trueBuild = orderRequest.newBuilder()
trueBuild.header("Authorization", "Bearer " + token)
trueBuild.method(orderRequest.method(), orderRequest.body())
trueBuild.url(addQueryParameter.build())
trueBuild.build()
return chain.proceed(trueBuild.build())
}
}
// I added a global request parameter
val userInfo = getUserInfo(getMyBaseContext())
var commonInterceptor: CommonInterceptor? = null;
if (userInfo != null) {
commonInterceptor = CommonInterceptor("partyId", ""+userInfo.orgParty, userInfo.token)
}
val builder = OkHttpClient.Builder()
if (commonInterceptor != null) {
builder.addInterceptor(commonInterceptor)
}
val client = builder.build()
Retrofit.Builder()
.addConverterFactory(GsonConverterFactory.create())
.baseUrl(Constant.iP)
.client(client)
.build()
.create(RetrofitInterface::class.java);
边栏推荐
- Priority queue with dynamically changing priority
- Leetcode 926. Flip string to monotonically increasing [prefix and]
- Leetcode 450. 删除二叉搜索树中的节点 [二叉搜索树]
- Leetcode 473. 火柴拼正方形 [暴力+剪枝]
- Leetcode 926. 将字符串翻转到单调递增 [前缀和]
- The precision of C language printf output floating point numbers
- 01 初识微信小程序
- Flow chart of interrupt process
- Redirection setting parameters -redirectattributes
- Leetcode 450. Delete node in binary search tree [binary search tree]
猜你喜欢
![[reading point paper] yolo9000:better, faster, stronger, (yolov2), integrating various methods to improve the idea of map and wordtree data fusion](/img/b7/0171a637d00e979fb92b82d0c038be.jpg)
[reading point paper] yolo9000:better, faster, stronger, (yolov2), integrating various methods to improve the idea of map and wordtree data fusion

04路由跳转并携带参数

An image is word 16x16 words: transformers for image recognition at scale

Introduction and download of common data sets for in-depth learning (with network disk link)

Detailed explanation of data processing in machine learning (I) -- missing value processing (complete code attached)

02 optimize the default structure of wechat developer tools
![[data analysis and visualization] key points of data drawing 4- problems of pie chart](/img/e1/618ff53b33b4b1de6acf4942130c17.jpg)
[data analysis and visualization] key points of data drawing 4- problems of pie chart

Understand speech denoising
![PCR validation of basic biological experiments in [life sciences]](/img/92/1cecb7cb4728937bd18b336ba4e606.jpg)
PCR validation of basic biological experiments in [life sciences]
![[reading papers] dcgan, the combination of generating countermeasure network and deep convolution](/img/31/8c225627177169f1a3d6c48fd7e97e.jpg)
[reading papers] dcgan, the combination of generating countermeasure network and deep convolution
随机推荐
Detailed explanation of data processing in machine learning (I) -- missing value processing (complete code attached)
js 解构赋值
How to learn to understand Matplotlib instead of simple code reuse
Opencvsharp4 pixel read / write and memory structure of color image and gray image
Understand HMM
Priority queue with dynamically changing priority
Detailed explanation of handwritten numeral recognition based on support vector machine (Matlab GUI code, providing handwriting pad)
vant实现移动端的适配
[data analysis and visualization] key points of data drawing 5- the problem of error line
redis 多个服务器共用一个
Paper reading - beat tracking by dynamic programming
[common tools] pyautogui tutorial
01 初识微信小程序
如何挑选基金产品?什么样的基金是好基金?
[data analysis and visualization] key points of data drawing 4- problems of pie chart
OpenCVSharpSample04WinForms
Special topic I of mathematical physics of the sprint strong foundation program
AutoX. JS invitation code
Laravel 权限导出
Useful websites for writing papers and studying at ordinary times