|
| 1 | +/* |
| 2 | + Android Asynchronous Http Client Sample |
| 3 | + Copyright (c) 2014 Marek Sebera <[email protected]> |
| 4 | + http://loopj.com |
| 5 | +
|
| 6 | + Licensed under the Apache License, Version 2.0 (the "License"); |
| 7 | + you may not use this file except in compliance with the License. |
| 8 | + You may obtain a copy of the License at |
| 9 | +
|
| 10 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | +
|
| 12 | + Unless required by applicable law or agreed to in writing, software |
| 13 | + distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | + See the License for the specific language governing permissions and |
| 16 | + limitations under the License. |
| 17 | +*/ |
| 18 | + |
| 19 | +package com.loopj.android.http.sample; |
| 20 | + |
| 21 | +import android.os.Bundle; |
| 22 | + |
| 23 | +import com.fasterxml.jackson.core.JsonFactory; |
| 24 | +import com.fasterxml.jackson.databind.ObjectMapper; |
| 25 | + |
| 26 | +import com.loopj.android.http.AsyncHttpClient; |
| 27 | +import com.loopj.android.http.BaseJsonHttpResponseHandler; |
| 28 | +import com.loopj.android.http.PersistentCookieStore; |
| 29 | +import com.loopj.android.http.RequestHandle; |
| 30 | +import com.loopj.android.http.ResponseHandlerInterface; |
| 31 | +import com.loopj.android.http.sample.util.SampleJSON; |
| 32 | + |
| 33 | +import org.apache.http.Header; |
| 34 | +import org.apache.http.HttpEntity; |
| 35 | +import org.apache.http.client.CookieStore; |
| 36 | + |
| 37 | +public class PersistentCookiesSample extends SampleParentActivity { |
| 38 | + |
| 39 | + private static final String LOG_TAG = "PersistentCookiesSample"; |
| 40 | + |
| 41 | + private CookieStore cookieStore; |
| 42 | + |
| 43 | + @Override |
| 44 | + protected void onCreate(Bundle savedInstanceState) { |
| 45 | + // Use the application's context so that memory leakage doesn't occur. |
| 46 | + cookieStore = new PersistentCookieStore(getApplicationContext()); |
| 47 | + |
| 48 | + // Set the new cookie store. |
| 49 | + getAsyncHttpClient().setCookieStore(cookieStore); |
| 50 | + |
| 51 | + super.onCreate(savedInstanceState); |
| 52 | + } |
| 53 | + |
| 54 | + @Override |
| 55 | + public int getSampleTitle() { |
| 56 | + return R.string.title_persistent_cookies; |
| 57 | + } |
| 58 | + |
| 59 | + @Override |
| 60 | + public boolean isRequestBodyAllowed() { |
| 61 | + return false; |
| 62 | + } |
| 63 | + |
| 64 | + @Override |
| 65 | + public boolean isRequestHeadersAllowed() { |
| 66 | + return true; |
| 67 | + } |
| 68 | + |
| 69 | + @Override |
| 70 | + public String getDefaultURL() { |
| 71 | + // The base URL for testing cookies. |
| 72 | + String url = "http://httpbin.org/cookies"; |
| 73 | + |
| 74 | + // If the cookie store is empty, suggest a cookie. |
| 75 | + if(cookieStore.getCookies().isEmpty()) { |
| 76 | + url += "/set?time=" + System.currentTimeMillis(); |
| 77 | + } |
| 78 | + |
| 79 | + return url; |
| 80 | + } |
| 81 | + |
| 82 | + @Override |
| 83 | + public ResponseHandlerInterface getResponseHandler() { |
| 84 | + return new BaseJsonHttpResponseHandler<SampleJSON>() { |
| 85 | + @Override |
| 86 | + public void onStart() { |
| 87 | + clearOutputs(); |
| 88 | + } |
| 89 | + |
| 90 | + @Override |
| 91 | + public void onSuccess(int statusCode, Header[] headers, String rawJsonResponse, SampleJSON response) { |
| 92 | + debugHeaders(LOG_TAG, headers); |
| 93 | + debugStatusCode(LOG_TAG, statusCode); |
| 94 | + if (response != null) { |
| 95 | + debugResponse(LOG_TAG, rawJsonResponse); |
| 96 | + } |
| 97 | + } |
| 98 | + |
| 99 | + @Override |
| 100 | + public void onFailure(int statusCode, Header[] headers, Throwable throwable, String rawJsonData, SampleJSON errorResponse) { |
| 101 | + debugHeaders(LOG_TAG, headers); |
| 102 | + debugStatusCode(LOG_TAG, statusCode); |
| 103 | + debugThrowable(LOG_TAG, throwable); |
| 104 | + if (errorResponse != null) { |
| 105 | + debugResponse(LOG_TAG, rawJsonData); |
| 106 | + } |
| 107 | + } |
| 108 | + |
| 109 | + @Override |
| 110 | + protected SampleJSON parseResponse(String rawJsonData, boolean isFailure) throws Throwable { |
| 111 | + return new ObjectMapper().readValues(new JsonFactory().createParser(rawJsonData), SampleJSON.class).next(); |
| 112 | + } |
| 113 | + }; |
| 114 | + } |
| 115 | + |
| 116 | + @Override |
| 117 | + public RequestHandle executeSample(AsyncHttpClient client, String URL, Header[] headers, HttpEntity entity, ResponseHandlerInterface responseHandler) { |
| 118 | + return client.get(this, URL, headers, null, responseHandler); |
| 119 | + } |
| 120 | + |
| 121 | +} |
0 commit comments