Skip to content

Instantly share code, notes, and snippets.

@minibugdev
Created August 26, 2016 08:02
Show Gist options
  • Save minibugdev/e5778bfb66f958a6a239bec706eaf439 to your computer and use it in GitHub Desktop.
Save minibugdev/e5778bfb66f958a6a239bec706eaf439 to your computer and use it in GitHub Desktop.
public class CookieApplication extends Application {
@Override
public void onCreate() {
super.onCreate();
CookieHandler defaultHandler = CookieHandler.getDefault();
if (defaultHandler == null) {
CookieHandler.setDefault(new CookieManager(new PersistentCookieStore(this), CookiePolicy.ACCEPT_ALL));
}
/***
* PersistentCookieStore Source code
* link : https://github.com/loopj/android-async-http/blob/master/library/src/main/java/com/loopj/android/http/PersistentCookieStore.java
*/
}
}
public class Retrofit2CookieManager {
public static final String BASE_URL = "http://your-api-url.com/";
public static YourApiInterface getInstance() {
// Cookies
CookieManager cookieManager = (CookieManager) CookieHandler.getDefault();
// Http Log
HttpLoggingInterceptor interceptor = new HttpLoggingInterceptor();
interceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
// Http Client
OkHttpClient client = new OkHttpClient.Builder()
.addInterceptor(interceptor)
.cookieJar(new JavaNetCookieJar(cookieManager))
.build();
return new Retrofit.Builder()
.addConverterFactory(GsonConverterFactory.create())
.baseUrl(BASE_URL)
.client(client)
.build()
.create(YourApiInterface.class);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment