Skip to content

Commit a947934

Browse files
author
Noor Dawod
committed
New example to demo use of PersistentCookieStore.
1 parent 2f96987 commit a947934

File tree

4 files changed

+138
-5
lines changed

4 files changed

+138
-5
lines changed
Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
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+
}

sample/src/main/java/com/loopj/android/http/sample/SampleParentActivity.java

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -125,23 +125,33 @@ public void onClick(View v) {
125125
}
126126
};
127127

128-
public Header[] getRequestHeaders() {
128+
public List<Header> getRequestHeadersList() {
129129
List<Header> headers = new ArrayList<Header>();
130130
String headersRaw = headersEditText.getText() == null ? null : headersEditText.getText().toString();
131131

132132
if (headersRaw != null && headersRaw.length() > 3) {
133133
String[] lines = headersRaw.split("\\r?\\n");
134134
for (String line : lines) {
135135
try {
136-
String[] kv = line.split("=");
137-
if (kv.length != 2)
136+
int equalSignPos = line.indexOf('=');
137+
if (1 > equalSignPos) {
138138
throw new IllegalArgumentException("Wrong header format, may be 'Key=Value' only");
139-
headers.add(new BasicHeader(kv[0].trim(), kv[1].trim()));
139+
}
140+
141+
String headerName = line.substring(0, equalSignPos).trim();
142+
String headerValue = line.substring(1 + equalSignPos).trim();
143+
144+
headers.add(new BasicHeader(headerName, headerValue));
140145
} catch (Throwable t) {
141146
Log.e("SampleParentActivity", "Not a valid header line: " + line, t);
142147
}
143148
}
144149
}
150+
return headers;
151+
}
152+
153+
public Header[] getRequestHeaders() {
154+
List<Header> headers = getRequestHeadersList();
145155
return headers.toArray(new Header[headers.size()]);
146156
}
147157

sample/src/main/java/com/loopj/android/http/sample/WaypointsActivity.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,8 @@ public class WaypointsActivity extends ListActivity {
4646
new SampleConfig(R.string.title_cancel_handle, CancelRequestHandleSample.class),
4747
new SampleConfig(R.string.title_synchronous, SynchronousClientSample.class),
4848
new SampleConfig(R.string.title_intent_service_sample, IntentServiceSample.class),
49-
new SampleConfig(R.string.title_post_files, FilesSample.class)
49+
new SampleConfig(R.string.title_post_files, FilesSample.class),
50+
new SampleConfig(R.string.title_persistent_cookies, PersistentCookiesSample.class)
5051
};
5152

5253
@Override

sample/src/main/res/values/strings.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,4 +24,5 @@
2424
<string name="title_intent_service_sample">IntentService Synchronised Request</string>
2525
<string name="title_post_files">Post Multipart-encoded files</string>
2626
<string name="title_redirect_302">302 Redirect handling</string>
27+
<string name="title_persistent_cookies">Handling persistent cookies</string>
2728
</resources>

0 commit comments

Comments
 (0)