Unfortunately has stopped

后端 未结 4 589
渐次进展
渐次进展 2021-01-27 08:20

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

相关标签:
4条回答
  • 2021-01-27 08:49

    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.

    0 讨论(0)
  • 2021-01-27 08:52

    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.

    0 讨论(0)
  • 2021-01-27 08:58

    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

    0 讨论(0)
  • 2021-01-27 09:11

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

    add this code in onCreate

    StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
    
    StrictMode.setThreadPolicy(policy); 
    

    or add HTTP call code into 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"/>
    
    0 讨论(0)
提交回复
热议问题