使用图形API中的Facebook墙上张贴不工作 [英] Posting in Facebook wall using graph API not Working

查看:135
本文介绍了使用图形API中的Facebook墙上张贴不工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想用图形API,但它不是为我工作后的东西的Facebook墙上。
它有没有错误,但它也没有张贴在墙上什么。

I'm trying to post something to Facebook wall using graph API but its not working for me. It has no error but it also not posting anything on my wall.

下面是codeS:

public class ActivityName extends Activity{


    Dialog dialog;
    Facebook fb;
    SharedPreferences sp;
    String access_token, name, email;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.success);

            String APP_ID = "MY_ID";
            fb = new Facebook(APP_ID);
            sp = getPreferences(MODE_PRIVATE);
            access_token = sp.getString("access_token", null);
            long expires = sp.getLong("access_expires", 0);

            if(access_token!= null){
                fb.setAccessToken(access_token);
            }

            if(expires != 0){
                fb.setAccessExpires(expires);
            }

        ((Button) findViewById(R.id.btn_home)).setOnClickListener(new View.OnClickListener() {

            public void onClick(View v) {
                // TODO Auto-generated method stub
                openContextMenu(v);

            }
        });

        ((Button) findViewById(R.id.btn_home)).setOnCreateContextMenuListener(new View.OnCreateContextMenuListener() {

            public void onCreateContextMenu(ContextMenu menu, View v,
                    ContextMenuInfo menuInfo) {
                // TODO Auto-generated method stub
                menu.setHeaderTitle("Tell the world!");
                menu.add(0, 0, 0, "Facebook");
                menu.add(0, 1, 0, "Twitter");
                menu.add(0, 2, 0, "Skip");
            }
        });

    }

    public boolean onContextItemSelected(MenuItem item) {
            AdapterContextMenuInfo info = (AdapterContextMenuInfo) item
            .getMenuInfo();

            switch (item.getItemId()) {
            case 1:



                break;

            case 0:

                 fb.authorize(SuccessActivity.this, new String[]{ "photo_upload,user_photos,publish_checkins,publish_actions,publish_stream"},new DialogListener() {
                        public void onComplete(Bundle values) {
                            postToWall();
                        }

                        public void onFacebookError(FacebookError error) {
                        }

                        public void onError(DialogError e) {
                        }

                        public void onCancel() {
                        }
                    });



                break;


            case 2:
                Intent i = new Intent(getApplicationContext(),MainActivity.class);
                startActivity(i);
                overridePendingTransition(android.R.anim.slide_in_left, android.R.anim.slide_out_right);


                break;
            }
            return true;
            }

    public void onBackPressed() {

   }

    @SuppressLint("SdCardPath")
    public void postToWall() {
        // post on user's wall.
         try {
                if (fb.isSessionValid()) {
                    byte[] data = null;

                     Bitmap bi = BitmapFactory.decodeFile("/sdcard/Asa.jpg");
                     ByteArrayOutputStream baos = new ByteArrayOutputStream();
                     bi.compress(Bitmap.CompressFormat.JPEG, 100, baos);
                     data = baos.toByteArray();


                     Bundle params = new Bundle();
                     params.putString(Facebook.TOKEN, fb.getAccessToken());
                     params.putString("message", "Test from Android AVD");
                     params.putByteArray("picture", data);



                     AsyncFacebookRunner mAsyncRunner = new AsyncFacebookRunner(fb);
                     mAsyncRunner.request("/me/feed", params,"POST", new SampleUploadListener(),null);

                }
            }catch(Exception e){
                e.printStackTrace();
            }

    }

    public class SampleUploadListener extends BaseRequestListener {

        public void onComplete(final String response, final Object state) {
            try {
                // process the response here: (executed in background thread)
                Log.d("Facebook-Example", "Response: " + response.toString());
                JSONObject json = Util.parseJson(response);
                final String src = json.getString("src");

                // then post the processed result back to the UI thread
                // if we do not do this, an runtime exception will be generated
                // e.g. "CalledFromWrongThreadException: Only the original
                // thread that created a view hierarchy can touch its views."

            } catch (JSONException e) {
                Log.w("Facebook-Example", "JSON Error in response");
            } catch (FacebookError e) {
                Log.w("Facebook-Example", "Facebook Error: " + e.getMessage());
            }
        }

        @Override
        public void onFacebookError(FacebookError e, Object state) {    
        }
    }

}

谁能品脱什么遗漏或错误在这个codeS?
任何想法将是非常美联社preciated。

Can anyone pint what missing or wrong in this codes? any thoughts will be highly appreciated.

推荐答案

下面是一些code我用了一小会儿回来做类似的事情:

Here's some code I used a little while back to do something similar:

function publishPost(session) {
var $title = $('#title').val(),
    $story = $('#story').val(),
    publish = {
        method: 'stream.publish',
        message: 'I just entered the Your Perfect Kent Day Competition.',
        picture : 'http://apps.facebook.com/perfect-kent-day/',
        link : 'http://apps.facebook.com/perfect-kent-day/',
        name: 'Enter to win or vote for your faourite story here!',
        caption: $title,
        description: $story,
        actions : { name : 'Apply for Kent Teach Jobs...', link : 'http://www.kent-       teach.com/'}
    };

    FB.api('/me/feed', 'POST', publish, function(response) {
    //alert('A post had just been published into the stream on your wall.');
        $('#before-submit').hide();
        $('#after-submit').show();
        $('#loading').fadeOut();
    });
};

然后我叫publishPost()函数一旦网页上的表单与AJAX提交。希望这有助于!

And then I called the publishPost() function once a form on the page was submitted with AJAX. Hope this help!

这篇关于使用图形API中的Facebook墙上张贴不工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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