Skip to content

Instantly share code, notes, and snippets.

@imminent
Created October 15, 2015 16:16
Show Gist options
  • Save imminent/3a0855030aa93b355317 to your computer and use it in GitHub Desktop.
Save imminent/3a0855030aa93b355317 to your computer and use it in GitHub Desktop.
Handling multiple API subdomains with Retrofit 2.0
package com.example.api;
import java.util.Map;
import retrofit.Call;
import retrofit.http.Body;
import retrofit.http.GET;
import retrofit.http.POST;
public interface Api {
@GET("action")
Call getAction();
@POST("action")
Call postAction(@Body Map<String, Object> requestBody);
}
package com.example.api;
import com.squareup.okhttp.OkHttpClient;
import retrofit.GsonConverterFactory;
import retrofit.Retrofit;
public class ApiClient {
public final PlacesApi placesApi;
public final Api api;
public ApiClient(OkHttpClient client) {
final Retrofit.Builder retrofitBuilder = new Retrofit.Builder()
.addConverterFactory(GsonConverterFactory.create())
// Important to set a client on the build to prevent .build() from making new ones
.client(client);
api = retrofitBuilder.baseUrl("https://api.example.com").build().create(Api.class);
placesApi = retrofitBuilder.baseUrl("https://places.example.com").build().create(PlacesApi.class);
}
}
package com.example.api;
import retrofit.Call;
import retrofit.http.GET;
import retrofit.http.Query;
public interface PlacesApi {
@GET("places")
Call getPlaceAutocomplete(@Query("query") String query);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment