我创建了一个应用程序,在其中我创建了一个EditText和一个按钮。我想在点击按钮后发布在Facebook好友墙上的EditText上写的那条消息。请告诉我如何使用Facebook sdk执行此任务。
代码如下:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.setContentView(R.layout.post_wall);
share = (Button) findViewById(R.id.share);
friend_name = (TextView) findViewById(R.id.wall_to);
wall = (EditText) findViewById(R.id.wall);
savedInstanceState = new Bundle();
savedInstanceState.getString("to");
onComplete(savedInstanceState);
}
@Override public void onComplete(Bundle values)
{
Utility.currentPermissions.clear();
if (values.isEmpty())
{
//"skip" clicked ?
return;
}
// if facebookClient.authorize(...) was successful, this runs
// this also runs after successful post
// after posting, "to"(which is the id of friend) is added to the values bundle
// I use that to differentiate between a call from
// faceBook.authorize(...) and a call from a successful post
// is there a better way of doing this?
if (!values.containsKey("to"))
{
try
{
Log.d("Wall try", "Click successfully");
for (String key : parameters.keySet()) {
if (parameters.getByteArray(key) != null) {
parameters.putByteArray(key, parameters.getByteArray(key));
Log.d("key", parameters.getByteArray(key).toString());
}
}
mHandler.post(new Runnable() {
@Override
public void run() {
performActivityInfo();
}
});
}
catch (Exception e)
{
// TODO: handle exception
System.out.println(e.getMessage());
}
}
}
protected void performActivityInfo() {
Log.d("perform wall", "Perform Activity");
mHandler.sendEmptyMessage(FRIEND_WALL);
parameters.putString("message", wall.getText().toString());
facebookClient.dialog(this, "stream.publish", parameters, this);// "stream.publish" is an API call
Log.d("Wall post", "Click successfully");
}
public Handler mHandler = new Handler() {
@Override
public void handleMessage(Message msg) {
switch (msg.what) {
case FRIEND_WALL:
Log.d("Handler WALL", "Handler");
postOnWall(wall.getText().toString());
break;
}
super.handleMessage(msg);
}
};
@Override
public void onError(DialogError e)
{
System.out.println("Error: " + e.getMessage());
}
@Override
public void onFacebookError(FacebookError e)
{
System.out.println("Error: " + e.getMessage());
}
@Override
public void onCancel()
{
}
@Override
public void onClick(View v)
{
facebookClient = new Facebook(APP_ID);
// replace APP_API_ID with your own
Log.d("Wall click", "Click successfully");
facebookClient.authorize(this,
new String[] {"publish_stream", "read_stream", "offline_access"}, this);
}
public void postOnWall(String msg) {
Log.d("Tests", "Testing graph API wall post");
try {
String response = facebookClient.request("me");
Bundle parameters = new Bundle();
parameters.putString("message", msg);
parameters.putString("description", "test test test");
response = facebookClient.request("me/feed", parameters,
"POST");
Log.d("Tests", "got response: " + response);
if (response == null || response.equals("") ||
response.equals("false")) {
Log.v("Error", "Blank response");
}
} catch(Exception e) {
e.printStackTrace();
}
}
先谢谢。
答案 0 :(得分:2)
要从编辑文本中获取值,只需使用:
EditText edittext;
edittext.getEditableText().toString();
内部按钮单击侦听器然后使用此代码
button.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View arg0) {
String entered_value=edittext.getEditableText().toString();
try{
parameters.putString("message", entered_value);
parameters.putString("target_id", "XXXXX"); // target Id in which you need to Post
parameters.putString("method", "stream.publish");
String response = authenticatedFacebook.request(parameters);
Log.v("response", response);
}
catch(Exception e){}
}
});
其中button是您的按钮对象。