Il mio apiPath è completamente dinamico. Sto avendo oggetti contenenti campi come "ipAddress" e "SSLprotocol". Sulla base di loro posso costruire il mio url:Come installare Retrofit senza baseUrl
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;
}
Quindi il mio protocollo + indirizzo può essere: http: // + 192.168.0.01:8010 = http://192.168.0.01:8010
e vorrei usarlo come quella:
@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);
percorso in modo completo per authenticateUser sarebbe http://192.168.0.01:8010/json/token.php - per esempio.
Ciò significa che non ho bisogno di alcun basePath perché creo me stesso basePath in base al server a cui desidero connettermi.
La mia configurazione è retrofit:
@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();
}
Se rimuovo baseurl tanto sono errore che questo parametro è obbligatorio. Così ho impostato il mio apiBasePath a:
public String getApiBasePath() {
return "";
}
E poi ottengo l'errore immediatamente dopo creo esempio retrofit:
java.lang.IllegalArgumentException: Illegal URL:
Come risolverlo?
Ti consente di utilizzare "http: //" come URL di base? – TheoKanning
Hmm no, ricevo: java.lang.IllegalArgumentException: URL non valido: http: // – F1sher
potresti utilizzare l'annotazione @Url. Ma dovrai compilare ogni URL completamente – Blackbelt