|
package com.medweb.cloud.manager; |
|
|
|
import android.content.Context; |
|
|
|
import com.koushikdutta.async.http.Headers; |
|
import com.koushikdutta.ion.Ion; |
|
|
|
import java.io.IOException; |
|
import java.net.CookieManager; |
|
import java.net.URI; |
|
import java.net.URLConnection; |
|
import java.util.ArrayList; |
|
import java.util.List; |
|
|
|
/* |
|
This is adapted from my own code... so I'm not sure this will work exactly like this but it'll give you a starting point |
|
*/ |
|
public class IonCookieManager { |
|
CookieManager manager; |
|
|
|
public IonCookieManager(Context context) { |
|
Ion ion = Ion.getDefault(context); |
|
manager = ion.getCookieMiddleware().getCookieManager(); |
|
} |
|
|
|
public void storeCookies(URLConnection conn) throws IOException { |
|
|
|
List<String> cookies = conn.getHeaderFields().get("Set-Cookie"); |
|
URI uri = URI.create(conn.getURL().toString()); |
|
if(cookies != null) { |
|
storeCookies(uri, cookies); |
|
} |
|
} |
|
|
|
public void storeCookies(URI uri, List<String> cookies) throws IOException { |
|
Headers headers = new Headers(); |
|
headers.addAll("Set-Cookie", cookies); |
|
|
|
manager.put(uri, headers.getMultiMap()); |
|
} |
|
|
|
public void storeCookie(URI uri, String cookieName, String cookieValue) throws IOException { |
|
List<String> cookie = new ArrayList<String>(); |
|
cookie.add(String.format("%s=%s", cookieName, cookieValue)); |
|
storeCookies(uri, cookie); |
|
} |
|
|
|
} |