Created
August 24, 2016 16:53
-
-
Save surajsau/eceb379d8a36ea5948dd6c54461fb68b to your computer and use it in GitHub Desktop.
Boilerplate code for Retrofit in code
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
dependencies { | |
/* take care of the current versions while adding */ | |
compile fileTree(dir: 'libs', include: ['*.jar']) | |
testCompile 'junit:junit:4.12' | |
compile 'com.android.support:appcompat-v7:24.0.0' | |
compile 'com.android.support:design:24.0.0' | |
compile 'com.android.support:cardview-v7:24.0.0' | |
compile 'com.jakewharton:butterknife:7.0.1' | |
compile 'com.squareup.retrofit2:retrofit:2.0.1' | |
compile 'com.squareup.retrofit2:converter-jackson:2.0.1' | |
compile 'com.squareup.retrofit2:adapter-rxjava:2.0.1' | |
compile 'com.squareup.okhttp3:logging-interceptor:3.3.1' | |
compile 'com.squareup.picasso:picasso:2.5.2' | |
compile 'io.reactivex:rxandroid:1.1.0' | |
compile 'io.reactivex:rxjava:1.1.0' | |
compile 'com.android.support:support-v4:24.0.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
import java.io.IOException; | |
import in.surajsau.popularmovies.IConstants; | |
import okhttp3.HttpUrl; | |
import okhttp3.Interceptor; | |
import okhttp3.OkHttpClient; | |
import okhttp3.Request; | |
import okhttp3.Response; | |
import okhttp3.logging.HttpLoggingInterceptor; | |
import retrofit2.Retrofit; | |
import retrofit2.adapter.rxjava.RxJavaCallAdapterFactory; | |
import retrofit2.converter.jackson.JacksonConverterFactory; | |
/** | |
* Created by MacboolBro on 08/04/16. | |
*/ | |
public class ServiceGenerator { | |
/*Interceptor for common query param to be put in every url like API key*/ | |
// private static Interceptor apiKeyInterceptor = new Interceptor() { | |
// @Override | |
// public Response intercept(Chain chain) throws IOException { | |
// Request request = chain.request(); | |
// HttpUrl url = request.url().newBuilder().addQueryParameter(IConstants.API_KEY_PARAM, IConstants.API_KEY).build(); | |
// request = request.newBuilder().url(url).build(); | |
// return chain.proceed(request); | |
// } | |
// }; | |
/*For logging the Requests. | |
in build.gradle add .. compile 'com.squareup.okhttp3:logging-interceptor:3.3.1' | |
*/ | |
// private static HttpLoggingInterceptor loggingInterceptor = new HttpLoggingInterceptor(); | |
// private static OkHttpClient.Builder httpClient = new OkHttpClient.Builder() | |
// .addInterceptor(apiKeyInterceptor); | |
private static Retrofit.Builder builder = new Retrofit.Builder() | |
.baseUrl(IConstants.BASE_URL) | |
.addConverterFactory(JacksonConverterFactory.create()) | |
.addCallAdapterFactory(RxJavaCallAdapterFactory.create()); | |
public static<T> T createService (Class<T> serviceClass) { | |
//--adding loggingInterceptor | |
loggingInterceptor.setLevel(HttpLoggingInterceptor.Level.BODY); | |
httpClient.addInterceptor(loggingInterceptor); | |
Retrofit retrofit = builder.client(httpClient.build()).build(); | |
return retrofit.create(serviceClass); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment