title: How to fix NetworkonMainThreadException in Android?
body: <p>I'm creating an project for assignment, I'm new to Android, and I wanted to access json from very common url <code>http://api.androidhive.info/contacts/</code>,</p>
  
  <p><strong>Problem:</strong> I'm trying to read the url and fetch and parse the json returned by this url,</p>
  
  <p>I've already added following line into my AndroidManifest.xml</p>
  
  <pre><code>&lt;uses-permission android:name="android.permission.INTERNET"/&gt;
  </code></pre>
  
  <p><strong>Preferences:</strong> and my android preferences are</p>
  
  <pre><code>&lt;uses-sdk
          android:minSdkVersion="8"
          android:targetSdkVersion="18" /&gt;
  </code></pre>
  
  <ol>
  <li>Api level 18</li>
  <li>Android 4.3</li>
  </ol>
  
  <p>and this is how I'm trying to read the url</p>
  
  <pre><code>static InputStream is = null;
  
  DefaultHttpClient httpClient = new DefaultHttpClient();
  HttpPost httpPost = new HttpPost(url);
  
  HttpResponse httpResponse = httpClient.execute(httpPost);
  HttpEntity httpEntity = httpResponse.getEntity();
  is = httpEntity.getContent();
  </code></pre>
  
  <p><strong>The Error Message</strong></p>
  
  <pre><code>11-02 05:23:47.843: E/AndroidRuntime(2207): FATAL EXCEPTION: main
  11-02 05:23:47.843: E/AndroidRuntime(2207): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.me.countrypedia/com.me.countrypedia.MainActivity}: android.os.NetworkOnMainThreadException
  11-02 05:23:47.843: E/AndroidRuntime(2207):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2211)
  11-02 05:23:47.843: E/AndroidRuntime(2207):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2261)
  11-02 05:23:47.843: E/AndroidRuntime(2207):     at android.app.ActivityThread.access$600(ActivityThread.java:141)
  11-02 05:23:47.843: E/AndroidRuntime(2207):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1256)
  </code></pre>
  
  <p>Also I'm following this tutorial for ListView Example
  <a href="http://www.androidhive.info/2011/11/android-xml-parsing-tutorial/" rel="noreferrer">http://www.androidhive.info/2011/11/android-xml-parsing-tutorial/</a></p>