How to Keep Session in Fresco with OKHTTP or Add Cookie Before Image URI call?
See original GitHub issueI login my server use retrofit with okhttp I login using the Retrofit with okhttp to the server . and I save Cookie at Preferences.
client = new OkHttpClient();
client.interceptors().add(new AddCookiesInterceptor());
client.interceptors().add(new ReceivedCookiesInterceptor());
AddCookiesInterceptor.java
public class AddCookiesInterceptor implements Interceptor {
@Override
public Response intercept(Chain chain) throws IOException {
Request.Builder builder = chain.request().newBuilder();
HashSet<String> preferences = (HashSet) PreferenceHelper.getDefaultPreferences().getStringSet(PreferenceHelper.PREF_COOKIES, new HashSet<String>());
for (String cookie : preferences) {
builder.addHeader("Cookie", cookie);
Log.v("OkHttp", "Adding Header: " + cookie); // This is done so I know which headers are being added; this interceptor is used after the normal logging of OkHttp
}
return chain.proceed(builder.build());
}
}
ReceivedCookiesInterceptor.java
public class ReceivedCookiesInterceptor implements Interceptor {
@Override
public Response intercept(Interceptor.Chain chain) throws IOException {
Response originalResponse = chain.proceed(chain.request());
if (!originalResponse.headers("Set-Cookie").isEmpty()) {
HashSet<String> cookies = new HashSet<>();
for (String header : originalResponse.headers("Set-Cookie")) {
cookies.add(header);
}
PreferenceHelper.getDefaultPreferences().edit()
.putStringSet(PreferenceHelper.PREF_COOKIES, cookies)
.apply();
}
return originalResponse;
}
}
And then I initialize Fresco.
ImagePipelineConfig config = OkHttpImagePipelineConfigFactory
.newBuilder(this, CustomRestAdapter.getClient())
.build();
Fresco.initialize(this , config);
...
public static DirectFoldertInterface getInstance() {
if (DirectFoldertInterface == null) {
client = new OkHttpClient();
client.setConnectTimeout(1500, TimeUnit.MILLISECONDS);
client.setWriteTimeout(1500, TimeUnit.MILLISECONDS);
client.setReadTimeout(1500, TimeUnit.MILLISECONDS);
client.interceptors().add(new AddCookiesInterceptor());
client.interceptors().add(new ReceivedCookiesInterceptor());
}
But Fresco can’t load Image. I Think The Problem is cookie. How to Keep Session in Fresco with OKHTTP or Add Cookie Before Image URI call?
Issue Analytics
- State:
- Created 8 years ago
- Comments:8
Top Results From Across the Web
Add cookie to client request OkHttp - android - Stack Overflow
There are 2 ways you can do this: OkHttpClient client = new OkHttpClient().newBuilder() .cookieJar(new CookieJar() { @Override public void ...
Read more >Using Other Network Layers - Fresco
Handling sessions and cookies correctly. The OkHttpClient you pass to Fresco in the above step should be set up with interceptors needed to...
Read more >Maintain cookie session in Android(Retrofit) - Nanostuffs
A session is a sequence of requests made by a single end-user ... So, we need to keep the cookie from one call...
Read more >check the crashlytics plugin to make sure that the application has ...
Open your Firebase project at the Google console and click 'Add app' to add an iOS and / or Android app. Follow the...
Read more >Free Automated Malware Analysis Service - powered by Falcon ...
Re-analyze Hash Not Seen Before ... Not all malicious and suspicious indicators are displayed. ... Has the ability to query the phone location...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
@kirtov I solved this problem. right this. Client Setting
MyCookieManager
DCB_AddCookiesInterceptor
DCB_ReceivedCookiesInterceptor
I am doing this request :
But how to get the cookies that the server returned me? I just want to store it on a String variable (retreivedCookie ) and reuse on the second request
Thank you in advance !