title: Get JSON in AsyncTask Android
body: <p>I'm trying to get JSON but I have to do it in AsyncTask , because I get this in logcat <code>AndroidRuntime(18153): Caused by: android.os.NetworkOnMainThreadException</code>.</p>
  
  <p>Here is my code:</p>
  
  <pre><code>public class LatestAlbums extends Activity {
  
      TextView t;
  
      // url to make request
      private static String url = "www.example.com";
  
      // JSON Node names
      private static final String TAG_ALBUMS = "albums";
      private static final String TAG_ID = "id";
      private static final String TAG_NAME = "name";
      private static final String TAG_SINGER = "singer";
      private static final String TAG_GENRE = "genre";
      private static final String TAG_MIX = "mix";
      private static final String TAG_THUMB = "thumb";
      private static final String TAG_SONGS = "songs";
      private static final String TAG_SONG_TITLE = "song";
      private static final String TAG_SONG_ARTIST = "artist";
      private static final String TAG_SONG_MP3 = "mp3";
      private static final String TAG_SONG_MP4 = "mp4";
      private static final String TAG_SONG_THUMB = "thumb";
  
      // albums JSONArray
      JSONArray albums = null;
      JSONArray sngs = null;
      JSONObject objects = null;
  
      @Override
      protected void onCreate(Bundle savedInstanceState) {
          // TODO Auto-generated method stub
          super.onCreate(savedInstanceState);
          setContentView(R.layout.favorites);
          t = (TextView) findViewById(R.id.json);
          loadJSON();
      }
  
      public void loadJSON() {
          // Creating JSON Parser instance
          JSONParser jParser = new JSONParser();
  
          // getting JSON string from URL
          JSONObject json = jParser.getJSONFromUrl(url);
  
          try {
              // Getting Array of albums
  
              albums = json.getJSONArray(TAG_ALBUMS);
              sngs=json.getJSONArray(TAG_SONGS);
              // looping through All albums
              for (int i = 0; i &lt; albums.length(); i++) {
                  JSONObject c = albums.getJSONObject(i);
  
                  // Storing each json item in variable
                  String album_id = c.getString(TAG_ID);
                  String album_name = c.getString(TAG_NAME);
                  String album_singer = c.getString(TAG_SINGER);
                  String album_genre = c.getString(TAG_GENRE);
                  String album_thumb = c.getString(TAG_THUMB);
  
                  // songs are again JSON Object
                  for (int j = 0; i &lt; sngs.length(); j++) {
                      JSONObject s = sngs.getJSONObject(j);
                      JSONObject songs = s.getJSONObject(TAG_SONGS);
                      String artist = s.getString(TAG_SONG_ARTIST);
                      String mp3 = s.getString(TAG_SONG_MP3);
                      String mp4 = s.getString(TAG_SONG_MP4);
                      String song_thumb = s.getString(TAG_SONG_THUMB);
                      String song_title = s.getString(TAG_SONG_TITLE);
                  }
                  Log.v("--", "Albums \n" + " " + album_id + " " + album_name
                          + " " + album_genre + " " + album_singer + " "
                          + album_thumb);
              }
  
          } catch (JSONException e) {
              e.printStackTrace();
          }
  
      }
  
  }
  </code></pre>
  
  <p>Can anybody please tell me how to do this?</p>