I have an app where the user submits some data in a form which is then sent to a server. I am testing it on a tablet and an Android smartphone (Galaxy S2). On the tablet, as soo
You're doing your network operations from the main thread, which is a very very bad idea. You should at least do this inside an AsyncTask.
You're trying to run a network request on the main UI thread. Android does not allow you to do that since 3.0 (I believe). Doing so causes your UI to lock up until the request is completed, rendering your app useless during the execution of the request.
You'll either have to run your request in a new Thread
or an ASyncTask
, to take the load of the UI thread. You can find more info on how to use multiple threads here.
This issue is happening that you are not using the painless thread. That all the android os versions which is higher than 3.0 wont support any king of internet accessing works (http connections ) in the oncreate.. So that you are supposed to use ASYNCTask . please have a look at this link to know about this .
http://samir-mangroliya.blogspot.in/p/android-asynctask-example.html
http://developer.android.com/reference/android/os/AsyncTask.html
NetworkOnMainThreadException: The exception that is thrown when an application attempts to perform a networking operation on its main thread.
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);
AsyncTask
class RetreiveFeedTask extends AsyncTask<String, Void, RSSFeed> {
private Exception exception;
protected RSSFeed doInBackground(String... urls) {
try {
// add your all code here
} catch (Exception e) {
this.exception = e;
return null;
}
}
protected void onPostExecute(RSSFeed feed) {
// TODO: check this.exception
// TODO: do something with the feed
}
}
to execute the AsyncTask:
new RetreiveFeedTask().execute(urlToRssFeed);
hope you have added below permission in android manifest file
<uses-permission android:name="android.permission.INTERNET"/>