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?
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With