Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Add support for a 'state' parameter on async requests.

This change allows developers to attach an arbitrary object to an asynchronous
Facebook request that will be returned in the request's callback, which
should make it easier to distinguish the returns from concurrent requests.
  • Loading branch information...
commit ab327c68a765415d0c1ae0fc1f472f3a6b6d35c9 1 parent ac14a5f
Jim Brusstar authored
View
3  examples/simple/AndroidManifest.xml
@@ -16,6 +16,5 @@
</activity>
</application>
<uses-permission android:name="android.permission.INTERNET"></uses-permission>
- <uses-sdk android:minSdkVersion="3" android:targetSdkVersion="7"/>
- <supports-screens android:anyDensity="true"/>
+ <uses-sdk android:minSdkVersion="3"/>
</manifest>
View
2  examples/simple/default.properties
@@ -11,4 +11,4 @@
split.density=false
android.library.reference.1=../../facebook/
# Project target.
-target=android-4
+target=android-3
View
10 examples/simple/src/com/facebook/android/BaseRequestListener.java
@@ -15,22 +15,24 @@
*/
public abstract class BaseRequestListener implements RequestListener {
- public void onFacebookError(FacebookError e) {
+ public void onFacebookError(FacebookError e, final Object state) {
Log.e("Facebook", e.getMessage());
e.printStackTrace();
}
- public void onFileNotFoundException(FileNotFoundException e) {
+ public void onFileNotFoundException(FileNotFoundException e,
+ final Object state) {
Log.e("Facebook", e.getMessage());
e.printStackTrace();
}
- public void onIOException(IOException e) {
+ public void onIOException(IOException e, final Object state) {
Log.e("Facebook", e.getMessage());
e.printStackTrace();
}
- public void onMalformedURLException(MalformedURLException e) {
+ public void onMalformedURLException(MalformedURLException e,
+ final Object state) {
Log.e("Facebook", e.getMessage());
e.printStackTrace();
}
View
12 examples/simple/src/com/facebook/android/Example.java
@@ -117,7 +117,7 @@ public void onClick(View v) {
}
mAsyncRunner.request(null, params, "POST",
- new SampleUploadListener());
+ new SampleUploadListener(), null);
}
});
mUploadButton.setVisibility(mFacebook.isSessionValid() ?
@@ -170,7 +170,7 @@ public void onLogoutFinish() {
public class SampleRequestListener extends BaseRequestListener {
- public void onComplete(final String response) {
+ public void onComplete(final String response, final Object state) {
try {
// process the response here: executed in background thread
Log.d("Facebook-Example", "Response: " + response.toString());
@@ -196,7 +196,7 @@ public void run() {
public class SampleUploadListener extends BaseRequestListener {
- public void onComplete(final String response) {
+ public void onComplete(final String response, final Object state) {
try {
// process the response here: (executed in background thread)
Log.d("Facebook-Example", "Response: " + response.toString());
@@ -221,7 +221,7 @@ public void run() {
}
public class WallPostRequestListener extends BaseRequestListener {
- public void onComplete(final String response) {
+ public void onComplete(final String response, final Object state) {
Log.d("Facebook-Example", "Got response: " + response);
String message = "<empty>";
try {
@@ -243,7 +243,7 @@ public void run() {
public class WallPostDeleteListener extends BaseRequestListener {
- public void onComplete(final String response) {
+ public void onComplete(final String response, final Object state) {
if (response.equals("true")) {
Log.d("Facebook-Example", "Successfully deleted wall post");
Example.this.runOnUiThread(new Runnable() {
@@ -268,7 +268,7 @@ public void onComplete(Bundle values) {
mDeleteButton.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
mAsyncRunner.request(postId, new Bundle(), "DELETE",
- new WallPostDeleteListener());
+ new WallPostDeleteListener(), null);
}
});
mDeleteButton.setVisibility(View.VISIBLE);
View
2  examples/simple/src/com/facebook/android/LoginButton.java
@@ -106,7 +106,7 @@ public void onCancel() {
}
private class LogoutRequestListener extends BaseRequestListener {
- public void onComplete(String response) {
+ public void onComplete(String response, final Object state) {
// callback should be run in the original thread,
// not the background thread
mHandler.post(new Runnable() {
View
2  examples/stream/default.properties
@@ -11,4 +11,4 @@
split.density=false
android.library.reference.1=../../facebook/
# Project target.
-target=android-8
+target=android-3
View
16 examples/stream/src/com/facebook/stream/AsyncRequestListener.java
@@ -31,10 +31,10 @@
abstract class AsyncRequestListener implements RequestListener {
- public void onComplete(String response) {
+ public void onComplete(String response, final Object state) {
try {
JSONObject obj = Util.parseJson(response);
- onComplete(obj);
+ onComplete(obj, state);
} catch (JSONException e) {
e.printStackTrace();
Log.e("facebook-stream", "JSON Error:" + e.getMessage());
@@ -44,21 +44,23 @@ public void onComplete(String response) {
}
- public abstract void onComplete(JSONObject obj);
+ public abstract void onComplete(JSONObject obj, final Object state);
- public void onFacebookError(FacebookError e) {
+ public void onFacebookError(FacebookError e, final Object state) {
Log.e("stream", "Facebook Error:" + e.getMessage());
}
- public void onFileNotFoundException(FileNotFoundException e) {
+ public void onFileNotFoundException(FileNotFoundException e,
+ final Object state) {
Log.e("stream", "Resource not found:" + e.getMessage());
}
- public void onIOException(IOException e) {
+ public void onIOException(IOException e, final Object state) {
Log.e("stream", "Network Error:" + e.getMessage());
}
- public void onMalformedURLException(MalformedURLException e) {
+ public void onMalformedURLException(MalformedURLException e,
+ final Object state) {
Log.e("stream", "Invalid URL:" + e.getMessage());
}
View
4 examples/stream/src/com/facebook/stream/LoginHandler.java
@@ -96,7 +96,7 @@ public void onComplete(Bundle values) {
*/
new AsyncFacebookRunner(fb).request("/me",
new AsyncRequestListener() {
- public void onComplete(JSONObject obj) {
+ public void onComplete(JSONObject obj, final Object state) {
// save the session data
String uid = obj.optString("id");
String name = obj.optString("name");
@@ -110,7 +110,7 @@ public void run() {
}
});
}
- });
+ }, null);
}
public void onError(DialogError e) {
View
13 examples/stream/src/com/facebook/stream/LogoutHandler.java
@@ -47,26 +47,29 @@ public void go() {
new AsyncFacebookRunner(fb).logout(getActivity(),
new RequestListener() {
- public void onComplete(String response) {
+ public void onComplete(String response, final Object state) {
dispatcher.runHandler("login");
}
- public void onFileNotFoundException(FileNotFoundException error) {
+ public void onFileNotFoundException(FileNotFoundException error,
+ final Object state) {
Log.e("app", error.toString());
dispatcher.runHandler("login");
}
- public void onIOException(IOException error) {
+ public void onIOException(IOException error, final Object state) {
Log.e("app", error.toString());
dispatcher.runHandler("login");
}
- public void onMalformedURLException(MalformedURLException error) {
+ public void onMalformedURLException(MalformedURLException error,
+ final Object state) {
Log.e("app", error.toString());
dispatcher.runHandler("login");
}
- public void onFacebookError(FacebookError error) {
+ public void onFacebookError(FacebookError error,
+ final Object state) {
Log.e("app", error.toString());
dispatcher.runHandler("login");
}
View
12 examples/stream/src/com/facebook/stream/StreamHandler.java
@@ -70,7 +70,7 @@ public void go() {
public class StreamRequestListener implements RequestListener {
- public void onComplete(String response) {
+ public void onComplete(String response, final Object state) {
try {
JSONObject obj = Util.parseJson(response);
// try to cache the result
@@ -96,19 +96,21 @@ public void run() {
}
}
- public void onFacebookError(FacebookError e) {
+ public void onFacebookError(FacebookError e, final Object state) {
Log.e("stream", "Facebook Error:" + e.getMessage());
}
- public void onFileNotFoundException(FileNotFoundException e) {
+ public void onFileNotFoundException(FileNotFoundException e,
+ final Object state) {
Log.e("stream", "Resource not found:" + e.getMessage());
}
- public void onIOException(IOException e) {
+ public void onIOException(IOException e, final Object state) {
Log.e("stream", "Network Error:" + e.getMessage());
}
- public void onMalformedURLException(MalformedURLException e) {
+ public void onMalformedURLException(MalformedURLException e,
+ final Object state) {
Log.e("stream", "Invalid URL:" + e.getMessage());
}
View
12 examples/stream/src/com/facebook/stream/StreamJsHandler.java
@@ -65,7 +65,7 @@ public void updateStatus(final String message) {
params.putString("message", message);
fb.request("me/feed", params, "POST", new AsyncRequestListener() {
- public void onComplete(JSONObject obj) {
+ public void onComplete(JSONObject obj, final Object state) {
String html;
try {
html = renderStatus(obj, message);
@@ -76,7 +76,7 @@ public void onComplete(JSONObject obj) {
e.printStackTrace();
}
}
- });
+ }, null);
}
/**
@@ -131,10 +131,10 @@ public void like(final String post_id, final boolean val) {
getFb().request(post_id + "/likes", new Bundle(), "POST",
new AsyncRequestListener() {
- public void onComplete(JSONObject response) {
+ public void onComplete(JSONObject response, final Object state) {
callJs("javascript:onLike('" + post_id + "'," + val + ")");
}
- });
+ }, null);
}
@@ -145,7 +145,7 @@ public void postComment(final String post_id, final String message) {
getFb().request(post_id + "/comments", params, "POST",
new AsyncRequestListener() {
- public void onComplete(JSONObject response) {
+ public void onComplete(JSONObject response, final Object state) {
try {
String html = renderComment(response, message);
@@ -156,7 +156,7 @@ public void onComplete(JSONObject response) {
}
}
- });
+ }, null);
}
/**
View
2  facebook/default.properties
@@ -9,4 +9,4 @@
android.library=true
# Project target.
-target=android-8
+target=android-3
View
97 facebook/src/com/facebook/android/AsyncFacebookRunner.java
@@ -40,8 +40,9 @@
* @see RequestListener
* The callback interface.
*
- * @author ssoneff@facebook.com
- *
+ * @author Jim Brusstar (jimbru@fb.com),
+ * Yariv Sadan (yariv@fb.com),
+ * Luke Shepard (lshepard@fb.com)
*/
public class AsyncFacebookRunner {
@@ -68,29 +69,39 @@ public AsyncFacebookRunner(Facebook fb) {
* @param listener
* Callback interface to notify the application when the request
* has completed.
+ * @param state
+ * An arbitrary object used to identify the request when it
+ * returns to the callback. This has no effect on the request
+ * itself.
*/
- public void logout(final Context context, final RequestListener listener) {
+ public void logout(final Context context,
+ final RequestListener listener,
+ final Object state) {
new Thread() {
@Override public void run() {
try {
String response = fb.logout(context);
if (response.length() == 0 || response.equals("false")){
listener.onFacebookError(new FacebookError(
- "auth.expireSession failed"));
+ "auth.expireSession failed"), state);
return;
}
- listener.onComplete(response);
+ listener.onComplete(response, state);
} catch (FileNotFoundException e) {
- listener.onFileNotFoundException(e);
+ listener.onFileNotFoundException(e, state);
} catch (MalformedURLException e) {
- listener.onMalformedURLException(e);
+ listener.onMalformedURLException(e, state);
} catch (IOException e) {
- listener.onIOException(e);
+ listener.onIOException(e, state);
}
}
}.start();
}
+ public void logout(final Context context, final RequestListener listener) {
+ logout(context, listener, /* state */ null);
+ }
+
/**
* Make a request to Facebook's old (pre-graph) API with the given
* parameters. One of the parameter keys must be "method" and its value
@@ -115,10 +126,19 @@ public void logout(final Context context, final RequestListener listener) {
* @param listener
* Callback interface to notify the application when the request
* has completed.
+ * @param state
+ * An arbitrary object used to identify the request when it
+ * returns to the callback. This has no effect on the request
+ * itself.
*/
public void request(Bundle parameters,
- RequestListener listener) {
- request(null, parameters, "GET", listener);
+ RequestListener listener,
+ final Object state) {
+ request(null, parameters, "GET", listener, state);
+ }
+
+ public void request(Bundle parameters, RequestListener listener) {
+ request(null, parameters, "GET", listener, /* state */ null);
}
/**
@@ -137,10 +157,19 @@ public void request(Bundle parameters,
* @param listener
* Callback interface to notify the application when the request
* has completed.
+ * @param state
+ * An arbitrary object used to identify the request when it
+ * returns to the callback. This has no effect on the request
+ * itself.
*/
public void request(String graphPath,
- RequestListener listener) {
- request(graphPath, new Bundle(), "GET", listener);
+ RequestListener listener,
+ final Object state) {
+ request(graphPath, new Bundle(), "GET", listener, state);
+ }
+
+ public void request(String graphPath, RequestListener listener) {
+ request(graphPath, new Bundle(), "GET", listener, /* state */ null);
}
/**
@@ -165,11 +194,22 @@ public void request(String graphPath,
* @param listener
* Callback interface to notify the application when the request
* has completed.
+ * @param state
+ * An arbitrary object used to identify the request when it
+ * returns to the callback. This has no effect on the request
+ * itself.
*/
public void request(String graphPath,
Bundle parameters,
+ RequestListener listener,
+ final Object state) {
+ request(graphPath, parameters, "GET", listener, state);
+ }
+
+ public void request(String graphPath,
+ Bundle parameters,
RequestListener listener) {
- request(graphPath, parameters, "GET", listener);
+ request(graphPath, parameters, "GET", listener, /* state */ null);
}
/**
@@ -197,22 +237,27 @@ public void request(String graphPath,
* @param listener
* Callback interface to notify the application when the request
* has completed.
+ * @param state
+ * An arbitrary object used to identify the request when it
+ * returns to the callback. This has no effect on the request
+ * itself.
*/
public void request(final String graphPath,
final Bundle parameters,
final String httpMethod,
- final RequestListener listener) {
+ final RequestListener listener,
+ final Object state) {
new Thread() {
@Override public void run() {
try {
String resp = fb.request(graphPath, parameters, httpMethod);
- listener.onComplete(resp);
+ listener.onComplete(resp, state);
} catch (FileNotFoundException e) {
- listener.onFileNotFoundException(e);
+ listener.onFileNotFoundException(e, state);
} catch (MalformedURLException e) {
- listener.onMalformedURLException(e);
+ listener.onMalformedURLException(e, state);
} catch (IOException e) {
- listener.onIOException(e);
+ listener.onIOException(e, state);
}
}
}.start();
@@ -220,6 +265,10 @@ public void request(final String graphPath,
/**
* Callback interface for API requests.
+ *
+ * Each method includes a 'state' parameter that identifies the calling
+ * request. It will be set to the value passed when originally calling the
+ * request method, or null if none was passed.
*/
public static interface RequestListener {
@@ -228,14 +277,14 @@ public void request(final String graphPath,
*
* Executed by a background thread: do not update the UI in this method.
*/
- public void onComplete(String response);
+ public void onComplete(String response, Object state);
/**
* Called when a request has a network or request error.
*
* Executed by a background thread: do not update the UI in this method.
*/
- public void onIOException(IOException e);
+ public void onIOException(IOException e, Object state);
/**
* Called when a request fails because the requested resource is
@@ -243,7 +292,8 @@ public void request(final String graphPath,
*
* Executed by a background thread: do not update the UI in this method.
*/
- public void onFileNotFoundException(FileNotFoundException e);
+ public void onFileNotFoundException(FileNotFoundException e,
+ Object state);
/**
* Called if an invalid graph path is provided (which may result in a
@@ -251,14 +301,15 @@ public void request(final String graphPath,
*
* Executed by a background thread: do not update the UI in this method.
*/
- public void onMalformedURLException(MalformedURLException e);
+ public void onMalformedURLException(MalformedURLException e,
+ Object state);
/**
* Called when the server-side Facebook method fails.
*
* Executed by a background thread: do not update the UI in this method.
*/
- public void onFacebookError(FacebookError e);
+ public void onFacebookError(FacebookError e, Object state);
}
View
2  tests/default.properties
@@ -9,4 +9,4 @@
android.library.reference.1=../facebook/
# Project target.
-target=android-8
+target=android-3
View
12 tests/src/com/facebook/android/tests/Tests.java
@@ -454,7 +454,7 @@ public void onFacebookError(FacebookError e) {
public class TestPostRequestListener implements RequestListener {
- public void onComplete(final String response) {
+ public void onComplete(final String response, final Object state) {
Log.d("Tests", "Got response: " + response);
try {
JSONObject json = Util.parseJson(response);
@@ -494,19 +494,21 @@ public void run() {
}
}
- public void onFacebookError(FacebookError e) {
+ public void onFacebookError(FacebookError e, final Object state) {
e.printStackTrace();
}
- public void onFileNotFoundException(FileNotFoundException e) {
+ public void onFileNotFoundException(FileNotFoundException e,
+ final Object state) {
e.printStackTrace();
}
- public void onIOException(IOException e) {
+ public void onIOException(IOException e, final Object state) {
e.printStackTrace();
}
- public void onMalformedURLException(MalformedURLException e) {
+ public void onMalformedURLException(MalformedURLException e,
+ final Object state) {
e.printStackTrace();
}
}
Please sign in to comment.
Something went wrong with that request. Please try again.