我正在尝试创建一个 Facebook 实用程序类,到目前为止它工作得很好,但是当我在大于 ICS 的 API 上测试它时,我得到了这个错误。
09-27 07:43:50.706:E/AndroidRuntime(565):致命异常:主要 09-27 07:43:50.706:E/AndroidRuntime(565):android.os.NetworkOnMainThreadException 09-27 07:43:50.706: E/AndroidRuntime(565): 在 android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1084) 09-27 07:43:50.706: E/AndroidRuntime(565): 在 java.net.InetAddress.lookupHostByName(InetAddress .java:391) 09-27 07:43:50.706: E/AndroidRuntime(565): 在 java.net.InetAddress.getAllByNameImpl(InetAddress.java:242) 09-27 07:43:50.706: E/AndroidRuntime(565 ): 在 java.net.InetAddress.getAllByName(InetAddress.java:220) 09-27 07:43:50.706: E/AndroidRuntime(565): 在 libcore.net.http.HttpConnection.(HttpConnection.java:71) 09 -27 07:43:50.706: E/AndroidRuntime(565): 在 libcore.net.http.HttpConnection.(HttpConnection.java:50) 09-27 07:43:50.706: E/AndroidRuntime(565):在 libcore.net.http.HttpConnection$Address.connect(HttpConnection.java:351) 09-27 07:43:50.706: E/AndroidRuntime(565): 在 libcore.net.http.HttpConnectionPool.get(HttpConnectionPool.java: 86) 09-27 07:43:50.706: E/AndroidRuntime(565): 在 libcore.net.http.HttpConnection.connect(HttpConnection.java:128) 09-27 07:43:50.706: E/AndroidRuntime(565) : 在 libcore.net.http.HttpEngine.openSocketConnection(HttpEngine.java:308) 09-27 07:43:50.706: E/AndroidRuntime(565): 在 libcore.net.http.HttpsURLConnectionImpl$HttpsEngine.makeSslConnection(HttpsURLConnectionImpl.java :460) 09-27 07:43:50.706: E/AndroidRuntime(565): 在 libcore.net.http.HttpsURLConnectionImpl$HttpsEngine.connect(HttpsURLConnectionImpl.java:432) 09-27 07:43:50.706: E/AndroidRuntime (565): 在 libcore.net.http.HttpEngine.sendSocketRequest(HttpEngine.java:282) 09-27 07:43:50.706:E/AndroidRuntime(565): 在 libcore.net.http.HttpEngine.sendRequest(HttpEngine.java:232) 09-27 07:43:50.706: E/AndroidRuntime(565): 在 libcore.net.http.HttpURLConnectionImpl.connect (HttpURLConnectionImpl.java:80) 09-27 07:43:50.706: E/AndroidRuntime(565): 在 libcore.net.http.HttpsURLConnectionImpl.connect(HttpsURLConnectionImpl.java:164) 09-27 07:43:50.706: E /AndroidRuntime(565): 在 com.facebook.android.Util.openUrl(Util.java:193) 09-27 07:43:50.706: E/AndroidRuntime(565): 在 com.facebook.android.Facebook.request( Facebook.java:751) 09-27 07:43:50.706: E/AndroidRuntime(565): at com.infobest.praiser.share.ShareFacebook.postWall(ShareFacebook.java:99) 09-27 07:43:50.706: E/AndroidRuntime(565): at com.infobest.praiser.share.ShareFacebook.send(ShareFacebook.java:47) 09-27 07:43:50.706: E/AndroidRuntime(565): at com.infobest.praiser。activity.PraiseActivity$7.onClick(PraiseActivity.java:472) 09-27 07:43:50.706: E/AndroidRuntime(565): at com.android.internal.app.AlertController$AlertParams$3.onItemClick(AlertController.java:924 ) 09-27 07:43:50.706: E/AndroidRuntime(565): 在 android.widget.AdapterView.performItemClick(AdapterView.java:292) 09-27 07:43:50.706: E/AndroidRuntime(565): 在 android .widget.AbsListView.performItemClick(AbsListView.java:1058) 09-27 07:43:50.706: E/AndroidRuntime(565): 在 android.widget.AbsListView$PerformClick.run(AbsListView.java:2514) 09-27 07 :43:50.706: E/AndroidRuntime(565): 在 android.widget.AbsListView$1.run(AbsListView.java:3168) 09-27 07:43:50.706: E/AndroidRuntime(565): 在 android.os.Handler .handleCallback(Handler.java:605) 09-27 07:43:50.706: E/AndroidRuntime(565): 在 android.os.Handler.dispatchMessage(Handler.java:92) 09-27 07:43:50.706: E/AndroidRuntime(565): 在 android.os.Looper.loop(Looper.java:137) 09-27 07:43:50.706: E/AndroidRuntime(565): 在android.app.ActivityThread.main(ActivityThread.java:4340) 09-27 07:43:50.706: E/AndroidRuntime(565): 在 java.lang.reflect.Method.invokeNative(Native Method) 09-27 07:43 :50.706: E/AndroidRuntime(565): at java.lang.reflect.Method.invoke(Method.java:511) 09-27 07:43:50.706: E/AndroidRuntime(565): at com.android.internal。 os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784) 09-27 07:43:50.706: E/AndroidRuntime(565): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551) 09 -27 07:43:50.706:E/AndroidRuntime(565):在 dalvik.system.NativeStart.main(本机方法)E/AndroidRuntime(565): 在 android.app.ActivityThread.main(ActivityThread.java:4340) 09-27 07:43:50.706: E/AndroidRuntime(565): 在 java.lang.reflect.Method.invokeNative(Native方法) 09-27 07:43:50.706: E/AndroidRuntime(565): at java.lang.reflect.Method.invoke(Method.java:511) 09-27 07:43:50.706: E/AndroidRuntime(565) : 在 com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784) 09-27 07:43:50.706: E/AndroidRuntime(565): 在 com.android.internal.os.ZygoteInit.main (ZygoteInit.java:551)09-27 07:43:50.706:E/AndroidRuntime(565):在 dalvik.system.NativeStart.main(本机方法)E/AndroidRuntime(565): 在 android.app.ActivityThread.main(ActivityThread.java:4340) 09-27 07:43:50.706: E/AndroidRuntime(565): 在 java.lang.reflect.Method.invokeNative(Native方法) 09-27 07:43:50.706: E/AndroidRuntime(565): at java.lang.reflect.Method.invoke(Method.java:511) 09-27 07:43:50.706: E/AndroidRuntime(565) : 在 com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784) 09-27 07:43:50.706: E/AndroidRuntime(565): 在 com.android.internal.os.ZygoteInit.main (ZygoteInit.java:551)09-27 07:43:50.706:E/AndroidRuntime(565):在 dalvik.system.NativeStart.main(本机方法)调用(Method.java:511)09-27 07:43:50.706:E/AndroidRuntime(565):在 com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)09-27 07: 43:50.706: E/AndroidRuntime(565): 在 com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551) 09-27 07:43:50.706: E/AndroidRuntime(565): 在 dalvik.system .NativeStart.main(本机方法)调用(Method.java:511)09-27 07:43:50.706:E/AndroidRuntime(565):在 com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)09-27 07: 43:50.706: E/AndroidRuntime(565): 在 com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551) 09-27 07:43:50.706: E/AndroidRuntime(565): 在 dalvik.system .NativeStart.main(本机方法)
如果我的班级看起来像这样,我该如何实现 AsyncFacebookRunner
{
public String msg;
public Context ctx;
public Facebook fb;
public SharedPreferences sp;
public ShareFacebook(String msg, Context ctx, Facebook fb, SharedPreferences sp)
{
this.msg = msg;
this.ctx = ctx;
this.fb = fb;
this.sp = sp;
}
public void send()
{
if (fb.isSessionValid())
{
postWall();
}
else
{
fb.authorize((Activity) ctx, new String[] {"email", "publish_checkins",
"publish_stream"}, new DialogListener()
{
@Override
public void onFacebookError(FacebookError e)
{
e.printStackTrace();
}
@Override
public void onError(DialogError e)
{
Toast.makeText(ctx,R.string.facebookError, Toast.LENGTH_SHORT).show();
}
@Override
public void onComplete(Bundle values)
{
Editor editor = sp.edit();
editor.putString("access_token", fb.getAccessToken());
editor.putLong("access_expires", fb.getAccessExpires());
editor.commit();
postWall();
}
@Override
public void onCancel()
{
}
});
}
}
private void postWall()
{
Bundle params = new Bundle();
params.putString("message", msg );
params.putString("link",ctx.getResources().getString(R.string.rateLink));
params.putString("picture",
ctx.getResources().getDrawable(R.drawable.ic_launcher).toString());
try
{
fb.request("me/feed", params, "POST");
Toast.makeText(ctx,R.string.facebookSuccess, Toast.LENGTH_SHORT).show();
}
catch (FileNotFoundException e)
{
e.printStackTrace();
}
catch (MalformedURLException e)
{
e.printStackTrace();
}
catch (IOException e)
{
e.printStackTrace();
}
}
}
谢谢 :)