I am using facebook defaul dialog to share post on my facebook wall . My code is given below .
facebook.dialog(this, "feed", new DialogListener() {
#Override
public void onFacebookError(FacebookError e) {
}
#Override
public void onError(DialogError e) {
}
#Override
public void onComplete(Bundle values) {
}
#Override
public void onCancel() {
}
});
But now I am adding a TextView and a Button , and after clicking on button the textView text will be posted on my facebook wall.
Override onComplete() in your DialogListener then do what you want or get the value using getText from the textview and post it.
public void onComplete(Bundle values) {
mProgress.setMessage("Posting ...");
mProgress.show();
AsyncFacebookRunner mAsyncFbRunner = new AsyncFacebookRunner(mFacebook);
Bundle params = new Bundle();
params.putString("message", "A Game Developed By Me !");
params.putString("name", "Match Maker");
params.putString("caption", "www.labmimosa.com/");
params.putString("link", "https://play.google.com/store/apps/details?id=com.reverie.fushh");
params.putString("description", "Dexter: Blood. Sometimes it sets my teeth on edge, other times it helps me control the chaos.");
params.putString("picture", "http://twitpic.com/show/thumb/6hqd44");
//params.putByteArray("picture", bitMapData);
mAsyncFbRunner.request("me/feed", params, "POST", new WallPostListener());
}
Wallpostlistener class is
private final class WallPostListener extends BaseRequestListener {
public void onComplete(final String response) {
mRunOnUi.post(new Runnable() {
#Override
public void run() {
mProgress.cancel();
Toast.makeText(Exp_Fb_Twt_Activity.this, "Posted to Facebook", Toast.LENGTH_SHORT).show();
}
});
}
}
Related
//i got this error sometimes when using the feed dialog on facebook :com.facebook.android.DialogError: net::ERR_SPDY_PROTOCOL_ERROR
// sometime it works and sometimes it won't(loading appears the nothing happens) error is return on dialogerror function
here is the code :
Bundle parameters1 = new Bundle();
System.out.println("link>>>>"+srcLnk);
System.out.println("picture>>>>"+imagesrc);
System.out.println("name>>>>"+titlev);
System.out.println("description>>>>"+Html.fromHtml(url.toString()).toString());
parameters1.putString("link",srcLnk);
parameters1.putString("picture",imagesrc);
parameters1.putString("name",titlev);
parameters1.putString("caption","www.alsumaria.tv");
// parameters1.putString("description",url.toString().replaceAll("\\n", ""));
parameters1.putString("description",Html.fromHtml(url.toString()).toString());
facebook.dialog(activity,"feed",parameters1,
new DialogListener() {
#Override
public void onComplete(Bundle values) {
Toast.makeText(activity, "Posted successfully", Toast.LENGTH_SHORT).show();
}
#Override
public void onFacebookError(FacebookError error) {
error.printStackTrace();
}
#Override
public void onError(DialogError e) {
e.printStackTrace();
}
#Override
public void onCancel() {}
}
);
I have been searching a lot to find a way to do this. But nothing seems to be working for me. Can someone please help in doing this?
This is my image button for facebook status post:
<ImageButton
android:id="#+id/imageButton1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignRight="#+id/hoributtons"
android:layout_alignTop="#+id/imageButton2"
android:background="#00000000"
android:contentDescription="#string/facebook"
android:onClick="shareOnFacebook"
android:src="#drawable/facebookbutton" />
This is my mainactivity.java file's corresponding part:
public class MainActivity extends FacebookActivity {
private static final String APP_ID = "xxxxxxxxxxxxx";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void shareOnFacebook(View v) {
//mfacebook = new Facebook("xxxxxxxxxxxxx");
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
}
Can someone point in the right direction? :)
Assuming that you want to post on your own wall (since the question is not clear), this should work for you.
public class MainActivity extends Activity {
private static final String APP_ID = "xxxxxxxxxxxxx";
private Facebook mFacebook;
private AsyncFacebookRunner mAsyncRunner;
private EditText yourEditText;
private String toShare;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mFacebook = new Facebook();
mAsyncRunner = new AsyncFacebookRunner(mFacebook);
SessionEvents.addAuthListener(new SampleAuthListener());
SessionEvents.addLogoutListener(new SampleLogoutListener());
yourEditText = (EditText) findViewById(R.id.<youreditTextId>);
toShare = yourEditText.getText().toString();
}
public void shareOnFacebook(View v) {
Bundle params = new Bundle();
params.putString("message", toShare);
mAsyncRunner.request("me/feed", params, "POST", new RequestListener() {
public void onMalformedURLException(MalformedURLException e) {}
public void onIOException(IOException e) {}
public void onFileNotFoundException(FileNotFoundException e) {}
public void onFacebookError(FacebookError e) {}
public void onComplete(String response) {
}
});
Toast.makeText(MainActivity.this, "Posting to your Wall", Toast.LENGTH_SHORT).show();
}
}
For details on posting pictures to wall, Facebook has a good documentation.
The simplest way to post message on user`s wall who is successfully login using your android application is(my working code)_
public class AndroidFacebookWallPost extends Activity {
// Your Facebook APP ID:
private static String APP_ID = "your App ID here"; //
// Instance of Facebook Class:
private Facebook facebook;
private AsyncFacebookRunner mAsyncRunner;
// Your ImageButton that Post Message to Facebook Wall:
ImageButton btnPostToWall;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
btnPostToWall = (ImageButton) findViewById(R.id.imageButton1);// Your image button...
facebook = new Facebook(APP_ID);
mAsyncRunner = new AsyncFacebookRunner(facebook);
// set listener for Post Message button
btnPostToWall.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
postToWall();
}
});
}
/**
* Method that Post a Text Status using Facebook API on user`s wall.
*/
public void postToWall() {
// post on user's wall.
facebook.dialog(this, "feed", new DialogListener() {
#Override
public void onFacebookError(FacebookError error) {
Toast.makeText(AndroidFacebookWallPost.this, "Post fail "+error, Toast.LENGTH_LONG).show();
}
#Override
public void onError(DialogError error) {
Toast.makeText(AndroidFacebookWallPost.this, "Post fail due to "+error, Toast.LENGTH_LONG).show();
}
#Override
public void onComplete(Bundle values) {
Toast.makeText(AndroidFacebookWallPost.this, "Post success.", Toast.LENGTH_LONG).show();
}
#Override
public void onCancel() {
Toast.makeText(AndroidFacebookWallPost.this, "Cancle by user!", Toast.LENGTH_LONG).show();
}
});
}
}
for more please gone through Getting Started with the Facebook SDK for Android.
Here is details of variable that you can out in bundle
Bundle params = new Bundle();
params.putString("message", "This string will appear as the status message");
params.putString("link", "This is the URL to go to");
params.putString("name", "This will appear beside the picture");
params.putString("caption", "This will appear under the title");
params.putString("description", "This will appear under the caption");
params.putString("picture", "This is the image to appear in the post");
And use AsyncFacebookRunner for post data to facebook :
mAsyncRunner.request("me/feed", params, "POST", new RequestListener() {
public void onMalformedURLException(MalformedURLException e) {}
public void onIOException(IOException e) {}
public void onFileNotFoundException(FileNotFoundException e) {}
public void onFacebookError(FacebookError e) {}
public void onComplete(String response) {
}
});
I followed this tutorial http://www.androidhive.info/2012/03/android-facebook-connect-tutorial/ to connect Facebook to my Android application. Instead of having many buttons, I have a button that will be used for both login and logout.
1) The first time I run the class, I could login and then logout successfully. And when I click on the same button to login again, the login page would not appear, however the Toast text "LOGGING IN" appear, which is after loginToFacebook() function in my if-else. Hence, I assume, it should have run the facebook login page like the first time I run the class. But the login page does not appear.
What did I do wrong? And what should I do?
2) And how do I display the username in String fbLoggedIn after logged in instead of the text "CONNECTED!!" ?
public class FacebookActivity extends Activity{
private static String APP_ID = "";
private Facebook facebook;
private AsyncFacebookRunner mAsyncRunner;
String FILENAME = "AndroidSSO_data";
private SharedPreferences mPrefs;
private Button backButton;
private String name = "CONNECTED!!";
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.settings_share);
facebook = new Facebook(APP_ID);
mAsyncRunner = new AsyncFacebookRunner(facebook);
RelativeLayout fbButton = (RelativeLayout) findViewById(R.id.fbLayout);
fbButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
if (!facebook.isSessionValid())
{
System.out.println("Not Connected. Clicked and Login.");
loginToFacebook();
Toast.makeText(getApplicationContext(), "LOGGING IN", Toast.LENGTH_LONG).show();}
else
{
System.out.println("Connected. Logged Out.");
logoutFromFacebook();
Toast.makeText(getApplicationContext(), "LOGGED OUT", Toast.LENGTH_LONG).show();}
}
});
}
public void loginToFacebook() {
mPrefs = getPreferences(MODE_PRIVATE);
String access_token = mPrefs.getString("access_token", null);
long expires = mPrefs.getLong("access_expires", 0);
if (access_token != null) {
facebook.setAccessToken(access_token);
}
if (expires != 0) {
facebook.setAccessExpires(expires);
}
if (!facebook.isSessionValid()) {
facebook.authorize(this, new String[] { "email", "publish_stream" }, new DialogListener() {
#Override
public void onCancel() {
// Function to handle cancel event
}
#Override
public void onComplete(Bundle values) {
// Function to handle complete event
// Edit Preferences and update facebook acess_token
SharedPreferences.Editor editor = mPrefs.edit();
editor.putString("access_token", facebook.getAccessToken());
editor.putLong("access_expires", facebook.getAccessExpires());
editor.commit();
TextView fbUser = (TextView) findViewById(R.id.fbUser);
fbUser.setVisibility(View.VISIBLE);
String fbLoggedIn = name;
fbUser.setText(fbLoggedIn);
Toast.makeText(getApplicationContext(), "LOGGED IN AS " + name, Toast.LENGTH_LONG).show();
}
#Override
public void onError(DialogError error) {
// Function to handle error
}
#Override
public void onFacebookError(FacebookError fberror) {
// Function to handle Facebook errors
}
});
}
}
public void getProfileInformation() {
mAsyncRunner.request("me", new RequestListener() {
#Override
public void onComplete(String response, Object state) {
Log.d("Profile", response);
String json = response;
try {
JSONObject profile = new JSONObject(json);
// getting name of the user
final String name = profile.getString("name");
// getting email of the user
final String email = profile.getString("email");
runOnUiThread(new Runnable() {
#Override
public void run() {
Toast.makeText(getApplicationContext(), "Name: " + name + "\nEmail: " + email, Toast.LENGTH_LONG).show();
}
});
} catch (JSONException e) {
e.printStackTrace();
}
}
#Override
public void onIOException(IOException e, Object state) { }
#Override
public void onFileNotFoundException(FileNotFoundException e, Object state) { }
#Override
public void onMalformedURLException(MalformedURLException e, Object state) { }
#Override
public void onFacebookError(FacebookError e, Object state) { }
});
}
//logout from Facebook
public void logoutFromFacebook() {
mAsyncRunner.logout(this, new RequestListener() {
#Override
public void onComplete(String response, Object state) {
Log.d("Logout from Facebook", response);
if (Boolean.parseBoolean(response) == true) {
// User successfully Logged out
}
}
#Override
public void onIOException(IOException e, Object state) { }
#Override
public void onFileNotFoundException(FileNotFoundException e, Object state) { }
#Override
public void onMalformedURLException(MalformedURLException e, Object state) { }
#Override
public void onFacebookError(FacebookError e, Object state) { }
});
}
Just to be clearer, I set my if-else like this (extracted from the complete codes above).
RelativeLayout fbButton = (RelativeLayout) findViewById(R.id.fbLayout);
fbButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
if (!facebook.isSessionValid())
{
System.out.println("Not Connected. Clicked and Login.");
loginToFacebook();
Toast.makeText(getApplicationContext(), "LOGGING IN", Toast.LENGTH_LONG).show();}
else
{
System.out.println("Connected. Logged Out.");
logoutFromFacebook();
Toast.makeText(getApplicationContext(), "LOGGED OUT", Toast.LENGTH_LONG).show();}
}
});
This is the behavior that Facebook wants you to take with their API.Your login method checks to see if you have already signed in once and that the facebook session is active. If the session is active then it sets the properties of the facebook object and connects it behind the sences so that the login page does not show again.
So you're not doing anything wrong, it's suppose to happen this way.
I want to get the id from the Facebook User but what I've tried so far doesn't work...
public class fbLogin extends Activity {
public static final int LOGIN = Menu.FIRST;
public static final int GET_EVENTS = Menu.FIRST + 1;
public static final int GET_ID = Menu.FIRST + 2;
public static final String APP_ID = "361579407254212";
public static final String TAG = "FACEBOOK CONNECT";
private Facebook mFacebook;
private AsyncFacebookRunner mAsyncRunner;
private Handler mHandler = new Handler();
private static final String[] PERMS = new String[] { "user_events","email" };
private TextView mText;
/** Called when the activity is first created. */
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
super.onCreate(savedInstanceState);
// setup the content view
initLayout();
// setup the facebook session
mFacebook = new Facebook(APP_ID);
mAsyncRunner = new AsyncFacebookRunner(mFacebook);
if (mFacebook.isSessionValid()) {
AsyncFacebookRunner asyncRunner = new AsyncFacebookRunner(mFacebook);
asyncRunner.logout(this, new LogoutRequestListener());
} else {
mFacebook.authorize(this, PERMS, new LoginDialogListener());
}
mAsyncRunner.request("me", new IDRequestListener());
}
protected void initLayout() {
LinearLayout rootView = new LinearLayout(this.getApplicationContext());
rootView.setOrientation(LinearLayout.VERTICAL);
this.mText = new TextView(this.getApplicationContext());
this.mText.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT));
rootView.addView(this.mText);
this.setContentView(rootView);
}
private class LoginDialogListener implements DialogListener {
public void onComplete(Bundle values) {}
public void onFacebookError(FacebookError e) {}
public void onError(DialogError e) {}
public void onCancel() {}
}
private class LogoutRequestListener implements RequestListener {
public void onComplete(String response, Object state) {
// Dispatch on its own thread
mHandler.post(new Runnable() {
public void run() {
mText.setText("Logged out");
}
});
}
public void onIOException(IOException e, Object state) {}
public void onFileNotFoundException(FileNotFoundException e, Object state) {}
public void onMalformedURLException(MalformedURLException e, Object state) {}
public void onFacebookError(FacebookError e, Object state) {}
}
private class IDRequestListener implements RequestListener {
public void onComplete(String response, Object state) {
try {
// process the response here: executed in background thread
Log.d(TAG, "Response: " + response.toString());
JSONObject json = Util.parseJson(response);
final String id = json.getString("id");
fbLogin.this.runOnUiThread(new Runnable() {
public void run() {
mText.setText("Hello there, " + id + "!");
}
});
} catch (JSONException e) {
Log.w(TAG, "JSON Error in response");
mText.setText("catch1...");
} catch (FacebookError e) {
mText.setText("catch2");
}
}
public void onIOException(IOException e, Object state) {mText.setText("Logging out...1");}
public void onFileNotFoundException(FileNotFoundException e, Object state) {mText.setText("Logging out...2");}
public void onMalformedURLException(MalformedURLException e,Object state) {mText.setText("Logging out...3");}
public void onFacebookError(FacebookError e, Object state) {mText.setText("Logging out...4");}
}
}
It gives a Facebook Error when I go to the OnComplete method from the IDRequestListener class...
How can I fix it? Can anyone help me, please?
You can use socialauth android sdk. Can get user profile, post messages , gets friends list
http://code.google.com/p/socialauth-android/
authorize() is not synchronous and will return immediately. You'll need to wait for the authorization request to complete before submitting requests requiring an access token. Basically, fire the me request only when onComplete() has been called in the authorize() listener.
Also your activity seems to be missing the onActivityResult() implementation that feeds the Facebook object with authorization results, i.e.
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
mFacebook.authorizeCallback(requestCode, resultCode, data);
}
I'm trying to open a facebook post dialog when I click on some element. Dialog itself is working well. I could write some comments in the text field and the post could be seen on the website. However I want to add some info the area below the comment, which contains caption, link, picture and so on. I referred to several tutorials to implement this but they didn't work for me. Say, the caption I entered would not be shown in the dialog. The code is like:
mFacebook = new Facebook(APP_ID);
mAsyncRunner = new AsyncFacebookRunner(mFacebook);
dFbPostButton.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
Bundle b = new Bundle();
b.putString("caption","Check This Hotel");
//Result is the class name
//Nothing too much in SampleDialogListener
mFacebook.dialog(Result.this, "feed", b,new SampleDialogListener());
}
}
Any help would be really appreciated!!
Follow the below code
Bundle params = new Bundle();
params.putString("caption", "Mona Lisa");
params.putString("description","The Mona Lisa...");
params.putString("picture","http://tineye.com/images/widgets/mona.jpg");
params.putString("name", "Mona Lisa");
mFacebook.dialog(this, "feed", params, new DialogListener() {
#Override
public void onComplete(Bundle values) {}
#Override
public void onCancel() {}
#Override
public void onError(DialogError de) {}
#Override
public void onFacebookError(FacebookError fbe) {}
});
it ll help