0

Honeycomb error android.os.NetworkOnMainThreadException after i set my target sdk to 11. I used the below codes to ignore it. Will it effect my app? Can somebody suggest anything to overcome this problem.

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

StrictMode.setThreadPolicy(policy); 



@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.grid_layout);
        StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();

        StrictMode.setThreadPolicy(policy); 

        ArrayList<HashMap<String, String>> songsList = new ArrayList<HashMap<String, String>>();

        XMLParser parser = new XMLParser();
        String xml = parser.getXmlFromUrl(URL); // getting XML from URL
        Document doc = parser.getDomElement(xml); // getting DOM element

        NodeList nl = doc.getElementsByTagName(KEY_SONG);
        // looping through all song nodes <song>
        for (int i = 0; i < nl.getLength(); i++) {
            // creating new HashMap
            HashMap<String, String> map = new HashMap<String, String>();
            Element e = (Element) nl.item(i);
            // adding each child node to HashMap key => value
            map.put(KEY_ID, parser.getValue(e, KEY_ID));
            map.put(KEY_TITLE, parser.getValue(e, KEY_TITLE));
            map.put(KEY_ARTIST, parser.getValue(e, KEY_ARTIST));
            map.put(KEY_DURATION, parser.getValue(e, KEY_DURATION));
            map.put(KEY_THUMB_URL, parser.getValue(e, KEY_THUMB_URL));

            // adding HashList to ArrayList
            songsList.add(map);
        }
KC Chai
  • 1,607
  • 9
  • 30
  • 59

1 Answers1

2

It is not good practice to do network access on main thread(UI thread).if your target SDK is Honeycomb or higher, it will give network on main thread exception.Try using Asynac tasks to avoid this.

Code EX:

public class yourclass extends Activity{
        @Override
        public void onCreate(Bundle savedInstanceState)  {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.uoyrlayout);
        //this is UI thread you should not do network access from here.
        new LongRunning().execute();//call to background thread to do network access
 }
    public class LongRunning extends AsyncTask<Void, Void, Void> {
        protected void onPreExecute() {
            //UI updating goes here before background thread..
        }
        @Override
        protected Void doInBackground(Void... params) {     
            //This is not the UI thread
            //do your network acces
            return null;            
        }

        @Override
        protected void onPostExecute(Void result) {
           //update your UI after background thread
        }

    }
}
Dinesh Anuruddha
  • 7,137
  • 6
  • 32
  • 45
  • Thanks. this looks difficult. With those upvote, I will accept your answer. Dare not ask for more, i'll need to find help to implement my codes into yours. – KC Chai Aug 16 '12 at 05:32
  • ok kcchai it is not that much of difficult you just need to do is to put your network access ligic into doInBackground method and make sure not to update your UI in doInBackground method. that is all you need to do. – Dinesh Anuruddha Aug 16 '12 at 06:09