问题描述
因此,我正在尝试为Http请求集成Firebase性能,并在它们显示此处(步骤9).
So I'm trying to integrate Firebase performance for Http requests and add them manually as they show here (step 9).
我正在使用Retrofit 2和RxJava 2,所以我想到了一个自定义运算符,请检查以下代码:
I'm using Retrofit 2 and RxJava 2, so I had the idea of doing a custom operator, check code below:
Retrofit 2客户端
@GET("branch-{environment}/v2/branches")
fun getBranch(@Path("environment") environment: String, @Query("location") location: String, @Query("fulfilment_type") fulfilmentType: String): Single<Response<GetBranchResponse>>
RxJava调用到改造客户端
private val client: BranchClient = clientFactory.create(urlProvider.apiUrl)
override fun getBranch(postCode: String, fulfilmentType: FulfilmentType): Single<GetBranchResponse> {
return client
.getBranch(environment, postCode.toUpperCase(), fulfilmentType.toString())
.lift(RxHttpPerformanceSingleOperator(URL?, METHOD?))
.map { it.body() }
.subscribeIO() //custom Kotlin extension
.observeMain() //custom Kotlin extension
...
}
通过提升操作的RxJava 2 Custom Operator:
class RxHttpPerformanceSingleOperator<T>(private val url: String, private val method: String) : SingleOperator<Response<T>, Response<T>> {
private lateinit var metric: HttpMetric
@Throws(Exception::class)
override fun apply(observer: SingleObserver<in Response<T>>): SingleObserver<in Response<T>> {
return object : SingleObserver<Response<T>> {
override fun onSubscribe(d: Disposable) {
metric = FirebasePerformance.getInstance().newHttpMetric(url,
method.toUpperCase())
metric.start()
observer.onSubscribe(d)
}
override fun onSuccess(t: Response<T>) {
observer.onSuccess(t)
//More info: https://firebase.google.com/docs/perf-mon/get-started-android
metric.setRequestPayloadSize(t.raw().body().contentLength())
metric.setHttpResponseCode(t.code())
metric.stop()
}
override fun onError(e: Throwable) {
observer.onError(e)
metric.stop()
}
}
}
因此,目前我不确定如何正确地获取请求的URL和方法(标记为URL?和METHOD?)发送给运营商,
So currently I'm not sure how it the proper way to get the URL and METHOD of the request (marked as URL? and METHOD? ) to send to the operator,
我需要onSubscribe上的它们才能启动指标 ..并且我没有响应...
I need them on onSubscribe to start the metric.. and there I don't have the response with it...
目前UGLYYYYYYYY我的操作方法是:
添加到翻新客户端:
@GET("branch-{environment}/v2/branches")
fun getBranchURL(@Path("environment") environment: String, @Query("location") location: String, @Query("fulfilment_type") fulfilmentType: String): Call<JsonObject>
添加将参数添加为:
val request = client.getBranchURL(environment, postCode.toUpperCase(), fulfilmentType.toString()).request()
url = request.url().toString()
method = request.method()
这使我在每个请求的客户端上都有2个条目...这是没有道理的.
This makes me have 2 entries on the Client for each request... which makes no sense.
一些有用的线索:-如何获取请求网址用rxjava改造2.0?
Some helpful clues along the way:- How to get the request url in retrofit 2.0 with rxjava?
推荐答案
使用FirebaseInstance
向HttpClient.Builder
添加改造Interceptor
,并在其中生成HttpMetrics
:
Add a Retrofit Interceptor
to your HttpClient.Builder
with the FirebaseInstance
and generate your HttpMetrics
there:
class FirebasePerformanceInterceptor(val performanceInstance: FirebasePerformance) : Interceptor {
override fun intercept(chain: Interceptor.Chain): Response {
val request = chain.request()
//Get request values
val url = request.url().url()
val requestPayloadSize = request.body()?.contentLength() ?: 0L
val httpMethod = request.method()
//Initialize http trace
val trace = performanceInstance.newHttpMetric(url, httpMethod)
trace.setRequestPayloadSize(requestPayloadSize)
trace.start()
//Proceed
val response = chain.proceed(chain.request())
//Get response values
val responseCode = response.code()
val responsePayloadSize = response.body()?.contentLength() ?: 0L
//Add response values to trace and close it
trace.setHttpResponseCode(responseCode)
trace.setResponsePayloadSize(responsePayloadSize)
trace.stop()
return response
}
}
您可以直接复制并粘贴此代码,它将起作用.享受吧!
You can directly copy and paste this code and it will work.Enjoy!
这篇关于如何在RxJava 2自定义运算符的onSubscribe上获取RetroFit请求的URL和方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!