Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

OkHttp 4.x giving error when trying to access url via Call request

I recently started update to okhttp3 to 4.x

In doing so I am getting following build time error:

Using 'url(): HttpUrl' is an error. moved to val

The issue is happening when I am trying to get url from request object obtained via call:

e.g

   call.enque(callback : Callback){
     override fun onFailure(call : Call, t:Throwable) {
       val url = call.request().url().toString
     }
   }

I looked up further and the Url object within Request is val aka final now.

Also, no directions on their upgrade guide https://square.github.io/okhttp/upgrading_to_okhttp_4/

I would appreciate for any suggestions on another way to obtain Url.

like image 397
Akshay Avatar asked Oct 23 '25 21:10

Akshay


1 Answers

Using 'url(): HttpUrl' is an error. moved to val

This means you should change the function call url() to a property access url.

okhttp 4 comes with replaceWith param in the deprecation annotation that makes e.g. Android Studio to offer that fix automatically with right-click/alt-enter on the error:

@Deprecated(
  message = "moved to val",
  replaceWith = ReplaceWith(expression = "url"),
  level = DeprecationLevel.ERROR)

From comments:

Upon further investigation I found out that the request() or Request object is from Retrofit 2. And Retrofit 2 returns call object from okhttp3

That's an issue with Android Studio. You can work around it with explicit cast to okhttp 4 types, e.g. (call.request() as Request).url.

like image 107
laalto Avatar answered Oct 26 '25 11:10

laalto



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!