Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using absolute URLs with Retrofit

I have a HAL API that I'm working with and in many cases I need to send request (with different methods) to a URL that I get back from API. Meaning I don't want to hardcode the path of URL in my retrofit api interface but what I want is just sending a simple request using retrofit to that URL. I'm using Volley now and I know that I can use OkHttp for this purpose but I was wondering if there is a nice way of doing such thing in Retrofit?

like image 601
Kayvan N Avatar asked Jan 23 '15 18:01

Kayvan N


People also ask

When to use a dynamic URL in retrofit?

There are cases when we need to use a dynamic URL in our application during runtime. Version 2 of the Retrofit library introduced the @Url annotation that allows us to pass a complete URL for an endpoint:

What is @ url annotation in retrofit?

@Url Annotation There are cases when we need to use a dynamic URL in our application during runtime. Version 2 of the Retrofit library introduced the @Url annotation that allows us to pass a complete URL for an endpoint:

How does Retrofit 2 handle okhttp endpoint URLs?

Retrofit 2 uses OkHttp’s HttpUrl and resolves each endpoint url like a link on any website ( <a href="">…</a> ). Let’s touch some scenarios and at first look at a url that points to a profile picture stored on Amazon S3.

What is an absolute URL?

In this discussion, an absolute URL is of the form: Specifies how the resource is to be accessed. Specifies the name of the computer where the resource is located. Specifies the sequence of directories leading to the target. If resource is omitted, the target is the last directory in path.


1 Answers

Recently Square has released the Retrofit v2.0.0 BETA and it has a built-in support for dynamic URLs. Even though the Library is in Beta, based on what Jake Wharton told us in DroidCon NYC 2015, all the apis are stable and will not change. I'm personally adding it to my production so its up to you.

You will find the following links useful if you decide to do the upgrade:
Jake Wharton presentation @ DroidCon NYC 2015
A very good guide on the changes

In simple word, you can now use the api annotations (like @GET or @POST and others) without any path and then you pass in a @URL to your api method that the method will use to call.

----------------Retrofit 1.x

I figured out a nice way for doing this and would like to share it.

The trick is to use the dynamic URL as your End Point in the creation of RestAdapter and then have a empty path on your API interface.

Here is how I did it:

public RestAdapter getHostAdapter(String baseHost){
    RestAdapter restAdapter = new RestAdapter.Builder()
            .setEndpoint(baseHost)
            .setRequestInterceptor(requestInterceptor)
            .build();

    return restAdapter;
}

I build my restAdapter using this method and then I have this in my interface:
(this will not work if your URL has query parameters added to it. See next answer for solution to that case)

public interface General {
    @GET("/")
    void getSomething(Callback<SomeObject> callback);
}

and finally using them like this:

getHostAdapter("YOUR_DYNAMIC_URL").create(General.class)
    .getSomething(new Callback<SomeObject>(){
        ...
    })

Hope it helps.

like image 86
Kayvan N Avatar answered Oct 03 '22 10:10

Kayvan N