-
-
Save lezorich/8f3f3a54f07515881581 to your computer and use it in GitHub Desktop.
Where shall I include this :
CookieManager cookieManager = new CookieManager(new PersistentCookieStore(mContext), CookiePolicy.ACCEPT_ORIGINAL_SERVER);
CookieHandler.setDefault(cookieManager); ??
Shall I include it after calling the request queue and making the json object request?
@arindamrockz: Yes, you should included before calling the request queue. I always included at the start of the application.
+1
Thank you for this implementation.
+1
Thank you!
I just used volley in my Android project, but it cannot save cookies.The file is really what I want!
Thank you, works impresive
Can you give an example app
First of all, thanks a lot for all the help!, I'm a beginner and i need to save sign in throughout an app, so how do i request the cookie back from the cookie store and apply it with volley in the post sign in subsequent requests?
Where are you removing cookies from SharedPreferences
? It looks like you remove cookies from the CookieStore
but only from SharedPreferences
when adding a new cookie.
This also works for OKHTTP3 + Retrofit 2.
For some reason, other solutions specific to Retrofit is giving me issues with a nodejs using passportjs. The socket.handshake.session
does not store passport user even if user is already logged in. I TRIED THIS AND IT WORKED! Session is persistent and can now retrieve passport object from socket session :)
PersistentCookieStore cookieStore = new PersistentCookieStore(context);
CookieManager cookieManager = new CookieManager(cookieStore, CookiePolicy.ACCEPT_ORIGINAL_SERVER);
CookieHandler.setDefault(cookieManager);
return new OkHttpClient.Builder()
.sslSocketFactory(getSslSocketFactory(caInput), getTrustManager())
.addNetworkInterceptor(new StethoInterceptor())
.addInterceptor(loggingInterceptor)
.cookieJar(new JavaNetCookieJar(cookieManager))
.build();
@lezorich, i get a cookie when i login to a website and i need to send that cookie with each request i make. can you please post a sample code for doing the same ?
This is my current code:
RequestQueue queue = VolleySingleton.getInstance(this.getApplicationContext()).
getRequestQueue();
String url = "https://my_api_url";
JsonObjectRequest jsObjRequest = new JsonObjectRequest
(Request.Method.POST, url, null, new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
Log.i(">>>>>resp: ", "Response: " + response.toString());
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
// TODO Auto-generated method stub
Log.i(">>>>>resp: ", "Error: " + error.toString());
}
}) {
@Override
protected Map<String, String> getParams() {
Map<String, String> params = new HashMap<String, String>();
params.put("aaaa", "dddd");
return params;
}
};
VolleySingleton.getInstance(this).addToRequestQueue(jsObjRequest);
Volley is giving ServorError when this CookieStore is bieng used, If I remove the line to set this, Volley works file but the response returns invalid session.
Do you have any idea why this happens like this?
@lezorich what happens when cookies are removed? Won't it still be stored in the persistent storage?
Hey Lukas,
Thank you for this!
I am facing one issue, though. If I kill the app, and then restart it, I am being logged out. Is this the expected behaviour?
Thank you, again!
Hello, there is another easy way to maintain cookies session and that is to add this line in a class that is extended with APPLICATION class: CookieHandler.setDefault(new CookieManager(null, CookiePolicy.ACCEPT_ALL));
Hello, there is another easy way to maintain cookies session and that is to add this line in a class that is extended with APPLICATION class: CookieHandler.setDefault(new CookieManager(null, CookiePolicy.ACCEPT_ALL));
@asadullah06 did it work for you ? so cookies are stored automatic and no need to explicitly send them in subsequent requests
@AnixPasBesoin, I have just made an update of the gist. Try to use it now, and tell me if you don't understand something :)