使用android facebook sdk在Facebook墙上发布照片时出错

时间:2012-01-04 06:37:23

标签: android facebook-wall facebook-android-sdk

我正在使用官方的Android Facebook sdk在墙上发布照片,但在登录后崩溃,同时允许我的帐户获得应用程序权限。 我正在为这个应用程序使用android 3.1库。

以下是我用于在墙上发布照片的代码,

facebook.authorize(ImageCropShare.this, new String[] { "publish_stream" },
                            new DialogListener() {

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

                        }

                        @Override
                        public void onError(DialogError dialogError) {
                        // TODO Auto-generated method stub

                        }

                        @Override
                        public void onComplete(Bundle values) {                                             

                                Bundle params = new Bundle();
                                params.putString(Facebook.TOKEN, values.getString(Facebook.TOKEN));
                                params.putByteArray("picture", data); // here data is byte array of bitmap image

                                try {
                                    facebook.request("me/photos", params, "POST");

                                } catch (FileNotFoundException fileNotFoundException) {
                                   // makeToast(fileNotFoundException.getMessage());
                                } catch (MalformedURLException malformedURLException) {
                                   // makeToast(malformedURLException.getMessage());
                                } catch (IOException ioException) {
                                   // makeToast(ioException.getMessage());
                                }

                                try {
                                    facebook.logout(getApplicationContext());
                                } catch (MalformedURLException e) {
                                    // TODO Auto-generated catch block
                                    e.printStackTrace();
                                } catch (IOException e) {
                                    // TODO Auto-generated catch block
                                    e.printStackTrace();
                                }
                        }

                        @Override
                        public void onCancel() {
                        // TODO Auto-generated method stub
                            try {
                                facebook.logout(getApplicationContext());
                            } catch (MalformedURLException e) {
                                // TODO Auto-generated catch block
                                e.printStackTrace();
                            } catch (IOException e) {
                                // TODO Auto-generated catch block
                                e.printStackTrace();
                            }
                        }
                    });

以下是我从位图图像

获取字节数组的函数
public byte[] bitmapTobyte(Bitmap bitmap)
    {
        byte[] data = null;

        ByteArrayOutputStream stream = new ByteArrayOutputStream();
        bitmap.compress(Bitmap.CompressFormat.PNG, 100, stream);
        data = stream.toByteArray();

        return data;
    }

以下是我的错误日志

