|
3 | 3 |
|
4 | 4 | package net.servicestack.android; |
5 | 5 |
|
| 6 | +import android.content.Context; |
6 | 7 | import android.os.AsyncTask; |
7 | 8 |
|
8 | | -import net.servicestack.client.AsyncComplete; |
9 | 9 | import net.servicestack.client.AsyncError; |
10 | 10 | import net.servicestack.client.AsyncResult; |
11 | 11 | import net.servicestack.client.AsyncResultVoid; |
12 | 12 | import net.servicestack.client.AsyncServiceClient; |
| 13 | +import net.servicestack.client.AsyncSuccess; |
13 | 14 | import net.servicestack.client.AsyncSuccessVoid; |
14 | 15 | import net.servicestack.client.IReturn; |
15 | 16 | import net.servicestack.client.IReturnVoid; |
16 | 17 | import net.servicestack.client.JsonServiceClient; |
17 | | -import net.servicestack.client.AsyncSuccess; |
18 | 18 | import net.servicestack.client.Utils; |
| 19 | +import net.servicestack.cookies.SerializableCookieStore; |
19 | 20 |
|
20 | 21 | import java.lang.reflect.Type; |
| 22 | +import java.net.CookieHandler; |
| 23 | +import java.net.CookieManager; |
| 24 | +import java.net.CookiePolicy; |
21 | 25 | import java.net.HttpURLConnection; |
22 | 26 | import java.util.Map; |
23 | 27 |
|
24 | 28 | public class AndroidServiceClient extends JsonServiceClient implements AsyncServiceClient { |
25 | 29 |
|
| 30 | + Context context; |
| 31 | + |
26 | 32 | public AndroidServiceClient(String baseUrl) { |
27 | 33 | super(baseUrl); |
28 | 34 | } |
29 | 35 |
|
| 36 | + public AndroidServiceClient(String baseUrl, Context context) { |
| 37 | + super(baseUrl, false); |
| 38 | + this.context = context; |
| 39 | + this.initCookieHandler(); |
| 40 | + } |
| 41 | + |
| 42 | + @Override |
| 43 | + public void initCookieHandler() { |
| 44 | + //Automatically populate response cookies |
| 45 | + if (CookieHandler.getDefault() == null) { |
| 46 | + if (this.context != null) { |
| 47 | + SerializableCookieStore cookieStore = new SerializableCookieStore(context); |
| 48 | + CookieManager cookieManager = new CookieManager(cookieStore, CookiePolicy.ACCEPT_ALL); |
| 49 | + CookieHandler.setDefault(cookieManager); |
| 50 | + } else { |
| 51 | + //Doesn't support getCookieStore() throws UnsupportedOperationException |
| 52 | + CookieManager cookieManager = new CookieManager(null, CookiePolicy.ACCEPT_ALL); |
| 53 | + CookieHandler.setDefault(cookieManager); |
| 54 | + } |
| 55 | + } |
| 56 | + } |
| 57 | + |
30 | 58 | // Override to customize execution of AsyncTask |
31 | 59 | public <T,TResponse> void execTask(AsyncTask<T,Void,TResponse> asyncTask, T... request){ |
32 | 60 | asyncTask.execute(request); |
|
0 commit comments