Skip to content Skip to sidebar Skip to footer

Error In Http Connectionandroid.os.networkonmainthreadexception

when I launched my application I have a problem with emulator 4.1 and 4.2 even I have already made ​​the Internet permission, logcat shows this mssage: Error in http connection

Solution 1:

NetworkOnMainThread exception occurs when you are running network related operation on the main UI thread. http://developer.android.com/reference/android/os/NetworkOnMainThreadException.html

You use should use a asynctask for this purpose or create your own thread.

You are making a http request on the main ui thread.

http://developer.android.com/reference/android/os/AsyncTask.html

Check the link above especially the topic under the heading The 4 steps.

Example:

classTheTaskextendsAsyncTask<Void,Void,Void>
 {
  protectedvoidonPreExecute()
  {           super.onPreExecute();
            //display progressdialog.
  } 

   protectedvoiddoInBackground(Void ...params)
  {  
        //Network related opearaiton. Do not update ui herereturnnull;
  } 

   protectedvoidonPostExecute(Void result)
  {     
            super.onPostExecute(result);
            //dismiss progressdialog.//update ui
  } 
}

Solution 2:

As doc says about NetworkOnMainThreadException:

the exception that is thrown when an application attempts to perform a networking operation on its main thread.

So, you're getting this error because you're running your client in the main Thread. To avoid this exception you can use an AsyncTask, Executor or simply using a Thread

Post a Comment for "Error In Http Connectionandroid.os.networkonmainthreadexception"