【问题标题】:How to get an original address from retrofit?如何从改造中获得原始地址?
【发布时间】:2019-06-19 01:58:21
【问题描述】:

我收到了来自我服务器上“/address/{id}/data”链接的改造请求的响应,其中 id - 查询参数。当我得到改造的响应时,我已经粘贴了参数“/address/001/data”的链接。如何在 Retrofit (/address/{id}/data) 中获取原始链接而不进行查询?

【问题讨论】:

标签: android retrofit retrofit2


【解决方案1】:

你能做什么

您可能还有其他一些选择,但您可以从这里开始

选项 1

您可以直接从改造返回的响应中访问 url 使用

response.raw().request().url()

这将返回请求的 http url,例如。打印出来

okhttp/example-request D: //http://example.com/address/001/data

选项 2

另一个是在okhttp-logging-inceptor的帮助下添加网络请求拦截器。

val logging = HttpLoggingInterceptor()
logging.level = Level.BASIC
val client = OkHttpClient.Builder()
    .addInterceptor(logging)
    .build()

选项 2 比选项 1 好得多,因为它将拦截应用程序中的所有网络请求,而无需对每个请求回调重新做太多工作。此外,您还可以选择自定义它的日志记录级别并仅在指定条件下添加它。

例如。默认登录DEBUG模式

val logging = HttpLoggingInterceptor(HttpLoggingInterceptor.Logger.DEFAULT)
val client = OkHttpClient.Builder()
if (BuildConfig.DEBUG) {
    logging.level = HttpLoggingInterceptor.Level.BODY // setting the level
    client.addInterceptor(okHttpLoggingInterceptor) // adding interceptor
}

参考文献

【讨论】:

  • 嗨 Strangelove,这个答案对你有帮助吗?
猜你喜欢
  • 2018-02-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-25
  • 2021-12-08
  • 2018-07-02
  • 1970-01-01
相关资源
最近更新 更多