我想創建一個Facebook實用程序類,迄今爲止它工作得很好,但是當我測試API大於ICS我得到這個錯誤。AsyncFacebookRunner實現登錄和wallpost
7月9日至27日:43:50.706:E/AndroidRuntime(565):致命異常:主 7月9日至27日:43:50.706:E/AndroidRuntime(565):android.os.NetworkOnMainThreadException 09 -27 07:43:50.706:E/AndroidRuntime(565):at android.os.StrictMode $ AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1084) 09-27 07:43:50.706:E/AndroidRuntime(565):at java.net.InetAddress.lookupHostByName(InetAddress.java:391) 09-27 07:43:50.706:E/AndroidRuntime(565):at 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):at 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):at libcore.net.http.HttpEngine.openSocketConnection(HttpEngine.java:308) 09-27 07:43:50.706:E/AndroidRuntime (565):at libcore.net.http.HttpsURLConnectionImpl $ HttpsEngine.makeSslConnection(HttpsURLConnectionImpl.java:460) 09-2 7 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):at libcore.net.http.HttpURLConnectionImpl.connect(HttpURLConnectionImpl.java:80) 09-27 07:43:50.706:E/AndroidRuntime(565) ):at libcore.net.http.HttpsURLConnectionImpl.connect(HttpsURLConnectionImpl.java:164) 09-27 07:43:50.706:E/AndroidRuntime(565):at com.facebook.android.Util.openUrl(Util.java :193) 09-27 07:43:50.706:E/AndroidRuntime(565):at 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):at android。 widget.AbsListView.performItemClick(AbsListView.java:1058) 09-27 07:43:50.706:E/AndroidRuntime(565):at android.wi dget.AbsListView $ PerformClick.run(AbsListView.java:2514) 09-27 07:43:50.706:E/AndroidRuntime(565):at android.widget.AbsListView $ 1.run(AbsListView.java:3168) 09- 27 07:43:50.706:E/AndroidRuntime(565):at android.os.Handler.handleCallback(Handler.java:605) 09-27 07:43:50.706:E/AndroidRuntime(565):at android.os .Handler.dispatchMessage(Handler.java:92) 09-27 07:43:50.706:E/AndroidRuntime(565):at 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):at 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(本機方法)
我怎樣才能實現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();
}
}
}
謝謝:)