Created
December 10, 2013 06:07
-
-
Save mochico/7886338 to your computer and use it in GitHub Desktop.
VolleyでCookieを使い回す ref: http://qiita.com/mochico@github/items/20c228a87e000039cd54
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class MyJsonObjectRequest extends JsonObjectRequest { | |
private MyJsonObjectRequest(int method, String url, | |
JSONObject jsonRequest, Listener<JSONObject> listener, | |
ErrorListener errorListener) { | |
super(method, url, jsonRequest, listener, errorListener); | |
} | |
public MyJsonObjectRequest(String url, JSONObject jsonRequest, | |
Listener<JSONObject> listener, ErrorListener errorListener) { | |
super(url, jsonRequest, listener, errorListener); | |
} | |
@Override | |
protected Response<JSONObject> parseNetworkResponse( | |
NetworkResponse response) { | |
Map<String, String> headers = response.headers; | |
if (headers.containsKey(Constants.SET_COOKIE)) { | |
mContext.getSharedPreferences(PREF_NAME, Context.MODE_PRIVATE) | |
.edit() | |
.putString(PREF_SAVED_COOKIE, headers.get("Set-Cookie")) | |
.commit(); | |
} | |
return super.parseNetworkResponse(response); | |
} | |
@Override | |
public Map<String, String> getHeaders() throws AuthFailureError { | |
Map<String, String> customHeaders = super.getHeaders(); | |
Map<String, String> newHeaders = new HashMap<String, String>(); | |
newHeaders.putAll(customHeaders); | |
String cookieStr = mContext.getSharedPreferences(PREF_NAME, | |
Context.MODE_PRIVATE).getString(PREF_SAVED_COOKIE, ""); | |
if (cookieStr.length() > 0) { | |
newHeaders.put("cookie", cookieStr); | |
} | |
return newHeaders; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment