Set dynamic base url using Retrofit 2.0 and Dagger 2

前端 未结 8 534
南旧
南旧 2020-11-27 12:36

I\'m trying to perform a login action using Retrofit 2.0 using Dagger 2

Here\'s how I set up Retrofit dependency

@Provides
@Singleton
Retrofit provid         


        
相关标签:
8条回答
  • 2020-11-27 13:05

    This might be late but Retrofit allows you to use dynamic URLs while making the network call itself using @Url annotation. I am also using Dagger2 to inject the Retrofit instance in my repositories and this solution is working fine for me.

    This will use the base url

    provided by you while creating the instance of Retrofit.

    @GET("/product/123")
    fun fetchDataFromNetwork(): Call<Product>
    

    This ignore the base url

    and use the url you will be providing this call at run time.

    @GET()
    fun fetchDataFromNetwork(@Url url : String): Call<Product> //
    
    0 讨论(0)
  • 2020-11-27 13:06

    Retrofit2 library comes with a @Url annotation. You can override baseUrl like this:

    API interface:

    public interface UserService {  
        @GET
        public Call<ResponseBody> profilePicture(@Url String url);
    }
    

    And call the API like this:

    Retrofit retrofit = Retrofit.Builder()  
        .baseUrl("https://your.api.url/");
        .build();
    
    UserService service = retrofit.create(UserService.class);  
    service.profilePicture("https://s3.amazon.com/profile-picture/path");
    

    For more details refer to this link: https://futurestud.io/tutorials/retrofit-2-how-to-use-dynamic-urls-for-requests

    0 讨论(0)
提交回复
热议问题