Skip to content

Commit ee71123

Browse files
committed
More javadoc documentation for classes
1 parent ffc03f3 commit ee71123

File tree

8 files changed

+204
-73
lines changed

8 files changed

+204
-73
lines changed

build.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@
5454
<!-- Clean out the build files -->
5555
<target name="clean">
5656
<delete dir="build" />
57+
<delete dir="doc" />
5758
<delete>
5859
<fileset dir="." includes="${jarfile}"/>
5960
<fileset file="MANIFEST.MF"/>

src/com/loopj/android/http/AsyncHttpResponseHandler.java

Lines changed: 61 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,9 @@ public class AsyncHttpResponseHandler {
4949

5050
private Handler handler;
5151

52+
/**
53+
* Creates a new AsyncHttpResponseHandler
54+
*/
5255
public AsyncHttpResponseHandler() {
5356
// Set up a handler to post events back to the correct thread if possible
5457
if(Looper.myLooper() != null) {
@@ -61,44 +64,58 @@ public void handleMessage(Message msg){
6164
}
6265

6366

64-
// Pre-processing of messages (in background thread)
65-
public void sendResponseMessage(HttpResponse response) {
66-
StatusLine status = response.getStatusLine();
67-
if(status.getStatusCode() >= 300) {
68-
sendFailureMessage(new HttpResponseException(status.getStatusCode(), status.getReasonPhrase()));
69-
} else {
70-
try {
71-
HttpEntity entity = null;
72-
HttpEntity temp = response.getEntity();
73-
if(temp != null) {
74-
entity = new BufferedHttpEntity(temp);
75-
}
67+
//
68+
// Callbacks to be overridden, typically anonymously
69+
//
70+
71+
/**
72+
* Fired when the request is started, override to handle in your own code
73+
*/
74+
public void onStart() {}
75+
76+
/**
77+
* Fired in all cases when the request is finished, after both success and failure, override to handle in your own code
78+
*/
79+
public void onFinish() {}
80+
81+
/**
82+
* Fired when a request returns successfully, override to handle in your own code
83+
* @param content the body of the HTTP response from the server
84+
*/
85+
public void onSuccess(String content) {}
86+
87+
/**
88+
* Fired when a request fails to complete, override to handle in your own code
89+
* @param error the underlying cause of the failure
90+
*/
91+
public void onFailure(Throwable error) {}
7692

77-
sendSuccessMessage(EntityUtils.toString(entity));
78-
} catch(IOException e) {
79-
sendFailureMessage(e);
80-
}
81-
}
82-
}
8393

84-
public void sendSuccessMessage(String responseBody) {
94+
//
95+
// Pre-processing of messages (executes in background threadpool thread)
96+
//
97+
98+
protected void sendSuccessMessage(String responseBody) {
8599
sendMessage(obtainMessage(SUCCESS_MESSAGE, responseBody));
86100
}
87101

88-
public void sendFailureMessage(Throwable e) {
102+
protected void sendFailureMessage(Throwable e) {
89103
sendMessage(obtainMessage(FAILURE_MESSAGE, e));
90104
}
91105

92-
public void sendStartMessage() {
106+
protected void sendStartMessage() {
93107
sendMessage(obtainMessage(START_MESSAGE, null));
94108
}
95109

96-
public void sendFinishMessage() {
110+
protected void sendFinishMessage() {
97111
sendMessage(obtainMessage(FINISH_MESSAGE, null));
98112
}
99113

100114

101-
// Pre-processing of messages (in original calling thread)
115+
//
116+
// Pre-processing of messages (in original calling thread, typically the UI thread)
117+
//
118+
102119
protected void handleSuccessMessage(String responseBody) {
103120
onSuccess(responseBody);
104121
}
@@ -108,7 +125,8 @@ protected void handleFailureMessage(Throwable e) {
108125
}
109126

110127

111-
// Utility functions
128+
129+
// Methods which emulate android's Handler and Message methods
112130
protected void handleMessage(Message msg) {
113131
switch(msg.what) {
114132
case SUCCESS_MESSAGE:
@@ -147,9 +165,23 @@ protected Message obtainMessage(int responseMessage, Object response) {
147165
}
148166

149167

150-
// Public callbacks
151-
public void onStart() {}
152-
public void onFinish() {}
153-
public void onSuccess(String content) {}
154-
public void onFailure(Throwable error) {}
168+
// Interface to AsyncHttpRequest
169+
void sendResponseMessage(HttpResponse response) {
170+
StatusLine status = response.getStatusLine();
171+
if(status.getStatusCode() >= 300) {
172+
sendFailureMessage(new HttpResponseException(status.getStatusCode(), status.getReasonPhrase()));
173+
} else {
174+
try {
175+
HttpEntity entity = null;
176+
HttpEntity temp = response.getEntity();
177+
if(temp != null) {
178+
entity = new BufferedHttpEntity(temp);
179+
}
180+
181+
sendSuccessMessage(EntityUtils.toString(entity));
182+
} catch(IOException e) {
183+
sendFailureMessage(e);
184+
}
185+
}
186+
}
155187
}

src/com/loopj/android/http/JsonHttpResponseHandler.java

Lines changed: 37 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,44 @@
2323
import org.json.JSONObject;
2424
import org.json.JSONTokener;
2525

26+
/**
27+
* Used to intercept and handle the responses from requests made using
28+
* {@link AsyncHttpClient}, with automatic parsing into a {@link JSONObject}
29+
* or {@link JSONArray}.
30+
* <p>
31+
* This class is designed to be passed to get, post, put and delete requests
32+
* with the {@link #onSuccess(JSONObject)} or {@link #onSuccess(JSONArray)}
33+
* methods anonymously overridden.
34+
* <p>
35+
* Additionally, you can override the other event methods from the
36+
* parent class.
37+
*/
2638
public class JsonHttpResponseHandler extends AsyncHttpResponseHandler {
39+
//
40+
// Callbacks to be overridden, typically anonymously
41+
//
42+
43+
/**
44+
* Fired when a request returns successfully and contains a json object
45+
* at the base of the response string. Override to handle in your
46+
* own code.
47+
* @param response the parsed json object found in the server response (if any)
48+
*/
49+
public void onSuccess(JSONObject response) {}
50+
51+
52+
/**
53+
* Fired when a request returns successfully and contains a json array
54+
* at the base of the response string. Override to handle in your
55+
* own code.
56+
* @param response the parsed json array found in the server response (if any)
57+
*/
58+
public void onSuccess(JSONArray response) {}
59+
60+
61+
// Utility methods
2762
@Override
28-
public void handleSuccessMessage(String responseBody) {
63+
protected void handleSuccessMessage(String responseBody) {
2964
super.handleSuccessMessage(responseBody);
3065

3166
try {
@@ -39,12 +74,8 @@ public void handleSuccessMessage(String responseBody) {
3974
onFailure(e);
4075
}
4176
}
42-
77+
4378
protected Object parseResponse(String responseBody) throws JSONException {
4479
return new JSONTokener(responseBody).nextValue();
4580
}
46-
47-
// Public callbacks
48-
public void onSuccess(JSONObject response) {}
49-
public void onSuccess(JSONArray response) {}
5081
}

src/com/loopj/android/http/PersistentCookieStore.java

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,16 @@
3434
import android.content.SharedPreferences;
3535
import android.text.TextUtils;
3636

37+
/**
38+
* A persistent cookie store which implements the Apache HttpClient
39+
* {@link CookieStore} interface. Cookies are stored and will persist on the
40+
* user's device between application sessions since they are serialized and
41+
* stored in {@link SharedPreferences}.
42+
* <p>
43+
* Instances of this class are designed to be used with
44+
* {@link AsyncHttpClient#setCookieStore}, but can also be used with a
45+
* regular old apache HttpClient/HttpContext if you prefer.
46+
*/
3747
public class PersistentCookieStore implements CookieStore {
3848
private static final String COOKIE_PREFS = "CookiePrefsFile";
3949
private static final String COOKIE_NAME_STORE = "names";
@@ -42,6 +52,9 @@ public class PersistentCookieStore implements CookieStore {
4252
private ConcurrentHashMap<String, Cookie> cookies;
4353
private SharedPreferences cookiePrefs;
4454

55+
/**
56+
* Construct a persistent cookie store.
57+
*/
4558
public PersistentCookieStore(Context context) {
4659
cookiePrefs = context.getSharedPreferences(COOKIE_PREFS, 0);
4760
cookies = new ConcurrentHashMap<String, Cookie>();
@@ -65,6 +78,7 @@ public PersistentCookieStore(Context context) {
6578
}
6679
}
6780

81+
@Override
6882
public void addCookie(Cookie cookie) {
6983
String name = cookie.getName();
7084

@@ -78,6 +92,7 @@ public void addCookie(Cookie cookie) {
7892
prefsWriter.commit();
7993
}
8094

95+
@Override
8196
public void clear() {
8297
// Clear cookies from local store
8398
cookies.clear();
@@ -91,6 +106,7 @@ public void clear() {
91106
prefsWriter.commit();
92107
}
93108

109+
@Override
94110
public boolean clearExpired(Date date) {
95111
boolean clearedAny = false;
96112
SharedPreferences.Editor prefsWriter = cookiePrefs.edit();
@@ -119,14 +135,16 @@ public boolean clearExpired(Date date) {
119135
return clearedAny;
120136
}
121137

122-
public Cookie getCookie(String name) {
123-
return cookies.get(name);
124-
}
125-
138+
@Override
126139
public List<Cookie> getCookies() {
127140
return new ArrayList<Cookie>(cookies.values());
128141
}
129142

143+
144+
//
145+
// Cookie serialization/deserialization
146+
//
147+
130148
protected String encodeCookie(SerializableCookie cookie) {
131149
ByteArrayOutputStream os = new ByteArrayOutputStream();
132150
try {

0 commit comments

Comments
 (0)