01-04 11:44:04.022: E/AndroidRuntime(480): FATAL EXCEPTION: main
01-04 11:44:04.022: E/AndroidRuntime(480): android.os.NetworkOnMainThreadException
01-04 11:44:04.022: E/AndroidRuntime(480):  at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1077)
01-04 11:44:04.022: E/AndroidRuntime(480):  at java.net.InetAddress.lookupHostByName(InetAddress.java:477)
01-04 11:44:04.022: E/AndroidRuntime(480):  at java.net.InetAddress.getAllByNameImpl(InetAddress.java:277)
01-04 11:44:04.022: E/AndroidRuntime(480):  at java.net.InetAddress.getAllByName(InetAddress.java:249)
01-04 11:44:04.022: E/AndroidRuntime(480):  at org.apache.harmony.luni.internal.net.www.protocol.http.HttpConnection.<init>(HttpConnection.java:69)
01-04 11:44:04.022: E/AndroidRuntime(480):  at org.apache.harmony.luni.internal.net.www.protocol.http.HttpConnection.<init>(HttpConnection.java:48)
01-04 11:44:04.022: E/AndroidRuntime(480):  at org.apache.harmony.luni.internal.net.www.protocol.http.HttpConnection$Address.connect(HttpConnection.java:304)
01-04 11:44:04.022: E/AndroidRuntime(480):  at org.apache.harmony.luni.internal.net.www.protocol.http.HttpConnectionPool.get(HttpConnectionPool.java:89)
01-04 11:44:04.022: E/AndroidRuntime(480):  at org.apache.harmony.luni.internal.net.www.protocol.http.HttpURLConnectionImpl.getHttpConnection(HttpURLConnectionImpl.java:292)
01-04 11:44:04.022: E/AndroidRuntime(480):  at org.apache.harmony.luni.internal.net.www.protocol.http.HttpURLConnectionImpl.makeConnection(HttpURLConnectionImpl.java:274)
01-04 11:44:04.022: E/AndroidRuntime(480):  at org.apache.harmony.luni.internal.net.www.protocol.https.HttpsURLConnectionImpl$HttpsEngine.makeSslConnection(HttpsURLConnectionImpl.java:447)
01-04 11:44:04.022: E/AndroidRuntime(480):  at org.apache.harmony.luni.internal.net.www.protocol.https.HttpsURLConnectionImpl$HttpsEngine.makeConnection(HttpsURLConnectionImpl.java:419)
01-04 11:44:04.022: E/AndroidRuntime(480):  at org.apache.harmony.luni.internal.net.www.protocol.http.HttpURLConnectionImpl.connect(HttpURLConnectionImpl.java:217)
01-04 11:44:04.022: E/AndroidRuntime(480):  at org.apache.harmony.luni.internal.net.www.protocol.https.HttpsURLConnectionImpl.connect(HttpsURLConnectionImpl.java:177)
01-04 11:44:04.022: E/AndroidRuntime(480):  at com.facebook.android.Util.openUrl(Util.java:178)
01-04 11:44:04.022: E/AndroidRuntime(480):  at com.facebook.android.Facebook.request(Facebook.java:563)
01-04 11:44:04.022: E/AndroidRuntime(480):  at com.mazda.cx5app.imagecrop.ImageCropShare$2$1.onComplete(ImageCropShare.java:123)
01-04 11:44:04.022: E/AndroidRuntime(480):  at com.facebook.android.Facebook$1.onComplete(Facebook.java:308)
01-04 11:44:04.022: E/AndroidRuntime(480):  at com.facebook.android.FbDialog$FbWebViewClient.shouldOverrideUrlLoading(FbDialog.java:145)
01-04 11:44:04.022: E/AndroidRuntime(480):  at android.webkit.CallbackProxy.uiOverrideUrlLoading(CallbackProxy.java:222)
01-04 11:44:04.022: E/AndroidRuntime(480):  at android.webkit.CallbackProxy.handleMessage(CallbackProxy.java:322)
01-04 11:44:04.022: E/AndroidRuntime(480):  at android.os.Handler.dispatchMessage(Handler.java:99)
01-04 11:44:04.022: E/AndroidRuntime(480):  at android.os.Looper.loop(Looper.java:132)
01-04 11:44:04.022: E/AndroidRuntime(480):  at android.app.ActivityThread.main(ActivityThread.java:4025)
01-04 11:44:04.022: E/AndroidRuntime(480):  at java.lang.reflect.Method.invokeNative(Native Method)
01-04 11:44:04.022: E/AndroidRuntime(480):  at java.lang.reflect.Method.invoke(Method.java:491)
01-04 11:44:04.022: E/AndroidRuntime(480):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:841)
01-04 11:44:04.022: E/AndroidRuntime(480):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:599)
01-04 11:44:04.022: E/AndroidRuntime(480):  at dalvik.system.NativeStart.main(Native Method)

请提前感谢帮我解决这个问题..!

1 个答案:

答案 0 :(得分:2)

我解决了我的错误,

我在我的清单文件中提供了<uses-sdk android:minSdkVersion="12" />,当时由于NetworkOnMainThreadException而崩溃了。

google之后,我发现只针对Honeycomb SDK或更高版本的应用程序抛出了此execption。

所以,我只是将我的SDK更改为较低级别的api,例如:<uses-sdk android:minSdkVersion="8" />,现在工作正常..

但是如果我们想要使用Honeycomb或更高版本的SDK,那么我们需要在单独的线程中执行所有与网络相关的任务(使用处理程序,Async Task或runOnUiThread)。

以下是handler & Async Task的一个很好的示例,还会检查如何使用runOnUiThread