从android fb sdk向facebook墙发布消息总是错误

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3726429/
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-08-20 11:33:00  来源:igfitidea点击:

Post message to facebook wall from android fb sdk always error

androidfacebookfacebook-wall

提问by Lorensius W. L. T

I have problem with fb sdk for Android (downloaded from http://github.com/facebook/facebook-android-sdk). Tried to post wall but always get error (permission already set and logged in to fb)

我的 fb sdk for Android 有问题(从http://github.com/facebook/facebook-android-sdk下载)。试图贴墙但总是出错(权限已经设置并登录到 fb)

here is the code snippet onClick function, i made small modifications on their sample code:

这是 onClick 函数的代码片段,我对他们的示例代码做了一些小的修改:

Bundle params = new Bundle();

params.putString("message", "Test");
params.putString("name", "American Virgin");
params.putString("link", "http://bit.ly/12345");
params.putString("description", "A Freshman College Girl on a scholarship from an ...");
params.putString("picture", "http://xxx/MOV1026.jpg");

mAsyncRunner.request("me/feed", params, "POST", new TestRequestListener());

From DDMS i get the following error:

从 DDMS 我收到以下错误:

    09-16 18:55:28.372: WARN/Bundle(14392): Key picture expected byte[] but value was a java.lang.String.  The default value <null> was returned.
09-16 18:55:28.414: WARN/Bundle(14392): Attempt to cast generated internal exception:
09-16 18:55:28.414: WARN/Bundle(14392): java.lang.ClassCastException: java.lang.String
09-16 18:55:28.414: WARN/Bundle(14392):     at android.os.Bundle.getByteArray(Bundle.java:1220)
09-16 18:55:28.414: WARN/Bundle(14392):     at com.facebook.android.Util.openUrl(Util.java:153)
09-16 18:55:28.414: WARN/Bundle(14392):     at com.facebook.android.Facebook.request(Facebook.java:295)
09-16 18:55:28.414: WARN/Bundle(14392):     at com.facebook.android.AsyncFacebookRunner.run(AsyncFacebookRunner.java:209)
09-16 18:55:28.422: WARN/Bundle(14392): Key message expected byte[] but value was a java.lang.String.  The default value <null> was returned.
09-16 18:55:28.432: WARN/Bundle(14392): Attempt to cast generated internal exception:
09-16 18:55:28.432: WARN/Bundle(14392): java.lang.ClassCastException: java.lang.String
09-16 18:55:28.432: WARN/Bundle(14392):     at android.os.Bundle.getByteArray(Bundle.java:1220)
09-16 18:55:28.432: WARN/Bundle(14392):     at com.facebook.android.Util.openUrl(Util.java:153)
09-16 18:55:28.432: WARN/Bundle(14392):     at com.facebook.android.Facebook.request(Facebook.java:295)
09-16 18:55:28.432: WARN/Bundle(14392):     at com.facebook.android.AsyncFacebookRunner.run(AsyncFacebookRunner.java:209)
09-16 18:55:28.452: WARN/Bundle(14392): Key format expected byte[] but value was a java.lang.String.  The default value <null> was returned.
09-16 18:55:28.472: WARN/Bundle(14392): Attempt to cast generated internal exception:
09-16 18:55:28.472: WARN/Bundle(14392): java.lang.ClassCastException: java.lang.String
09-16 18:55:28.472: WARN/Bundle(14392):     at android.os.Bundle.getByteArray(Bundle.java:1220)
09-16 18:55:28.472: WARN/Bundle(14392):     at com.facebook.android.Util.openUrl(Util.java:153)
09-16 18:55:28.472: WARN/Bundle(14392):     at com.facebook.android.Facebook.request(Facebook.java:295)
09-16 18:55:28.472: WARN/Bundle(14392):     at com.facebook.android.AsyncFacebookRunner.run(AsyncFacebookRunner.java:209)

采纳答案by Cristian

That's really weird... I have an app which uses the same syntax but it works really well. I just check the source code of the FB SDK and it seems it has changed a lot... I found this on the SDK src:

这真的很奇怪......我有一个使用相同语法的应用程序,但它运行得非常好。我只是检查了 FB SDK 的源代码,它似乎发生了很大变化......我在 SDK src 上找到了这个

    for (String key : params.keySet()) {
        if (params.getByteArray(key) != null) {
                dataparams.putByteArray(key, params.getByteArray(key));
        }
    }

So, you try to do this:

所以,你尝试这样做:

Bundle params = new Bundle();

params.putByteArray("message", "Test".getBytes());
params.putByteArray("name", "American Virgin".getBytes());
params.putByteArray("link", "http://bit.ly/12345".getBytes());
params.putByteArray("description", "A Freshman College Girl on a scholarship from an ...".getBytes());
params.putByteArray("picture", "http://xxx/MOV1026.jpg".getBytes());

mAsyncRunner.request("me/feed", params, "POST", new TestRequestListener());

回答by Chris Banes

The fix is:

修复方法是:

if (parameters.get(key) instanceof byte[]) {

instead of

代替

if (parameters.getByteArray(key) != null) {

on line 63 of Util.java.

在 Util.java 的第 63 行。

And

if (params.get(key) instanceof byte[]) {

instead of

代替

if (params.getByteArray(key) != null) {

on line 155 of Util.java.

在 Util.java 的第 155 行。

For some strange reason, on Samsung Nexus S (perhaps other devices too) it returns a String, not a byte[].

出于某种奇怪的原因,在三星 Nexus S(可能还有其他设备)上,它返回一个字符串,而不是一个字节 []。

回答by benvd

I think the error message is quite clear... "Key picture expected byte[] but value was a java.lang.String."

我认为错误信息很清楚...... "Key picture expected byte[] but value was a java.lang.String."

The value for the key "picture" in your Bundle paramsshould be a byte array, not a String.

Bundle 中关键“图片”的值params应该是一个字节数组,而不是一个字符串。

edit: Didn't read Cristian's answer. I'm pretty sure you should pass along the actual image data, not the filename in bytes. But I could be wrong.

编辑:没有阅读克里斯蒂安的回答。我很确定您应该传递实际的图像数据,而不是以字节为单位的文件名。但我可能是错的。

another edit: Yeah, so I'd downvote my own answer if I could, but it seems I didn't even read the question properly. The error happens not just for picture, so I have no idea what's wrong...

另一个编辑:是的,所以如果可以的话,我会否决我自己的答案,但似乎我什至没有正确阅读问题。错误不仅发生在图片上,所以我不知道出了什么问题......