Home > Mobile >  Use the Retrofit, the background return cookie value what to get?
Use the Retrofit, the background return cookie value what to get?

Time:10-22

Returns the value of the console is such printing: OkHttp: Set - cookies: f15 d8a JSESSIONID=54751703-2-4-969 - e - 34 b5edc05017; Path=/; HttpOnly
Just behind JSESSIONID=is cookies.
Background to save the return after I log in successfully cookie, then log in every time the cookie in the past. Now the question is: how to get the cookie value, after landing successful then every time pass with the past?
Below is the Retrofit wrapper class:
 public class RxManager {

Private static RxManager INSTANCE;
Private RequestMethod mRequestService;
Private Gson mGson=new Gson ();
//use cookies save user state
Private MediaType JSON=MediaType. Parse (" application/JSON; charset=utf-8");
Private static WeakReference MActivity;
Private static final String TAG="RxManager";

Private RxManager () {
OkHttpClient OkHttpClient=createOkHttpClient ();
Retrofit Retrofit=new Retrofit. The Builder (). The baseUrl RequestUrl. BASE_URL ()
AddCallAdapterFactory (RxJava2CallAdapterFactory. The create ())
AddConverterFactory (GsonConverterFactory. Create (mGson))
//addConverterFactory (ResponseConverterFactory. Create (mGson))//processing Rxjava, Retrofit abnormal returns json data parsing
. The client (okHttpClient)
.build();
MRequestService=retrofit. The create (RequestMethod. Class);
}

Public static RxManager getInstance () {
If (the INSTANCE==null) {
The INSTANCE=new RxManager ();
}
Return the INSTANCE.
}

Private OkHttpClient createOkHttpClient () {
//cookieJar. The clear ();
//the d (TAG, "createOkHttpClient: 1709=" + cookieJar. ToString ());

HttpLoggingInterceptor loggingInterceptor=new HttpLoggingInterceptor ();
LoggingInterceptor. SetLevel (HttpLoggingInterceptor. Level. BODY);//this line must be added or not print by default
//cache the url
The File httpCacheDirectory=new File (BaseApplication getContext () getCacheDir (), "responses");
Int cacheSize=10 * 1024 * 1024;//10 MiB
Cache Cache=new Cache (httpCacheDirectory cacheSize);

SharedPrefsCookiePersistor SharedPrefsCookiePersistor=new SharedPrefsCookiePersistor (BaseApplication getContext ());
List Cookies.=sharedPrefsCookiePersistor loadAll ();
for (int i=0; I & lt; Cookies, size (); I++) {
Cookie Cookie=cookies. Get (I);
The d (TAG, "createOkHttpClient: 1810=" + cookie. The name () + "" + cookies. The value ());
}
ClearableCookieJar cookieJar=new PersistentCookieJar (new SetCookieCache (), sharedPrefsCookiePersistor);

Return new OkHttpClient
The Builder ()
. CookieJar (cookieJar)//use cookies save user state
Cache (new cache (BaseApplication getContext () getCacheDir (), 1024 * 1024 * 100))
ConnectTimeout (30, TimeUnit. SECONDS)
ReadTimeout (30, TimeUnit. SECONDS)
WriteTimeout (30, TimeUnit. SECONDS)
. AddInterceptor (loggingInterceptor)//add the console network request message interceptor, directly in the console output
The cache (cache)
. AddInterceptor (REWRITE_CACHE_CONTROL_INTERCEPTOR)
//. AddInterceptor (new CacheInterceptor (BaseApplication getContext () getCacheDir () getAbsolutePath ()))
AddInterceptor (chain - & gt; {
Request the Request=chain. The Request ();
The Response the Response=chain. Proceed (request);
Int code=response. Code ();
The d (TAG, "0922: code inside OkHttpClient=OkHttpClient" + code);

If (code==200) {
if (null !=the response. The header (" Set - cookies ")) {
String cookie=response. The header (" Set - cookies "). The split (";" ) [0];
The d (TAG, "createOkHttpClient: 1917=" + cookie);//the cookies to print here is rememberMe=deleteMe, should not
}
}
return response;
}). The build ();
}

//cache
Interceptor REWRITE_CACHE_CONTROL_INTERCEPTOR=chain - & gt; {
CacheControl. Builder cacheBuilder=new CacheControl. Builder ();
CacheBuilder. MaxAge (0, TimeUnit. SECONDS);
CacheBuilder. MaxStale (365, TimeUnit. DAYS);
CacheControl CacheControl=cacheBuilder. The build ();
Request the Request=chain. The Request ();
if (! NetWorkStatusUtil. CheckNetWorkAvaliable (BaseApplication getContext ())) {
Request=request. NewBuilder ()
. CacheControl (cacheControl)
.build();
}
The Response originalResponse=chain. Proceed (request);
Int code=originalResponse. Code ();

The d (TAG, "0922: in the interceptor code=" + code);
//todo: 302 directly to the login page
If (code==302) {
UserManager. GetInstance (). Logout ();//remove all data
PreferenceUtil. ClearSingle (mActivity. The get (), "assessment");
ToastUtils. ShowShort (R.s tring. Please_relogin);
Intent loginIntent=new Intent (mActivity. The get (), UnloginMainActivity. Class);
LoginIntent. SetFlags (Intent. FLAG_ACTIVITY_CLEAR_TOP);
MActivity. The get (). StartActivity (loginIntent);
}

If (NetWorkStatusUtil checkNetWorkAvaliable (BaseApplication getContext ())) {
Int maxAge=0;//read from the cache
Return originalResponse. NewBuilder ()
. RemoveHeader (" Pragma ")
. The header (" cache-control ", "public, Max - age=" + maxAge)
.build();
} else {
Int maxStale=60 * 60 * 24 * 28; 4 - weekes//tolerate stale
Return originalResponse. NewBuilder ()
. RemoveHeader (" Pragma ")
nullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnull
  • Related