XML : Web XML crashes app?


I'm developing a weather android app that reads an XML file from the web, and displays the current temperature. This code works perfectly fine in a normal project within eclipse. I made slight changes to it but it crashes the app when I click the button. In my layout I just have a button added which is programmed in the code.

  package ali.androidtest;    import android.support.v7.app.ActionBarActivity;  import android.os.Bundle;  import android.view.View;  import android.view.Menu;  import android.view.MenuItem;  import android.widget.Button;  import android.widget.EditText;  import android.widget.TextView;  import java.io.IOException;  import java.io.InputStream;  import java.net.URL;  import javax.xml.parsers.DocumentBuilder;  import javax.xml.parsers.DocumentBuilderFactory;  import org.w3c.dom.NodeList;  import org.w3c.dom.Node;  import org.w3c.dom.Document;  import org.w3c.dom.Element;  public class MainActivity extends ActionBarActivity {      @Override      protected void onCreate(Bundle savedInstanceState) {          super.onCreate(savedInstanceState);          setContentView(R.layout.activity_main);          Button button = (Button)findViewById(R.id.button);            button.setOnClickListener(                  new Button.OnClickListener() {                      public void onClick(View v) {                          try {                              System.out.println("hello");                              URL xmlUrl = new URL("http://ift.tt/1CloN4c");                              System.out.println("hi");                              InputStream in = xmlUrl.openStream();                              Document doc = parse(in);                                  doc.getDocumentElement().normalize();                                  NodeList nList = doc.getElementsByTagName("temperature");                                System.out.println("----------------------------");                                for (int temp = 0; temp < nList.getLength(); temp++) {                                    Node nNode = nList.item(temp);                                      if (nNode.getNodeType() == Node.ELEMENT_NODE) {                                        Element eElement = (Element) nNode;                                        System.out.println("Temperature: " + eElement.getAttribute("value"));                                      }                              }                          } catch (IOException e) {                              // TODO Auto-generated catch block                              e.printStackTrace();                          }                      }                  }          );          }      public static Document parse (InputStream is) {          Document ret = null;          DocumentBuilderFactory domFactory;          DocumentBuilder builder;            try {              domFactory = DocumentBuilderFactory.newInstance();              domFactory.setValidating(false);              domFactory.setNamespaceAware(false);              builder = domFactory.newDocumentBuilder();                ret = builder.parse(is);          }          catch (Exception ex) {              System.err.println("unable to load XML: " + ex);          }          return ret;      }      @Override      public boolean onCreateOptionsMenu(Menu menu) {          // Inflate the menu; this adds items to the action bar if it is present.          getMenuInflater().inflate(R.menu.menu_main, menu);          return true;      }        @Override      public boolean onOptionsItemSelected(MenuItem item) {          // Handle action bar item clicks here. The action bar will          // automatically handle clicks on the Home/Up button, so long          // as you specify a parent activity in AndroidManifest.xml.          int id = item.getItemId();            //noinspection SimplifiableIfStatement          if (id == R.id.action_settings) {              return true;          }            return super.onOptionsItemSelected(item);      }  }    

No comments:

Post a Comment