Permalink
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...
1 parent ac14a5f commit ab327c68a765415d0c1ae0fc1f472f3a6b6d35c9 Jim Brusstar committed Jan 18, 2011
@@ -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>
@@ -11,4 +11,4 @@
split.density=false
android.library.reference.1=../../facebook/
# Project target.
-target=android-4
+target=android-3
@@ -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();
}
@@ -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);
@@ -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() {
@@ -11,4 +11,4 @@
split.density=false
android.library.reference.1=../../facebook/
# Project target.
-target=android-8
+target=android-3
@@ -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());
}
@@ -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) {
@@ -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");
}
@@ -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());
}
@@ -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);
}
/**
@@ -9,4 +9,4 @@
android.library=true
# Project target.
-target=android-8
+target=android-3
Oops, something went wrong.

0 comments on commit ab327c6

Please sign in to comment.