1

So usually I am able to figure out the problem by looking at the error log to see what is going on. Most of the time it gives me a line number to look at and I can figure it out from there.

but now, I am lost. I don't understand where my code is faulty.

I am not sure where to even start troubleshooting this error. I am not asking for any handouts or code corrections so I didn't display my code yet, I just would like some tips in figuring out this error:

12-10 17:12:20.359: E/AndroidRuntime(14072): FATAL EXCEPTION: main
12-10 17:12:20.359: E/AndroidRuntime(14072): android.os.NetworkOnMainThreadException
12-10 17:12:20.359: E/AndroidRuntime(14072):    at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1117)
12-10 17:12:20.359: E/AndroidRuntime(14072):    at java.net.InetAddress.lookupHostByName(InetAddress.java:385)
12-10 17:12:20.359: E/AndroidRuntime(14072):    at java.net.InetAddress.getAllByNameImpl(InetAddress.java:236)
12-10 17:12:20.359: E/AndroidRuntime(14072):    at java.net.InetAddress.getAllByName(InetAddress.java:214)
12-10 17:12:20.359: E/AndroidRuntime(14072):    at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:137)
12-10 17:12:20.359: E/AndroidRuntime(14072):    at org.apache.http.impl.conn.AbstractPoolEntry.open(AbstractPoolEntry.java:164)
12-10 17:12:20.359: E/AndroidRuntime(14072):    at org.apache.http.impl.conn.AbstractPooledConnAdapter.open(AbstractPooledConnAdapter.java:119)
12-10 17:12:20.359: E/AndroidRuntime(14072):    at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:360)
12-10 17:12:20.359: E/AndroidRuntime(14072):    at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:555)
12-10 17:12:20.359: E/AndroidRuntime(14072):    at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:487)
12-10 17:12:20.359: E/AndroidRuntime(14072):    at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:465)
12-10 17:12:20.359: E/AndroidRuntime(14072):    at com.sencide.AndroidLogin.postLoginData(AndroidLogin.java:101)
12-10 17:12:20.359: E/AndroidRuntime(14072):    at com.sencide.AndroidLogin.onClick(AndroidLogin.java:153)
12-10 17:12:20.359: E/AndroidRuntime(14072):    at android.view.View.performClick(View.java:4211)
12-10 17:12:20.359: E/AndroidRuntime(14072):    at android.view.View$PerformClick.run(View.java:17362)
12-10 17:12:20.359: E/AndroidRuntime(14072):    at android.os.Handler.handleCallback(Handler.java:725)
12-10 17:12:20.359: E/AndroidRuntime(14072):    at android.os.Handler.dispatchMessage(Handler.java:92)
12-10 17:12:20.359: E/AndroidRuntime(14072):    at android.os.Looper.loop(Looper.java:137)
12-10 17:12:20.359: E/AndroidRuntime(14072):    at android.app.ActivityThread.main(ActivityThread.java:5227)
12-10 17:12:20.359: E/AndroidRuntime(14072):    at java.lang.reflect.Method.invokeNative(Native Method)
12-10 17:12:20.359: E/AndroidRuntime(14072):    at java.lang.reflect.Method.invoke(Method.java:511)
12-10 17:12:20.359: E/AndroidRuntime(14072):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:795)
12-10 17:12:20.359: E/AndroidRuntime(14072):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:562)
12-10 17:12:20.359: E/AndroidRuntime(14072):    at dalvik.system.NativeStart.main(Native Method)
codeMagic
  • 44,549
  • 13
  • 77
  • 93
user3053446
  • 124
  • 2
  • 13
  • 2
    You could have searched for `NetworkOnMainThreadException`. Then you would have known that this happens when you perform network operations on the main/UI thread. Post code.. – Amulya Khare Dec 11 '13 at 01:38
  • [See this answer](http://stackoverflow.com/questions/16091341/what-is-the-way-to-run-a-new-thread-and-a-ui-thread-in-android/16091615#16091615) for putting your network operations in `AsyncTask` – codeMagic Dec 11 '13 at 01:51

2 Answers2

1

Assuming you are running a network operation on main thread either change ThreadPolicy or run it in AsyncTask

AsyncTask is the better Solution here is the basic outline look here for more info:

private class LoginTask extends AsyncTask {
    public LoginTask(Context context) {
        super(context);
    }

    @Override
    protected Object doInBackground(String... urls) {
           try{      

               }catch{

               }
               return.....;
               }
}

Thread policy is a bad way but its quick dirty and Android Dev guide suggests against that but it does work but I would suggest against it: StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();

dsuma
  • 1,000
  • 2
  • 9
  • 30
  • 1
    This is not a good idea. The `AsyncTask` is though – codeMagic Dec 11 '13 at 01:49
  • True ^^ but those are two solutions to the problem, but you are right I would suggest AsyncTask as well over ThreadPolicy change – dsuma Dec 11 '13 at 01:52
  • 2
    So its better to not suggest the one which is bad practice. Just the one that is a good option and elaborate on that one. – codeMagic Dec 11 '13 at 01:52
0

Look at the line 101 of AndroidLogin.java. The method postLoginData should not be invoked from a UI thread. You need to start up a new thread and move the method call to inside there.

Yuichi Araki
  • 3,438
  • 1
  • 19
  • 24