How to setup Retrofit with no baseUrl

24,325

Solution 1

From source (New URL resolving concept) you can simply specify whole path in post request.

Moreover we also can declare a full URL in @Post in Retrofit 2.0:

public interface APIService {

    @POST("http://api.nuuneoi.com/special/user/list")
    Call<Users> loadSpecialUsers();

}

Base URL will be ignored for this case.

Solution 2

just use like this

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

source

Solution 3

With Retrofit 2 you have to put base url anyway. If it is not known then you just can put any url, usually, it is good to use http://localhost/.

Share:
24,325
F1sher
Author by

F1sher

Updated on July 22, 2022

Comments

  • F1sher
    F1sher almost 2 years

    My apiPath is fully dynamic. I am having items containing fields such us "ipAddress" and "SSLprotocol". Based on them I can build my url:

    private String urlBuilder(Server server) {
        String protocol;
        String address = "";
    
        if (AppTools.isDeviceOnWifi(activity)) {
            address = serverToConnect.getExternalIp();
        } else if (AppTools.isDeviceOnGSM(activity)) {
            address = serverToConnect.getInternalIp();
        }
    
        if (server.isShouldUseSSL()) {
            protocol = "https://";
        } else {
            protocol = "http://";
        }
        return protocol + address;
    }
    

    So my protocol + address can be: http:// + 192.168.0.01:8010 = http://192.168.0.01:8010

    And I would like to use it like that:

    @FormUrlEncoded
    @POST("{fullyGeneratedPath}/json/token.php")
    Observable<AuthenticationResponse> authenticateUser(
                @Path("fullyGeneratedPath") String fullyGeneratedPath,
                @Field("login") String login,
                @Field("psw") String password,
                @Field("mobile") String mobile);
    

    So full path for authenticateUser would be http://192.168.0.01:8010/json/token.php - for example.

    That means I don't need any basePath because I create whole basePath myself depending on server I want to connect to.

    My retrofit setup is:

    @Provides
    @Singleton
    Retrofit provideRetrofit(OkHttpClient okHttpClient,
                Converter.Factory converterFactory,
                AppConfig appConfig) {
        Retrofit.Builder builder = new Retrofit.Builder();
        builder.client(okHttpClient)
                .baseUrl(appConfig.getApiBasePath())
                .addConverterFactory(converterFactory)
                .addCallAdapterFactory(RxJavaCallAdapterFactory.create());
    
        return builder.build();
    }
    

    If I remove baseUrl then I get error that this parameter is required. So I set my apiBasePath to:

    public String getApiBasePath() {
        return "";
    }
    

    And then I get error instantly after I create retrofit instance:

    java.lang.IllegalArgumentException: Illegal URL: 
    

    How to solve it?