我必须在用户的Facebook墙上发送一个链接。我正在努力,但没有成功。 我写了代码,但它不起作用。首先我写道:
import java.io.FileNotFoundException;
import java.io.IOException;
import java.net.MalformedURLException;
import com.facebook.android.AsyncFacebookRunner;
import com.facebook.android.DialogError;
import com.facebook.android.Facebook;
import com.facebook.android.FacebookError;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.Toast;
public class FacebookActivity extends Activity {
/** Called when the activity is first created. */
Facebook face;
AsyncFacebookRunner run;
String[] permission=new String[] {"publish_stream",
"read_stream", "offline_access"};
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
face=new Facebook("my key");
run=new AsyncFacebookRunner(face);
face.authorize(this, permission,new MyLoginDialogListener());
}
private final class MyLoginDialogListener implements com.facebook.android
.Facebook.DialogListener {
public void onComplete(Bundle values) {
Toast.makeText(FacebookActivity.this, "Ho Gya Authorize",Toast.LENGTH_LONG).show();
run=new AsyncFacebookRunner(face);
Bundle bundle=new Bundle();
bundle.putString("message", " hello");
bundle.putString("name", "hello");
bundle.putString("description", "Hello");
bundle.putString("link", "http://www.facebook.com");
try {
face.request("me/feed",bundle, "POST");
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} // here enable logout
public void onFacebookError(FacebookError error) {}
public void onError(DialogError error) {}
public void onCancel() {}
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
face.authorizeCallback(requestCode, resultCode, data);
}
}
请建议如何进一步处理。我的代码没有张贴在用户墙上。
答案 0 :(得分:0)
在Facebook上发布墙的示例代码
public class AndroidFacebookSample extends Activity {
private static final String FACEBOOK_APPID = "PUT YOUR FACEBOOK APP ID HERE";
private static final String FACEBOOK_PERMISSION = "publish_stream";
private static final String TAG = "FacebookSample";
private static final String MSG = "Message from FacebookSample";
private final Handler mFacebookHandler = new Handler();
private TextView loginStatus;
private FacebookConnector facebookConnector;
final Runnable mUpdateFacebookNotification = new Runnable() {
public void run() {
Toast.makeText(getBaseContext(), "Facebook updated !", Toast.LENGTH_LONG).show();
}
};
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
this.facebookConnector = new FacebookConnector(FACEBOOK_APPID, this, getApplicationContext(), new String[] {FACEBOOK_PERMISSION});
loginStatus = (TextView)findViewById(R.id.login_status);
Button tweet = (Button) findViewById(R.id.btn_post);
Button clearCredentials = (Button) findViewById(R.id.btn_clear_credentials);
tweet.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
postMessage();
}
});
clearCredentials.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
clearCredentials();
updateLoginStatus();
}
});
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
this.facebookConnector.getFacebook().authorizeCallback(requestCode, resultCode, data);
}
@Override
protected void onResume() {
super.onResume();
updateLoginStatus();
}
public void updateLoginStatus() {
loginStatus.setText("Logged into Twitter : " + facebookConnector.getFacebook().isSessionValid());
}
private String getFacebookMsg() {
return MSG + " at " + new Date().toLocaleString();
}
public void postMessage() {
if (facebookConnector.getFacebook().isSessionValid()) {
postMessageInThread();
} else {
SessionEvents.AuthListener listener = new SessionEvents.AuthListener() {
@Override
public void onAuthSucceed() {
postMessageInThread();
}
@Override
public void onAuthFail(String error) {
}
};
SessionEvents.addAuthListener(listener);
facebookConnector.login();
}
}
private void postMessageInThread() {
Thread t = new Thread() {
public void run() {
try {
facebookConnector.postMessageOnWall(getFacebookMsg());
mFacebookHandler.post(mUpdateFacebookNotification);
} catch (Exception ex) {
Log.e(TAG, "Error sending msg",ex);
}
}
};
t.start();
}
private void clearCredentials() {
try {
facebookConnector.getFacebook().logout(getApplicationContext());
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}