在Facebook墙上显示带有标题的图像显示ClassCastException(无法从字符串转换为byte []

时间:2014-12-12 09:13:23

标签: android facebook

我实现了一个异步任务,用于在Facebook墙上发布带有标题的图像代码工作正常,但现在突然显示以下异常:

12-12 14:18:32.277: W/Bundle(9197): Key access_token expected byte[] but value was a java.lang.String.  The default value <null> was returned.
12-12 14:18:32.280: W/Bundle(9197): Attempt to cast generated internal exception:
12-12 14:18:32.280: W/Bundle(9197): java.lang.ClassCastException: java.lang.String cannot be cast to byte[]
12-12 14:18:32.280: W/Bundle(9197):     at android.os.BaseBundle.getByteArray(BaseBundle.java:1104)
12-12 14:18:32.280: W/Bundle(9197):     at android.os.Bundle.getByteArray(Bundle.java:959)
12-12 14:18:32.280: W/Bundle(9197):     at com.hiteshi.thermoblue.util.Util.openUrl(Util.java:292)
12-12 14:18:32.280: W/Bundle(9197):     at com.hiteshi.facebook.Facebook.request(Facebook.java:541)
12-12 14:18:32.280: W/Bundle(9197):     at comfacebook.AsyncFacebookRunner$2.run(AsyncFacebookRunner.java:244)
12-12 14:18:32.280: W/Bundle(9197): Key format expected byte[] but value was a java.lang.String.  The default value <null> was returned.
12-12 14:18:32.281: W/Bundle(9197): Attempt to cast generated internal exception:
12-12 14:18:32.281: W/Bundle(9197): java.lang.ClassCastException: java.lang.String cannot be cast to byte[]
12-12 14:18:32.281: W/Bundle(9197):     at android.os.BaseBundle.getByteArray(BaseBundle.java:1104)
12-12 14:18:32.281: W/Bundle(9197):     at android.os.Bundle.getByteArray(Bundle.java:959)
12-12 14:18:32.281: W/Bundle(9197):     at com.hiteshi.thermoblue.util.Util.openUrl(Util.java:292)
12-12 14:18:32.281: W/Bundle(9197):     at com.hiteshi.facebook.Facebook.request(Facebook.java:541)
12-12 14:18:32.281: W/Bundle(9197):     at com.facebook.AsyncFacebookRunner$2.run(AsyncFacebookRunner.java:244)
12-12 14:18:32.293: W/Bundle(9197): Key method expected byte[] but value was a java.lang.String.  The default value <null> was returned.
12-12 14:18:32.294: W/Bundle(9197): Attempt to cast generated internal exception:
12-12 14:18:32.294: W/Bundle(9197): java.lang.ClassCastException: java.lang.String cannot be cast to byte[]
12-12 14:18:32.294: W/Bundle(9197):     at android.os.BaseBundle.getByteArray(BaseBundle.java:1104)
12-12 14:18:32.294: W/Bundle(9197):     at android.os.Bundle.getByteArray(Bundle.java:959)
12-12 14:18:32.294: W/Bundle(9197):     at com.hiteshi.thermoblue.util.Util.openUrl(Util.java:292)
12-12 14:18:32.294: W/Bundle(9197):     at com.hiteshi.facebook.Facebook.request(Facebook.java:541)
12-12 14:18:32.294: W/Bundle(9197):     at com.facebook.AsyncFacebookRunner$2.run(AsyncFacebookRunner.java:244)


12-12 14:55:21.846: D/Facebook-Example(14446): Response: {"error":{"message":"(#200) Permissions error","type":"OAuthException","code":200}}
12-12 14:55:21.847: W/Facebook-Example(14446): Facebook Error: (#200) Permissions error

这是我在Facebook墙上发布的方法:

/**
     * Post image and comment on facebook wall
     * 
     * @param wallMessage
     */
    public void postImageOnFacebookWall(final String wallMessage) {
        new AsyncTask<Void, Void, Boolean>() {
            protected void onPreExecute() {
                _progressDialog = ProgressDialog.show(_activity, "","Loading...");
            };

            @Override
            protected Boolean doInBackground(Void... params) {
                try {
                    try {
                        Bundle bundle = new Bundle();
                        mAsyncRunner = new AsyncFacebookRunner(facebook);
                        bundle.putString("caption", wallMessage);
                        bundle.putByteArray("picture", imageBytes);

                        mAsyncRunner.request("me/photos", bundle,
                                "POST",new SampleUploadListener(),null );

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

                    return true;
                } catch (Exception e) {
                    Log.d("TAG", " Exception.." + e);
                    _progressDialog .dismiss();
                    //StoreLocatorDetailScreen.bytes=null;
                    return false;
                }

            }

            protected void onPostExecute(Boolean result) {
                super.onPostExecute(result);
                _progressDialog .dismiss();
                Toast.makeText(_activity, _activity.getResources().getString(R.string.posted_success), Toast.LENGTH_SHORT).show();
            }
        }.execute();
    } 

1 个答案:

答案 0 :(得分:0)

我自己解决了。

我刚刚将代码更改为:

/**
     * Post image and comment on facebook wall
     * 
     * @param wallMessage
     */
    public void postImageOnFacebookWall(final String wallMessage) {
        new AsyncTask<Void, Void, Boolean>() {
            protected void onPreExecute() {
                _progressDialog = ProgressDialog.show(_activity, "","Posting...");
            };

            @Override
            protected Boolean doInBackground(Void... params) {
                try {
                    try {
                        Looper.prepare();
                        /*Bundle bundle = new Bundle();
                        mAsyncRunner = new AsyncFacebookRunner(facebook);
                        Bundle param = new Bundle();
                        param.putString("message", "picture caption");
                        param.putByteArray("picture", imageBytes);
                        mAsyncRunner.request("me/photos", param,
                                "POST",new SampleUploadListener(),null );*/
                        mAsyncRunner=new AsyncFacebookRunner(facebook);
                        Bundle bundle = new Bundle();
                        bundle.putString("app_id", APP_ID);
                        bundle.putString("message", msg);
                        bundle.putByteArray("picture", imageBytes);

                        facebook.authorize(_activity, PERMISSIONS, new LoginDialogListener());
                        mAsyncRunner.request("me/photos", bundle, "POST", new SampleUploadListener());

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

                    return true;
                } catch (Exception e) {
                    Log.d("TAG", " Exception.." + e);
                    _progressDialog .dismiss();
                    return false;
                }
            }

            protected void onPostExecute(Boolean result) {
                super.onPostExecute(result);
                _progressDialog .dismiss();
                Toast.makeText(_activity, _activity.getResources().getString(R.string.posted_success), Toast.LENGTH_SHORT).show();
            }
        }.execute();
    } 

然后创建了新的请求方法:

public void request(final String graphPath, final Bundle parameters, final String httpMethod,
            final RequestListener listener) {

        new Thread() {
            @Override
            public void run() {
                try {
                    String resp = fb.request(graphPath, parameters, httpMethod);
                    listener.onComplete(resp, null);
                } catch (FileNotFoundException e) {
                    listener.onFileNotFoundException(e, null);
                } catch (MalformedURLException e) {
                    listener.onMalformedURLException(e, null);
                } catch (IOException e) {
                    listener.onIOException(e, null);
                }
            }
        }.start();
    }