从android fb sdk发布消息到Facebook墙总是错误

时间:2010-09-16 12:01:13

标签: android facebook facebook-wall

我对Android的fb sdk有疑问(从http://github.com/facebook/facebook-android-sdk下载)。试图发布墙但总是得到错误(权限已经设置并登录到fb)

这是代码片段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());

从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$2.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$2.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$2.run(AsyncFacebookRunner.java:209)

3 个答案:

答案 0 :(得分:47)

修复方法是:

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

而不是

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

在Util.java的第63行。

并且

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

而不是

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

在Util.java的第155行。

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

答案 1 :(得分:10)

这真的很奇怪......我有一个使用相同语法的应用程序,但效果非常好。我只是查看了FB SDK的源代码,它似乎已经发生了很大变化...... I found this on the SDK src

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

所以,你试着这样做:

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

答案 2 :(得分:0)

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

Bundle params中键“picture”的值应该是字节数组,而不是字符串。

编辑:没读过克里斯蒂安的答案。我很确定你应该传递实际的图像数据,而不是字节的文件名。但我可能是错的。

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