I am building an app where I am trying to integrate facebook. When I am using device of android 2.3.3 its working fine and i am able to post message. But when I am running that same app in android 4.1 then it showing "failed to post".I am using facebook-sdk version 3.6.0
My code as below
FacebookActivity.java
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.facebook);
btnPostStatus = (Button) findViewById(R.id.mBtnPostStatus);
edtPost = (EditText) findViewById(R.id.post_text);
edtPost.setText(messageToPost);
btnPostStatus.setOnClickListener(new OnClickListener() {
@SuppressWarnings("deprecation")
public void onClick(View arg0) {
facebook = new Facebook(APP_ID);
restoreCredentials(facebook);
messageToPost = edtPost.getText().toString();
if (!facebook.isSessionValid()) {
loginAndPostToWall();
} else {
postToWall(messageToPost);
}
}
});
}
@SuppressWarnings("deprecation")
public boolean saveCredentials(Facebook facebook) {
Editor editor = getApplicationContext().getSharedPreferences(KEY,
Context.MODE_PRIVATE).edit();
editor.putString(TOKEN, facebook.getAccessToken());
editor.putLong(EXPIRES, facebook.getAccessExpires());
return editor.commit();
}
@SuppressWarnings("deprecation")
public boolean restoreCredentials(Facebook facebook) {
SharedPreferences sharedPreferences = getApplicationContext()
.getSharedPreferences(KEY, Context.MODE_PRIVATE);
facebook.setAccessToken(sharedPreferences.getString(TOKEN, null));
facebook.setAccessExpires(sharedPreferences.getLong(EXPIRES, 0));
return facebook.isSessionValid();
}
@SuppressWarnings("deprecation")
public void loginAndPostToWall() {
facebook.authorize(this, PERMISSIONS, Facebook.FORCE_DIALOG_AUTH,
new LoginDialogListener());
}
@SuppressWarnings("deprecation")
public void postToWall(String message) {
Bundle parameters = new Bundle();
parameters.putString("message", message);
parameters.putString("description", "topic share");
try {
facebook.request("me");
String response = facebook.request("me/feed", parameters, "POST");
Log.d("Tests", "got response: " + response);
if (response == null || response.equals("")
|| response.equals("false")) {
showToast("Blank response.");
} else {
showToast("Message posted to your facebook wall!");
}
} catch (Exception e) {
showToast("Failed to post to wall!");
e.printStackTrace();
}
}
class LoginDialogListener implements DialogListener {
public void onComplete(Bundle values) {
saveCredentials(facebook);
if (messageToPost != null) {
postToWall(messageToPost);
}
}
public void onFacebookError(FacebookError error) {
showToast("Authentication with Facebook failed!");
}
public void onError(DialogError error) {
showToast("Authentication with Facebook failed!");
}
public void onCancel() {
showToast("Authentication with Facebook cancelled!");
}
}
private void showToast(String message) {
Toast.makeText(getApplicationContext(), message, Toast.LENGTH_SHORT)
.show();
}
}
It not showing any exception, but not able post
Is there any good fb integration tutorial which using latest facebook sdk version ?
Actually I solved my problem. When I am setting a target-sdk version in my manifest.xml then its working for lower version android device but its not working for my jelly bean android device. So when I remove target-sdk version then its working properly in jelly bean device also .
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.