I followed this tutorial to implement facebook into my application. All I want is to post a text on the user’s wall. I imported all the necessary files like RyanM said here.
Problem: when I do not provide an app id, facebook starts loading, then says an error occurred. Then I click OKAY in the top-right corner and facebook loads. Why?
There is another button for posting on wall. I do not want any buttons for this, where should I put the postOnWall("Testing from Android"); line? I want the post to appear on the users wall right after they log in.
When I provide my app id, facebook starts loading then exits. Why?
PostActivity:
package com.bfarago.af2;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ProgressBar;
import android.widget.TextView;
public class PostActivity extends FacebookActivity {
/** Called when the activity is first created. */
private TextView txtUserName;
private ProgressBar pbLogin;
private Button btnLogin;
private Button btnWall;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
txtUserName = (TextView) findViewById(R.id.textFacebook);
pbLogin = (ProgressBar) findViewById(R.id.progressLogin);
btnLogin = (Button) findViewById(R.id.buttonLogin);
btnWall = (Button) findViewById(R.id.buttonWall);
btnLogin.setOnClickListener(listener);
btnWall.setOnClickListener(listener1);
}
OnClickListener listener = new OnClickListener(){
@Override
public void onClick(View v) {
// pbLogin.setVisibility(ProgressBar.VISIBLE);
setConnection();
getID(txtUserName, pbLogin);
postOnWall("Testing from Android");
}
};
OnClickListener listener1 = new OnClickListener(){
@Override
public void onClick(View v) {
// postOnWall("Testing from Android");
}
};
}
FacebookActivity:
package com.bfarago.af2;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.net.MalformedURLException;
import org.json.JSONException;
import org.json.JSONObject;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ProgressBar;
import android.widget.TextView;
import com.facebook.android.AsyncFacebookRunner;
import com.facebook.android.AsyncFacebookRunner.RequestListener;
import com.facebook.android.DialogError;
import com.facebook.android.Facebook;
import com.facebook.android.Facebook.DialogListener;
import com.facebook.android.FacebookError;
import com.facebook.android.Util;
public abstract class FacebookActivity extends Activity {
public static final String TAG = "FACEBOOK";
private Facebook mFacebook;
public static final String APP_ID = "394196520626466"; //the API Key for your Facebook APPs
private AsyncFacebookRunner mAsyncRunner;
private static final String[] PERMS = new String[] { "publish_stream" };
private SharedPreferences sharedPrefs;
private Context mContext;
private TextView username;
private ProgressBar pb;
public void setConnection() {
mContext = this;
mFacebook = new Facebook(APP_ID);
mAsyncRunner = new AsyncFacebookRunner(mFacebook);
}
public void getID(TextView txtUserName, ProgressBar progbar) {
username = txtUserName;
pb = progbar;
if (isSession()) {
Log.d(TAG, "sessionValid");
mAsyncRunner.request("me", new IDRequestListener());
} else {
// no logged in, so relogin
Log.d(TAG, "sessionNOTValid, relogin");
mFacebook.authorize(this, PERMS, new LoginDialogListener());
}
}
public boolean isSession() {
sharedPrefs = PreferenceManager.getDefaultSharedPreferences(mContext);
String access_token = sharedPrefs.getString("access_token", "x");
Long expires = sharedPrefs.getLong("access_expires", -1);
Log.d(TAG, access_token);
if (access_token != null && expires != -1) {
mFacebook.setAccessToken(access_token);
mFacebook.setAccessExpires(expires);
}
return mFacebook.isSessionValid();
}
private class LoginDialogListener implements DialogListener {
@Override
public void onComplete(Bundle values) {
Log.d(TAG, "LoginONComplete");
String token = mFacebook.getAccessToken();
long token_expires = mFacebook.getAccessExpires();
Log.d(TAG, "AccessToken: " + token);
Log.d(TAG, "AccessExpires: " + token_expires);
sharedPrefs = PreferenceManager
.getDefaultSharedPreferences(mContext);
sharedPrefs.edit().putLong("access_expires", token_expires)
.commit();
sharedPrefs.edit().putString("access_token", token).commit();
mAsyncRunner.request("me", new IDRequestListener());
}
@Override
public void onFacebookError(FacebookError e) {
Log.d(TAG, "FacebookError: " + e.getMessage());
}
@Override
public void onError(DialogError e) {
Log.d(TAG, "Error: " + e.getMessage());
}
@Override
public void onCancel() {
Log.d(TAG, "OnCancel");
}
}
private class IDRequestListener implements RequestListener {
@Override
public void onComplete(String response, Object state) {
try {
Log.d(TAG, "IDRequestONComplete");
Log.d(TAG, "Response: " + response.toString());
JSONObject json = Util.parseJson(response);
final String id = json.getString("id");
final String name = json.getString("name");
FacebookActivity.this.runOnUiThread(new Runnable() {
public void run() {
username.setText("Welcome: " + name+"\n ID: "+id);
pb.setVisibility(ProgressBar.GONE);
}
});
} catch (JSONException e) {
Log.d(TAG, "JSONException: " + e.getMessage());
} catch (FacebookError e) {
Log.d(TAG, "FacebookError: " + e.getMessage());
}
}
@Override
public void onIOException(IOException e, Object state) {
Log.d(TAG, "IOException: " + e.getMessage());
}
@Override
public void onFileNotFoundException(FileNotFoundException e,
Object state) {
Log.d(TAG, "FileNotFoundException: " + e.getMessage());
}
@Override
public void onMalformedURLException(MalformedURLException e,
Object state) {
Log.d(TAG, "MalformedURLException: " + e.getMessage());
}
@Override
public void onFacebookError(FacebookError e, Object state) {
Log.d(TAG, "FacebookError: " + e.getMessage());
}
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
mFacebook.authorizeCallback(requestCode, resultCode, data);
}
public void postOnWall(String msg) {
Log.d("Tests graph API %%%%%$$$$%%%", msg);
try {
String response = mFacebook.request("me");
Bundle parameters = new Bundle();
parameters.putString("message", msg);
parameters.putString("description", "test test test");
response = mFacebook.request("me/feed", parameters,
"POST");
Log.d("Tests", "got response: " + response);
if (response == null || response.equals("") ||
response.equals("false")) {
Log.v("Error", "Blank response");
}
} catch(Exception e) {
e.printStackTrace();
}
}
}
What I want:

Here’s what happens with your code:
User clicks any button which calls the
OnClickListener.onClickmethod.This method calls the
setConnectionmethod of the activity which constructs the facebook objects and return.Then the
getIDmethod is invoked which checks if the user is authorized or not.If the user is authorized you make an asynchronous request for
/me, and then the method returns which immediately callspostOnWall.The problem is that the request to
/me/feedis sent before the asynchronous request returns with the data.If the user is not authorized then you call
mFacebook.authorize, and here in theonCompletemethod you again make an asynchronous request for/mebut don’t wait for it to complete before callingpostOnWall.In both cases you need to wait for the asynchronous request to complete, something like:
And (notice that I move the
postOnWallinvocation to theonCompletehere):