title: Read Web XML using Android Studio
body: <p>I'm developing an app to read XML file from the web and parse it into text.
  I have tried the following code and it works perfectly with a normal project on eclipse(made slight changes for Android Studio):</p>
  
  <p><div class="snippet" data-lang="js" data-hide="false">
  <div class="snippet-code">
  <pre class="snippet-code-js lang-js prettyprint-override"><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.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);
      }
      public void getData(View v)throws IOException
      {
          System.out.println("hello");
          URL xmlUrl = new URL("http://api.openweathermap.org/data/2.5/weather?q=dallas%20ga&amp;units=metric&amp;mode=xml");
          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 &lt; 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"));
  
  
              }
          }
      }
      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);
      }
  }</code></pre>
  </div>
  </div>
  </p>
  
  <p>getData is link to a button. 
  When I use this code in android studio to develop I get an error:</p>
  
  <p><div class="snippet" data-lang="js" data-hide="false">
  <div class="snippet-code">
  <pre class="snippet-code-js lang-js prettyprint-override"><code>02-12 23:39:15.757  22930-22930/ali.androidtest I/ViewRootImpl﹕ ViewRoot's Touch Event : ACTION_DOWN
  02-12 23:39:15.811  22930-22930/ali.androidtest I/ViewRootImpl﹕ ViewRoot's Touch Event : ACTION_UP
  02-12 23:39:15.816  22930-22930/ali.androidtest I/System.out﹕ hello
  02-12 23:39:15.827  22930-22930/ali.androidtest I/System.out﹕ hi
  02-12 23:39:15.840  22930-22930/ali.androidtest D/libc-netbsd﹕ [getaddrinfo]: hostname=xxxxx; servname=(null); cache_mode=(null), netid=0; mark=0
  02-12 23:39:15.840  22930-22930/ali.androidtest D/libc﹕ getaddrinfo called from pid =22930
  02-12 23:39:15.840  22930-22930/ali.androidtest D/libc-netbsd﹕ [getaddrinfo]: ai_addrlen=0; ai_canonname=xxxxx; ai_flags=4; ai_family=0
  02-12 23:39:15.852  22930-22930/ali.androidtest D/AndroidRuntime﹕ Shutting down VM
  02-12 23:39:15.866  22930-22930/ali.androidtest E/AndroidRuntime﹕ FATAL EXCEPTION: main
      Process: ali.androidtest, PID: 22930
      java.lang.IllegalStateException: Could not execute method of the activity
              at android.view.View$1.onClick(View.java:4015)
              at android.view.View.performClick(View.java:4764)
              at android.view.View$PerformClick.run(View.java:19833)
              at android.os.Handler.handleCallback(Handler.java:739)
              at android.os.Handler.dispatchMessage(Handler.java:95)
              at android.os.Looper.loop(Looper.java:135)
              at android.app.ActivityThread.main(ActivityThread.java:5292)
              at java.lang.reflect.Method.invoke(Native Method)
              at java.lang.reflect.Method.invoke(Method.java:372)
              at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:908)
              at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:703)
       Caused by: java.lang.reflect.InvocationTargetException
              at java.lang.reflect.Method.invoke(Native Method)
              at java.lang.reflect.Method.invoke(Method.java:372)
              at android.view.View$1.onClick(View.java:4010)
              at android.view.View.performClick(View.java:4764)
              at android.view.View$PerformClick.run(View.java:19833)
              at android.os.Handler.handleCallback(Handler.java:739)
              at android.os.Handler.dispatchMessage(Handler.java:95)
              at android.os.Looper.loop(Looper.java:135)
              at android.app.ActivityThread.main(ActivityThread.java:5292)
              at java.lang.reflect.Method.invoke(Native Method)
              at java.lang.reflect.Method.invoke(Method.java:372)
              at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:908)
              at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:703)
       Caused by: android.os.NetworkOnMainThreadException
              at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1147)
              at java.net.InetAddress.lookupHostByName(InetAddress.java:426)
              at java.net.InetAddress.getAllByNameImpl(InetAddress.java:255)
              at java.net.InetAddress.getAllByName(InetAddress.java:218)
              at com.android.okhttp.HostResolver$1.getAllByName(HostResolver.java:29)
              at com.android.okhttp.internal.http.RouteSelector.resetNextInetSocketAddress(RouteSelector.java:232)
              at com.android.okhttp.internal.http.RouteSelector.next(RouteSelector.java:124)
              at com.android.okhttp.internal.http.HttpEngine.connect(HttpEngine.java:272)
              at com.android.okhttp.internal.http.HttpEngine.sendRequest(HttpEngine.java:211)
              at com.android.okhttp.internal.http.HttpURLConnectionImpl.execute(HttpURLConnectionImpl.java:373)
              at com.android.okhttp.internal.http.HttpURLConnectionImpl.getResponse(HttpURLConnectionImpl.java:323)
              at com.android.okhttp.internal.http.HttpURLConnectionImpl.getInputStream(HttpURLConnectionImpl.java:190)
              at java.net.URL.openStream(URL.java:470)
              at ali.androidtest.MainActivity.getData(MainActivity.java:32)
              at java.lang.reflect.Method.invoke(Native Method)
              at java.lang.reflect.Method.invoke(Method.java:372)
              at android.view.View$1.onClick(View.java:4010)
              at android.view.View.performClick(View.java:4764)
              at android.view.View$PerformClick.run(View.java:19833)
              at android.os.Handler.handleCallback(Handler.java:739)
              at android.os.Handler.dispatchMessage(Handler.java:95)
              at android.os.Looper.loop(Looper.java:135)
              at android.app.ActivityThread.main(ActivityThread.java:5292)
              at java.lang.reflect.Method.invoke(Native Method)
              at java.lang.reflect.Method.invoke(Method.java:372)
              at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:908)
              at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:703)</code></pre>
  </div>
  </div>
  </p>
  
  <p><div class="snippet" data-lang="js" data-hide="false">
  <div class="snippet-code">
  <pre class="snippet-code-html lang-html prettyprint-override"><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="ali.androidtest" &gt;
  
      &lt;application
          android:allowBackup="true"
          android:icon="@drawable/ic_launcher"
          android:label="@string/app_name"
          android:theme="@style/AppTheme" &gt;
          &lt;activity
              android:name=".MainActivity"
              android:label="@string/app_name" &gt;
              &lt;intent-filter&gt;
                  &lt;action android:name="android.intent.action.MAIN" /&gt;
  
                  &lt;category android:name="android.intent.category.LAUNCHER" /&gt;
              &lt;/intent-filter&gt;
          &lt;/activity&gt;
      &lt;/application&gt;
  
  &lt;/manifest&gt;</code></pre>
  </div>
  </div>
  </p>