-2

Getting exception in android facebook code on util class on line 215 Here i print the stack:

04-05 11:08:34.015: E/AndroidRuntime(13527): FATAL EXCEPTION: main
04-05 11:08:34.015: E/AndroidRuntime(13527): android.os.NetworkOnMainThreadException
04-05 11:08:34.015: E/AndroidRuntime(13527):    at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1117)
04-05 11:08:34.015: E/AndroidRuntime(13527):    at java.net.InetAddress.lookupHostByName(InetAddress.java:385)
04-05 11:08:34.015: E/AndroidRuntime(13527):    at java.net.InetAddress.getAllByNameImpl(InetAddress.java:236)
04-05 11:08:34.015: E/AndroidRuntime(13527):    at java.net.InetAddress.getAllByName(InetAddress.java:214)
04-05 11:08:34.015: E/AndroidRuntime(13527):    at libcore.net.http.HttpConnection.<init>(HttpConnection.java:70)
04-05 11:08:34.015: E/AndroidRuntime(13527):    at libcore.net.http.HttpConnection.<init>(HttpConnection.java:50)
04-05 11:08:34.015: E/AndroidRuntime(13527):    at libcore.net.http.HttpConnection$Address.connect(HttpConnection.java:341)
04-05 11:08:34.015: E/AndroidRuntime(13527):    at libcore.net.http.HttpConnectionPool.get(HttpConnectionPool.java:87)
04-05 11:08:34.015: E/AndroidRuntime(13527):    at libcore.net.http.HttpConnection.connect(HttpConnection.java:128)
04-05 11:08:34.015: E/AndroidRuntime(13527):    at libcore.net.http.HttpEngine.openSocketConnection(HttpEngine.java:315)
04-05 11:08:34.015: E/AndroidRuntime(13527):    at libcore.net.http.HttpsURLConnectionImpl$HttpsEngine.makeSslConnection(HttpsURLConnectionImpl.java:461)
04-05 11:08:34.015: E/AndroidRuntime(13527):    at libcore.net.http.HttpsURLConnectionImpl$HttpsEngine.connect(HttpsURLConnectionImpl.java:433)
04-05 11:08:34.015: E/AndroidRuntime(13527):    at libcore.net.http.HttpEngine.sendSocketRequest(HttpEngine.java:289)
04-05 11:08:34.015: E/AndroidRuntime(13527):    at libcore.net.http.HttpEngine.sendRequest(HttpEngine.java:239)
04-05 11:08:34.015: E/AndroidRuntime(13527):    at libcore.net.http.HttpURLConnectionImpl.getResponse(HttpURLConnectionImpl.java:273)
04-05 11:08:34.015: E/AndroidRuntime(13527):    at libcore.net.http.HttpURLConnectionImpl.getInputStream(HttpURLConnectionImpl.java:168)
04-05 11:08:34.015: E/AndroidRuntime(13527):    at libcore.net.http.HttpsURLConnectionImpl.getInputStream(HttpsURLConnectionImpl.java:271)
04-05 11:08:34.015: E/AndroidRuntime(13527):    at com.facebook.android.Util.openUrl(Util.java:215)
04-05 11:08:34.015: E/AndroidRuntime(13527):    at com.facebook.android.Facebook.request(Facebook.java:755)
04-05 11:08:34.015: E/AndroidRuntime(13527):    at com.facebook.android.Facebook.request(Facebook.java:692)
04-05 11:08:34.015: E/AndroidRuntime(13527):    at com.agequiz.fbhelper.FacebookConnector$1.onAuthSucceed(FacebookConnector.java:184)
04-05 11:08:34.015: E/AndroidRuntime(13527):    at com.agequiz.fbhelper.SessionEvents.onLoginSuccess(SessionEvents.java:78)
04-05 11:08:34.015: E/AndroidRuntime(13527):    at com.agequiz.fbhelper.FacebookConnector$LoginDialogListener.onComplete(FacebookConnector.java:112)
04-05 11:08:34.015: E/AndroidRuntime(13527):    at com.facebook.android.Facebook$1.onComplete(Facebook.java:358)
04-05 11:08:34.015: E/AndroidRuntime(13527):    at com.facebook.android.FbDialog$FbWebViewClient.shouldOverrideUrlLoading(FbDialog.java:154)
04-05 11:08:34.015: E/AndroidRuntime(13527):    at android.webkit.CallbackProxy.uiOverrideUrlLoading(CallbackProxy.java:274)
04-05 11:08:34.015: E/AndroidRuntime(13527):    at android.webkit.CallbackProxy.handleMessage(CallbackProxy.java:376)
04-05 11:08:34.015: E/AndroidRuntime(13527):    at android.os.Handler.dispatchMessage(Handler.java:99)
04-05 11:08:34.015: E/AndroidRuntime(13527):    at android.os.Looper.loop(Looper.java:137)
04-05 11:08:34.015: E/AndroidRuntime(13527):    at android.app.ActivityThread.main(ActivityThread.java:4745)
04-05 11:08:34.015: E/AndroidRuntime(13527):    at java.lang.reflect.Method.invokeNative(Native Method)
04-05 11:08:34.015: E/AndroidRuntime(13527):    at java.lang.reflect.Method.invoke(Method.java:511)
04-05 11:08:34.015: E/AndroidRuntime(13527):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
04-05 11:08:34.015: E/AndroidRuntime(13527):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
04-05 11:08:34.015: E/AndroidRuntime(13527):    at dalvik.system.NativeStart.main(Native Method)
rajeshwaran
  • 1,648
  • 1
  • 18
  • 24
