Skip to content Skip to sidebar Skip to footer

Android Connecting With Php And Mysql Force Close

I was writing an application that required login and registration, so I look up some tutorials on the net. I also bought and online hosting server to store my database. However I k

Solution 1:

android.os.NetworkOnMainThreadException

This exception occurs when you access network from your main thread. Use AsyncTask to access network as such.

privateclassMyInnerClassextendsAsyncTask<String, Void, String> {
   @OverrideprotectedvoidonPreExecute() {
   super.onPreExecute();

   }

   @OverrideprotectedStringdoInBackground(String params) {

   return"Done";
   }

   @OverrideprotectedvoidonPostExecute(String result) {
   super.onPostExecute(result);
   }
   }

Call new MyInnerClass().execute(); from you main Activity and Android will automatically call onPreExecute(). This method is for the stuff you wana do before network access

Network related stuff is done inside doInBackground() and then Android will call onPostExecute() and the result will be passed as params to this method.

Solution 2:

You're performing a (potentially slow) network operation on the main thread. If your target SDK is 11 (Honeycomb) or higher this will throw a NetworkOnMainThreadException on Honeycomb or above, because this behaviour can block the UI and lead to an unresponsive app.

You could use an AsyncTask to get around this, loading the data in its doInBackground(..).

Post a Comment for "Android Connecting With Php And Mysql Force Close"