Created
October 15, 2015 16:16
-
-
Save imminent/3a0855030aa93b355317 to your computer and use it in GitHub Desktop.
Handling multiple API subdomains with Retrofit 2.0
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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