java 安卓 Facebook 图谱 API

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5173552/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 09:49:18  来源:igfitidea点击:

Android Facebook Graph API

javaandroidfacebookfacebook-graph-api

提问by whirlwin

Has anyone got an example of how to use the Android Graph API?

有没有人有如何使用 Android Graph API 的例子?

I am stuck with the basics, like posting text to the wall on Facebook.

我坚持基础知识,比如在 Facebook 上的墙上发布文本。

I'm using the Facebook SDK for Android. Here is what I have so far:

我正在使用适用于 Android 的 Facebook SDK。这是我到目前为止所拥有的:

    Bundle params = new Bundle();
    params.putString("message", "picture caption");

    AsyncFacebookRunner mAsyncRunner = new AsyncFacebookRunner(facebook);
    mAsyncRunner.request("me/feed", params, "POST", new RequestListener() {

        @Override
        public void onMalformedURLException(MalformedURLException e, Object state) {
            // TODO Auto-generated method stub

        }

        @Override
        public void onIOException(IOException e, Object state) {
            // TODO Auto-generated method stub

        }

        @Override
        public void onFileNotFoundException(FileNotFoundException e, Object state) {
            // TODO Auto-generated method stub

        }

        @Override
        public void onFacebookError(FacebookError e, Object state) {
            // TODO Auto-generated method stub

        }

        @Override
        public void onComplete(String response, Object state) {
            // TODO Auto-generated method stub

        }
    }, "foo");

Nothing happens, and my Logcat says:

什么也没发生,我的 Logcat 说:

03-02 22:10:02.973: WARN/Bundle(1930): Key message expected byte[] but value was a java.lang.String.  The default value <null> was returned.
    03-02 22:10:02.983: WARN/Bundle(1930): Attempt to cast generated internal exception:
    03-02 22:10:02.983: WARN/Bundle(1930): java.lang.ClassCastException: java.lang.String
    03-02 22:10:02.983: WARN/Bundle(1930):     at android.os.Bundle.getByteArray(Bundle.java:1305)
    03-02 22:10:02.983: WARN/Bundle(1930):     at com.facebook.android.Util.encodePostBody(Util.java:63)
    03-02 22:10:02.983: WARN/Bundle(1930):     at com.facebook.android.Util.openUrl(Util.java:182)
    03-02 22:10:02.983: WARN/Bundle(1930):     at com.facebook.android.Facebook.request(Facebook.java:559)
    03-02 22:10:02.983: WARN/Bundle(1930):     at com.facebook.android.AsyncFacebookRunner.run(AsyncFacebookRunner.java:253)

回答by sandeep

try this

试试这个

Bundle params = new Bundle();
params.putString(Facebook.TOKEN, token);   
params.putString("message", "graph api");       
mAsyncRunner.request("/me/feed", params,"POST", new SampleUploadListener(),null);

you can get access token by using

您可以通过使用获取访问令牌

      token = mFacebook.getAccessToken();

回答by ashish

You can do this like :-- give description in bundle message and if u want to share some image from server u can provide link as i given below

您可以这样做:-- 在捆绑消息中提供描述,如果您想从服务器共享一些图像,您可以提供如下所示的链接

 Bundle params = new Bundle();
        params.putString("message", "description");
        params.putString("link","your image url" );

new GraphRequest( AccessToken.getCurrentAccessToken(),
            "me/feed",
            params,
            HttpMethod.POST,
            new GraphRequest.Callback() {
                @Override
                public void onCompleted(GraphResponse response) {
                    Log.d(TAG, "" + response.toString());
                    try {
                        JSONObject jsonObject = new JSONObject(response.getRawResponse());
                        if (jsonObject != null) {
                            String postId = jsonObject.getString("id");
                            if (postId != null && !postId.equalsIgnoreCase("")) {
                                hideProgressDialog();
                                Log.d("postId", "" + postId);
                            } else {
                                hideProgressDialog();
                                Utils.showToast(getActivity(), getResources().getString(R.string.txt_try_again));
                            }
                        } else {
                            hideProgressDialog();
                            Utils.showToast(getActivity(), getResources().getString(R.string.txt_try_again));
                        }

                    } catch (JSONException e) {
                        hideProgressDialog();
                        e.printStackTrace();
                    } catch (Throwable e) {
                        hideProgressDialog();
                        e.printStackTrace();
                    }

                }
            }).executeAsync();