-
-
Save jacobtabak/78e226673d5a6a4c4367 to your computer and use it in GitHub Desktop.
CookieManager cookieManager = new CookieManager(new PersistentCookieStore(context), CookiePolicy.ACCEPT_ALL); | |
OkHttpClient okHttpClient = new OkHttpClient(); | |
okHttpClient.setCookieHandler(cookieManager); | |
Client client = new OkClient(okHttpClient); | |
// then use this client in your RestAdapter.Builder() |
A use case where this can be used ? Thanks.
Thansk for this useful could.
But I am getting nullpointer exception in following code in add
method of class PersistentCookieStore.java
// Save cookie into persistent store
SharedPreferences.Editor prefsWriter = cookiePrefs.edit();
prefsWriter.putString(uri.getHost(), TextUtils.join(",", cookies.get(uri.getHost()).keySet()));
prefsWriter.putString(COOKIE_NAME_PREFIX + name, encodeCookie(new SerializableHttpCookie(cookie)));
prefsWriter.commit();
Here is the exception thrown
Fatal Exception: java.lang.NullPointerException: Attempt to invoke virtual method 'java.util.Set java.util.concurrent.ConcurrentHashMap.keySet()' on a null object reference
at com.magentocommerce.mobile.sdk.network.PersistentCookieStore.add(PersistentCookieStore.java:86)
at java.net.CookieManager.put(CookieManager.java:188)
at com.android.okhttp.internal.http.HttpEngine.receiveHeaders(HttpEngine.java:1257)
at com.android.okhttp.internal.http.HttpEngine.readResponse(HttpEngine.java:954)
at com.android.okhttp.internal.huc.HttpURLConnectionImpl.execute(HttpURLConnectionImpl.java:482)
at com.android.okhttp.internal.huc.HttpURLConnectionImpl.getResponse(HttpURLConnectionImpl.java:418)
at com.android.okhttp.internal.huc.HttpURLConnectionImpl.getResponseCode(HttpURLConnectionImpl.java:540)
at com.android.okhttp.internal.huc.DelegatingHttpsURLConnection.getResponseCode(DelegatingHttpsURLConnection.java:105)
at com.android.okhttp.internal.huc.HttpsURLConnectionImpl.getResponseCode(HttpsURLConnectionImpl.java:25)
at org.springframework.http.client.SimpleClientHttpResponse.getRawStatusCode(SimpleClientHttpResponse.java:47)
at org.springframework.http.client.AbstractClientHttpResponse.getStatusCode(AbstractClientHttpResponse.java:32)
at org.springframework.web.client.DefaultResponseErrorHandler.getHttpStatusCode(DefaultResponseErrorHandler.java:55)
at org.springframework.web.client.DefaultResponseErrorHandler.hasError(DefaultResponseErrorHandler.java:49)
at org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:484)
at org.springframework.web.client.RestTemplate.execute(RestTemplate.java:446)
at org.springframework.web.client.RestTemplate.getForObject(RestTemplate.java:214)
at com.magentocommerce.mobile.sdk.network.ConnectionController.executeRequest(ConnectionController.java:247)
at com.magentocommerce.mobile.sdk.service.ApiService.handleApiTask(ApiService.java:86)
at com.magentocommerce.mobile.sdk.service.ApiService.access$000(ApiService.java:29)
at com.magentocommerce.mobile.sdk.service.ApiService$ApiTask.run(ApiService.java:123)
at java.lang.Thread.run(Thread.java:818)
I am not getting why this happening, appreciate your help on this.
Do I have to check below if condition ?
if(cookies.containsKey(uri.getHost())) {
// Save cookie into persistent store
SharedPreferences.Editor prefsWriter = cookiePrefs.edit();
prefsWriter.putString(uri.getHost(), TextUtils.join(",", cookies.get(uri.getHost()).keySet()));
prefsWriter.putString(COOKIE_NAME_PREFIX + name, encodeCookie(new SerializableHttpCookie(cookie)));
prefsWriter.commit();
}
Hi ,
I used this code and am able to store session cookies, but am not able to understand how do I fetch these cookies when am sending next request.
Can you please help?
The above code will not work! The field accessed by cookie.getMaxAge()
references a duration from the initialization of the cookie. In order for this value to be meaningful the datetime saved in whenCreated
has to be serialized as well. Since this value is a private property you have to use reflection to access the correct field.
Using the proposed code above will save the cookie and it's value but in turn the cookie will never expire.
The above code will not work! The field accessed by
cookie.getMaxAge()
references a duration from the initialization of the cookie. In order for this value to be meaningful the datetime saved inwhenCreated
has to be serialized as well. Since this value is a private property you have to use reflection to access the correct field.Using the proposed code above will save the cookie and it's value but in turn the cookie will never expire.
This is True.
I want to add as well that on pre API 23, the HttpCookie hasExpired Method works COMPLETELY differently in a way that it will never call true on a cookie with an expiration date.
Hi all,
I am facing this bug. after uninstall and install app. could you please any one help
HTTP FAILED: java.lang.NullPointerException: Attempt to invoke virtual method 'java.util.Set java.util.concurrent.ConcurrentHashMap.keySet()' on a null object reference
How can I remove context here?
Much useful, saved my time writing all these. Thank you.