Unfortunately has stopped

后端 未结 4 649
渐次进展
渐次进展 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 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 {
    
            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

    
    

提交回复
热议问题