user2106897
  • 519
  • 1
  • 5
  • 10
  • refer this http://stackoverflow.com/questions/12757416/android-os-networkonmainthreadexception-in-facebook-wall-post – rajeshwaran Apr 05 '13 at 06:26

3 Answers3

0

Your current code runs, on the uiThread. Consider using either an asynctask or a class which extends Thread. There is a lot of questions regarding this, by doing a quick Google search, so I feel that an example would be too much.

Community
  • 1
  • 1
Tobias Moe Thorstensen
  • 8,660
  • 13
  • 71
  • 137
0

This exception is thrown when an application attempts to perform a networking operation on its main thread. Run your code in AsyncTask:

private class Retreive extends AsyncTask<Void, Void, Void> {

protected Void doInBackground(Void... urls) {       
    return null;         
}
RajaReddy PolamReddy
  • 22,160
  • 18
  • 112
  • 166
-2

This exception is thrown when an application attempts to perform a networking operation on its main thread. Run your code in AsyncTask or in Thread.

or Add below code in onCreate() method.

StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);
Community
  • 1
  • 1
jlopez
  • 6,287
  • 2
  • 51
  • 90
  • 1
    Bad, bad, bad. Please run long operations on a thread. – Tobias Moe Thorstensen Apr 05 '13 at 06:29
  • Do I use threads, but you're going to teach to use an asynctask a person who does not know to search for your answer hundreds of post with the same question? – jlopez Apr 05 '13 at 06:33
  • Since the person got to stackoverflow, posted a question probably setted up eclipse with android sdk, programmed a bit due to the fact that he gets an exception. How on earth wouldn't that person at least try to figure out by reading a tutorial on the subject, how to make an `asynctask`? Please do not downvote answers because you want to. If so, please add a comment so the community could see why! – Tobias Moe Thorstensen Apr 05 '13 at 06:37
  • This is a simple search in google in my answer edited – jlopez Apr 05 '13 at 06:42
  • You vote negative my answer by proposing its response and another possibility. If you want to help improve stackoverflow rather than negative votes to useful and complementary to his responses, set a flag to close the question – jlopez Apr 05 '13 at 06:45
  • This discuss is kinda irrelevant, but why should you teach hotfixes the way you do? `asynctask` or a `thread` is the way to deal with this sort of exception on android. I voted negative because the way you suggested is way off the simplest and not least the preferred way – Tobias Moe Thorstensen Apr 05 '13 at 06:48
  • At least we agree on something... this discussion is using too much of my valuable time. You think that your answer is the best, and I think the same way about mine. The user to choose – jlopez Apr 05 '13 at 06:53