安卓/ Facebook的:墙上张贴消息 [英] Android/Facebook: Post message on wall

查看:131
本文介绍了安卓/ Facebook的:墙上张贴消息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想张贴在自己的墙上的消息通过Android应用程序。我有方法loginToFacebook()登录。在点击一个按钮,如果我要张贴留言用户登录。我并不真正熟悉Facebook的API,所以我看Facebook的开发者文档和其他网站在互联网上提出的code后面。然而,没有消息被张贴在墙上。很显然,我错过了什么,但我不知道我有没有错误在我的LogCat中。

I would like to post a message on the my own wall via an Android application. I have the method loginToFacebook() to login. On click on a button, if the user is logged in I want a message to be posted. I am not really familiar with facebook API so I've looked the facebook developers documentation and other sites on internet to propose the code that follows. Yet, no message is posted on my wall. Obviously I miss something but I don't know what as I have no error on my LogCat.

public class ShareActivity extends Activity implements OnClickListener{
    Facebook facebook = new Facebook("477110419013909");
    AsyncFacebookRunner mAsyncRunner = new AsyncFacebookRunner(facebook);
    String FILENAME = "AndroidSSO_data";
    SharedPreferences mPrefs;


  public void onClick(View arg0) {
        loginToFacebook();
        if (facebook.isSessionValid()) {
            Bundle bundle = new Bundle();
            bundle.putString("message","Hey Facebook!");
            try {
                String strRet = facebook.request("/me/feed",bundle,"POST");
            } catch (FileNotFoundException e) {
                // TODO Auto-generated catch block
                Log.e("Facebook", "Error: " + e.getMessage());
            } catch (MalformedURLException e) {
                // TODO Auto-generated catch block
                Log.e("Facebook", "Error: " + e.getMessage());
            } catch (IOException e) {
                // TODO Auto-generated catch block
                Log.e("Facebook", "Error: " + e.getMessage());
            }

}

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);
    }

    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();
                    }

                    @Override
                    public void onError(DialogError error) {
                        // Function to handle error

                    }

                    @Override
                    public void onFacebookError(FacebookError fberror) {
                        // Function to handle Facebook errors

                    }

                });
    }
}
}

我试图做

if (facebook.isSessionValid()) {
            Bundle bundle = new Bundle();
            bundle.putString("message","Hey Facebook!");

            facebook.dialog((Activity) this, "feed", bundle,
                    new DialogListener() {

                        public void onComplete(Bundle values) {
                        }

                        public void onFacebookError(FacebookError error) {}
                        public void onError(DialogError e) {}
                        public void onCancel() {}
            });
 }

也不能工作。

Did not work either

推荐答案

我使用的图像后本次活动类:

I have use this activity class for post image :

public class PostStatusFBActivity extends Activity {
 private static String[] PERMISSIONS = 
            new String[] {"photo_upload","publish_stream"};

Bundle parems;
//String status;
private AsyncFacebookRunner mAsyncRunner;
public final static String FB_APP_ID = "xxxxxxxx"; 
private Facebook fb;
private Handler mHandler;
//private ProgressDialog mProgressDialog;
//private static String app_secret = "51a4b033e6cdbddf5761101d3eb09881";

/**
 * @see android.app.Activity#onCreate(Bundle)
 */
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    mHandler = new Handler();

    Intent intent=getIntent();
    parems=intent.getBundleExtra("params");
    fb= new Facebook(PostStatusFBActivity.FB_APP_ID);

    SessionStore.restore(fb, this);

    if (fb.isSessionValid()) 
        post_status();
    else
        fb.authorize(this, PERMISSIONS,new AppLoginListener());

}

private class AppLoginListener implements DialogListener {

    public AppLoginListener() {

    }

    public void onCancel() {

        Toast.makeText(getApplicationContext(), "Please accept to login", Toast.LENGTH_LONG).show();
        finish();
    }

