在我的postToFacebook()
方法中。我检查了用户是否已连接,如果它是我在墙上发布的链接。当用户未登录时,会弹出 facebook sdk 登录进度,我使用我的凭据成功登录。我的问题是登录后我需要postonWall
直接但postOnWall
没有发布到 Facebook。如果我已经登录并且我发布了,那么它就会被发布。请提供任何帮助。这是我完整的 postToFacebook 代码。
public void postToFacebook(){
mAsyncRunner = new AsyncFacebookRunner(facebook);
loginToFacebook();
}
/**
* Function to login into facebook
* */
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);
/*btnFbLogin.setVisibility(View.INVISIBLE);
// Making get profile button visible
btnFbGetProfile.setVisibility(View.VISIBLE);
// Making post to wall visible
btnPostToWall.setVisibility(View.VISIBLE);
// Making show access tokens button visible
btnShowAccessTokens.setVisibility(View.VISIBLE);*/
new loadingTask().execute();
System.out.println("one");
Log.d("FB Sessions", "" + facebook.isSessionValid());
}
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();
// Making Login button invisible
/*btnFbLogin.setVisibility(View.INVISIBLE);
// Making logout Button visible
btnFbGetProfile.setVisibility(View.VISIBLE);
// Making post to wall visible
btnPostToWall.setVisibility(View.VISIBLE);
// Making show access tokens button visible
btnShowAccessTokens.setVisibility(View.VISIBLE);*/
new loadingTask().execute();
System.out.println("two");
}
@Override
public void onError(DialogError error) {
// Function to handle error
}
@Override
public void onFacebookError(FacebookError fberror) {
// Function to handle Facebook errors
}
});
}
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
facebook.authorizeCallback(requestCode, resultCode, data);
}
/**
* Function to show Access Tokens
* */
public void showAccessTokens() {
String access_token = facebook.getAccessToken();
Toast.makeText(getApplicationContext(),
"Access Token: " + access_token, Toast.LENGTH_LONG).show();
}
/**
* Function to Logout user 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) {
runOnUiThread(new Runnable() {
@Override
public void run() {
// make Login button visible
/*btnFbLogin.setVisibility(View.VISIBLE);
// making all remaining buttons invisible
btnFbGetProfile.setVisibility(View.INVISIBLE);
btnPostToWall.setVisibility(View.INVISIBLE);
btnShowAccessTokens.setVisibility(View.INVISIBLE);*/
}
});
}
}
@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) {
}
});
}
@SuppressWarnings("deprecation")
public void postOWall(String msg) {
Bundle params = new Bundle();
params.putString("link", "http://google.com");
mAsyncRunner.request("me/feed", params, "POST", new RequestListener() {
@Override
public void onComplete(String response, Object state) {
// TODO Auto-generated method stub
Log.i("onComplete", "complete");
}
@Override
public void onIOException(IOException e, Object state) {
// TODO Auto-generated method stub
e.printStackTrace();
Log.i("onComplete", "onIOException");
}
@Override
public void onFileNotFoundException(FileNotFoundException e,
Object state) {
// TODO Auto-generated method stub
e.printStackTrace();
Log.i("onComplete", "onFileNotFoundException");
}
@Override
public void onMalformedURLException(MalformedURLException e,
Object state) {
// TODO Auto-generated method stub
e.printStackTrace();
Log.i("onComplete", "onMalformedURLException");
}
@Override
public void onFacebookError(FacebookError e, Object state) {
// TODO Auto-generated method stub
e.printStackTrace();
Log.i("onComplete", "onFacebookError");
}
// try {
// // String response = facebook.request("me");
// Bundle parameters = new Bundle();
// //parameters.putString("caption", "Test Caption");
// parameters.putString("link", "http://www.google.com");
// // parameters.putString("caption", "Test Caption: http://google.com/");
// // parameters.putString("method", "stream.publish");
//// response = facebook.request("me/feed", parameters,
//// "POST");
// //Utility.mAsyncRunner.request("me", params, new UserRequestListener());
// // Util.mAsyncRunner.request("me", parameters, new UserRequestListener());
// mAsyncRunner.request("me/feed", parameters, "POST", new UserRequestListener(),null);
//// Log.d("Tests", "got response: " + response);
//// if (response == null || response.equals("") ||
//// response.equals("false")) {
//// Log.v("Error", "Blank response");
//// }
// } catch(Exception e) {
// e.printStackTrace();
// }
}, params);
}
public void postOnWall(String link) {
try {
String response = facebook.request("me");
Bundle parameters = new Bundle();
//parameters.putString("caption", "Test Caption");
System.out.println("response link: "+link);
parameters.putString("link", link);
// parameters.putString("caption", "Test Caption: http://google.com/");
// parameters.putString("method", "stream.publish");
response = facebook.request("me/feed", parameters,
"POST");
FacebookSuccessMessage.title="";
FacebookSuccessMessage.mesg="Posté sur Facebook";
FacebookSuccessMessage.button_text="OK";
Log.d("Tests", "got response: " + response);
if (response!=null){
Intent i = new Intent (getApplicationContext(), FacebookSuccessMessage.class);
startActivity(i);
}
if (response == null || response.equals("") ||
response.equals("false")) {
Log.v("Error", "Blank response");
}
} catch(Exception e) {
e.printStackTrace();
}
}
class loadingTask extends AsyncTask<Void, Void,Void> {
@Override
protected void onPreExecute() {
// TODO Auto-generated method stub
super.onPreExecute();
}
@Override
protected void onPostExecute(Void result) {
// TODO Auto-generated method stub
super.onPostExecute(result);
}
@Override
protected Void doInBackground(Void... params) {
// TODO Auto-generated method stub
System.out.println("response c : "+ constants);
postOnWall(constants);
return null;
}
}