问题
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? :)
回答1:
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.
回答2:
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.
回答3:
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) {
}
});
来源:https://stackoverflow.com/questions/13780833/posting-a-text-status-using-facebook-api-in-android-the-simplest-method