1+2) I don't think it is a good way to work with a Cookie, because when you stop the app, this cookie also will be deleted. You should save them to persistent storage and use them later.
3) You have to implement your own CookieStore, then create a CookieManager and set its default with CookieHandler.setDefault(...). This applies to the whole-system so you shouldn't create it for every activity.
4) If you still want to use your method. You could get a Cookie from the response header by the getHeaderFields() method, and query "Set-Cookie", and "Set-Cookie2" header. Build your header String like that "Set-Cookie:Your-string-cookie-here" and use HttpCookie.parse(headerString) to get your list HttpCookie object.
This is an example for create persistent cookie store. Here is the full source if you need: https://github.com/nguyentrungduy/Customize-CookieStore
@Override
public void add(URI uri, HttpCookie cookie) {
// TODO Auto-generated method stub
synchronized (this) {
URI uri2 = createURI(uri);
List<HttpCookie> cookies = mCookies.get(uri2);
if (cookies == null) {
cookies = new ArrayList<HttpCookie>();
mCookies.put(uri2, cookies);
} else {
cookies.remove(cookie);
}
cookies.add(cookie);
// Save into SharePreferenced
saveToPersistent(uri2, cookies);
}
}
@Override
public List<HttpCookie> get(URI uri) {
// TODO Auto-generated method stub
synchronized (this) {
List<HttpCookie> httpCookies = new ArrayList<HttpCookie>();
List<HttpCookie> values = mCookies.get(uri);
if (values != null) {
for (HttpCookie httpCookie : values) {
if (!httpCookie.hasExpired()) {
httpCookies.add(httpCookie);
}
}
}
Set<Entry<URI, List<HttpCookie>>> set = mCookies.entrySet();
for (Entry<URI, List<HttpCookie>> entry : set) {
if (uri.equals(entry.getKey())) {
continue;
}
List<HttpCookie> cookies = entry.getValue();
Iterator<HttpCookie> iterator = cookies.iterator();
while (iterator.hasNext()) {
HttpCookie cookie = iterator.next();
String domain = cookie.getDomain();
if (!domainMatches(domain, uri.getHost())) {
continue;
}
if (cookie.hasExpired()) {
iterator.remove();
} else if (!httpCookies.contains(cookie)) {
httpCookies.add(cookie);
}
}
}
return Collections.unmodifiableList(httpCookies);
}
}