title: NetworkOnMainThread
body: <p>I get a NetworkOnMainThreadException when I try to implement the following code:</p>
  
  <pre><code>public class HandlingXMLStuff extends ListActivity{
  static final String URL = "xml_file";
  
   static final String ITEM = "item"; //parent
      static final String Id = "id";
      static final String Name = "name";
      static final String Desc = "desc";
      static final String Link = "Link";
  
  @Override
  public void onCreate(Bundle savedInstanceState){
      super.onCreate(savedInstanceState);
      setContentView(R.layout.xmllist);
  
  ArrayList&lt;HashMap&lt;String, String&gt;&gt; menuItems = new ArrayList&lt;HashMap&lt;String, String&gt;&gt;(); 
  
  xmlparser parser = new xmlparser();
  String xml = parser.getXmlFromUrl(URL);
  Document doc = parser.getDomElement(xml);
  
  
  NodeList nl = doc.getElementsByTagName(ITEM);
  
  
  //START: loop through all item nodes &lt;item&gt;
  for (int i = 0;i&lt;nl.getLength();i++){
      //lets create our HASHMAP!! (feeds items into our ArrayList)
      HashMap&lt;String, String&gt; map = new HashMap&lt;String, String&gt;();
      Element e = (Element) nl.item(i);
      //add each child node to the HashMap (key, value/&lt;String, String&gt;)
      map.put(Name, parser.getValue(e, Name));
      map.put(Desc, parser.getValue(e, Desc));
      map.put(Link, parser.getValue(e, Link));
  
  
      menuItems.add(map);
  
  }//DONE 
  
  
  ListAdapter adapter = new SimpleAdapter(this, menuItems, R.layout.list_item,
          new String[] {Name, Desc, Link}, new int []{R.id.name, R.id.description, R.id.link});
  
  setListAdapter(adapter);
          }
  }
  </code></pre>
  
  <p>and the handler:</p>
  
  <pre><code>public class xmlparser{
  
  public String getXmlFromUrl(String url) {
  String xml = null;
  
  try {
  
      DefaultHttpClient httpClient = new DefaultHttpClient();
      HttpPost httpPost = new HttpPost(url);
  
      HttpResponse httpResponse = httpClient.execute(httpPost);
      HttpEntity httpEntity = httpResponse.getEntity();
      xml = EntityUtils.toString(httpEntity);
  
  } catch (UnsupportedEncodingException e) {
      e.printStackTrace();
  } catch (ClientProtocolException e) {
      e.printStackTrace();
  } catch (IOException e) {
      e.printStackTrace();
  }
  
  return xml;
  }
  public Document getDomElement(String xml){
      Document doc = null;
      DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
      try {
  
          DocumentBuilder db = dbf.newDocumentBuilder();
  
          InputSource is = new InputSource();
              is.setCharacterStream(new StringReader(xml));
              doc = db.parse(is); 
  
          } catch (ParserConfigurationException e) {
              Log.e("Error: ", e.getMessage());
              return null;
          } catch (SAXException e) {
              Log.e("Error: ", e.getMessage());
              return null;
          } catch (IOException e) {
              Log.e("Error: ", e.getMessage());
              return null;
          }
  
          return doc;
    }
  
  public String getValue(Element item, String str) {      
          NodeList n = item.getElementsByTagName(str);        
          return this.getElementValue(n.item(0));
      }
  
  public final String getElementValue( Node elem ) {
           Node child;
           if( elem != null){
               if (elem.hasChildNodes()){
                   for( child = elem.getFirstChild(); child != null; child = child.getNextSibling() ){
                       if( child.getNodeType() == Node.TEXT_NODE  ){
                           return child.getNodeValue();
                       }
                   }
               }
           }
           return "";
    }    
  </code></pre>
  
  <p>}</p>
  
  <p>Any idea why? It should work, all the tutorials I've read treat this as working code but it doesn't run and only throws the exception.  I've read I might need to implement asynctask but im new to it and not sure what parts need their own thread.  Thanks for any help, critique (constructive), suggestions, etc.</p>