Se il parametro percorso non è nella stessa posizione nel URL per ogni richiesta, ad esempio, http://endpoint/blah/{apiKey}
e http://endpoint/blah/blah/{apiKey}/blah
, si potrebbe fare quanto segue.
nell'interfaccia APIService
@GET(/blah/{apiKey})
void getFoo(Callback<Object> callback);
@GET(/blah/blah/{apiKey}/blah)
void getFooBlah(Callback<Object> callback);
Poi nella classe ApiClient creare una RequestInterceptor
private static APIService sAuthorizedApiService;
private static Gson gson;
static {
gson = new GsonBuilder().setPrettyPrinting()
.create();
}
public static synchronized APIService getApiClient(final Context context) {
if (sAuthorizedApiService == null) {
RequestInterceptor requestInterceptor = new RequestInterceptor() {
@Override
public void intercept(RequestFacade request) {
request.addPathParam("apiKey", DataProvider.getInstance(context).getApiKey();
}
};
RestAdapter restAdapter = new RestAdapter.Builder().setLogLevel(RestAdapter.LogLevel.FULL)
.setClient(new OkClient(new OkHttpClient()))
.setEndpoint("http://endpoint")
.setRequestInterceptor(requestInterceptor)
.setConverter(new GsonConverter(gson))
.build();
sAuthorizedApiService = restAdapter.create(GMAuthorizedApiService.class);
}
return sAuthorizedApiService;
}
fonte
2015-05-27 14:57:37
Ti piace dare un'occhiata al mio tentativo di [affrontare più punti finali dell'API] (http://stackoverflow.com/questions/27399633/how-to-address-multiple-api-end-points-using-retrofit)? – JJD