私は 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): com.infobest.praiser.share.ShareFacebook.postWall(ShareFacebook.java:99) 09-27 07:43:50.706: E/AndroidRuntime(565): com.infobest.praiser.share.ShareFacebook.send(ShareFacebook.java:47) 09-27 07:43:50.706: E/AndroidRuntime(565): com.infobest.praiser.activity.PraiseActivity$7.onClick(PraiseActivity.java:472) 09-27 07:43:50.706: E/AndroidRuntime(565): 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): at Android.app.ActivityThread.main(ActivityThread.java:4340) 09-27 07:43:50.706: E/AndroidRuntime(565): java.lang.reflect.Method.invokeNative(ネイティブ メソッド) 09-27 07:43 で:50.706: E/AndroidRuntime(565): 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 Method) 09-27 07:43:50.706: E/AndroidRuntime(565): 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 Method) 09-27 07:43:50.706: E/AndroidRuntime(565): 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();
}
}
}
ありがとうございました :)