    public void onComplete(Bundle values) {
        /**
         * We request the user's info so we can cache it locally and
         * use it to render the new html snippets
         * when the user updates her status or comments on a post. 
         */

        mAsyncRunner=new AsyncFacebookRunner(fb);
        post_status();
    }

    public void onError(DialogError e) {
        Log.e("BTS", "dialog error: " , e);               
    }

    public void onFacebookError(FacebookError e) {
        Log.e("BTS ", "facebook error: " , e);
        Toast.makeText(getApplicationContext(), "Facebook error", Toast.LENGTH_LONG).show();
        finish();
    }
}




protected void onActivityResult(int requestCode, int resultCode,Intent data) {
    //Facebook fb = Session.wakeupForAuthCallback();
    fb.authorizeCallback(requestCode, resultCode, data);
}



public void post_status() {

    Log.e("BTS ____  ", " Post satus called ");
    mAsyncRunner.request("me/photos", parems,"POST",new mRequestListener(),null);
}

public class mRequestListener implements RequestListener{

    @Override
    public void onMalformedURLException(MalformedURLException e, Object state) {
        Log.d("BTS", "******************* FACEBOOK::onMalformedURLException *******************");
        e.printStackTrace();
        finish();
    }

    @Override
    public void onIOException(IOException e, Object state) {
        Log.d("BTS", "******************* FACEBOOK::onIOException *******************");
        e.printStackTrace();
        finish();
    }

    @Override
    public void onFileNotFoundException(FileNotFoundException e, Object state) {
        Log.d("BTS", "******************* FACEBOOK::onFileNotFoundException *******************");
        e.printStackTrace();
        Toast.makeText(getApplicationContext(), "Image not found", Toast.LENGTH_LONG).show();
        finish();
    }

    @Override
    public void onFacebookError(FacebookError e, Object state) {
        Log.d("BTS", "******************* FACEBOOK::onFacebookError *******************");
        e.printStackTrace();
        Toast.makeText(getApplicationContext(), "Facebook error", Toast.LENGTH_LONG).show();
        finish();
    }

    String msg="";

    @Override
    public void onComplete(String response, Object state) {
        Log.d("BTS", "******************* FACEBOOK::onComplete *******************");

        // TODO Auto-generated method stub
        Log.d("Facebook-Example",
                "News feed: " + response.toString());
        JSONObject json;

        /*JSONObject json = Util.parseJson(response);
            final String pictureURL = json.getString("picture");
            if (TextUtils.isEmpty(pictureURL)) {
         * */
        try {
            json = Util.parseJson(response);
            Log.d("Facebook-Example",
                    "News feed: " + json.toString(2));

             final String post_id = json.getString("post_id");
             if (TextUtils.isEmpty(post_id)) {
                msg="Error in update status";
             }else{
                 msg="Status updated";
             }
             mHandler.post(new Runnable() {
                    @Override
                    public void run() {
                        Toast.makeText(getApplicationContext(), msg, Toast.LENGTH_LONG).show();
                        //mProgressDialog.dismiss();
                        finish();
                    }
             });

        } catch (JSONException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (FacebookError e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

    }

}

}

这是我通过从previous活动捆绑PARAMS 字节[]数据= NULL;
捆绑PARAMS =新包();
            params.putByteArray(照片,数据);             params.putString(标题,状态);

And here is Bundle params that I pass from previous Activity byte[] data = null;
Bundle params = new Bundle();
params.putByteArray("photo", data); params.putString("caption", status);

这是你可以把捆绑其他值 params.putString(信息,此字符串将显示为状态信息);         params.putString(链接,这是URL去);         params.putString(名,这将图片旁出现了);         params.putString(标题,这将出现在标题);         params.putString(说明,这将出现在标题下);         params.putString(图片,这是形象出现在后);

Other values that you can put on 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");

这篇关于安卓/ Facebook的:墙上张贴消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