使用Android Facebook SDK在Facebook墙上发布照片时出错
作者:互联网
我正在使用官方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 one rror(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)
请提前帮助我解决此错误.
解决方法:
我解决了我的错误,
我正在给< uses-sdk android:minSdkVersion =“ 12” />在我的清单文件中,当时由于NetworkOnMainThreadException而崩溃.
在google之后,我发现只对针对Honeycomb SDK或更高版本的应用程序抛出此执行.
所以,我只是将SDK更改为较低级别的api,例如:< uses-sdk android:minSdkVersion =“ 8” /> ;,现在可以正常使用了. 但是仍然,如果我们要使用Honeycomb或更高版本的SDK,则需要在单独的线程中执行所有与网络相关的任务(使用处理程序,异步任务或runOnUiThread). 这是handler & Async Task的很好的例子,并检查如何使用runOnUiThread
标签:android,facebook-android-sdk,facebook-wall 来源: https://codeday.me/bug/20191011/1895355.html