我正在尝试创建一个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):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):at java.net.InetAddress.getAllByName(InetAddress.java:220) 09-27 07:43:50.706:E / AndroidRuntime(565):at libcore.net.http.HttpConnection。(HttpConnection.java:71) 09-27 07:43:50.706:E / AndroidRuntime(565):at 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):at libcore.net.http.HttpConnectionPool.get(HttpConnectionPool.java:86) 09-27 07:43:50.706:E / AndroidRuntime(565):at 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-27 07:43:50.706:E / AndroidRuntime(565):at libcore.net.http.HttpsURLConnectionImpl $ HttpsEngine.connect(HttpsURLConnectionImpl.java:432) 09-27 07:43:50.706:E / AndroidRuntime(565):at libcore.net.http.HttpEngine.sendSocketRequest(HttpEngine.java:282) 09-27 07:43:50.706:E / AndroidRuntime(565):at 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):在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):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):at dalvik.system.NativeStart.main(Native Method)
如果我的类看起来像这样,我该如何实现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();
}
}
}
谢谢:)
答案 0 :(得分:0)
您无法从UI线程调用Facebook.request()。
它只发生在ICS及以上版本,因为ICS会检查以确保您不会尝试在UI线程上进行网络通信。
来自facebook api的方法评论:
请注意,此方法会阻止等待网络响应,因此请勿在UI线程中调用它。
创建一个线程并从线程运行fb.request应该可以解决错误。