title: Loading ListView with JSON data using AsyncTask in Android
body: <p>I want to parse JSON data from this url <a href="http://inspirontrance.com/tpb.json" rel="nofollow">http://inspirontrance.com/tpb.json</a>. My code works without the AsyncTask but with the AsyncTask enabled, app crashes after loading for a certain number of times. Although my codes seems ok but somehow the app freezes during the async process. </p>
  
  <p>Here is my code,</p>
  
  <pre><code>    public class AndroidJSONParsingActivity extends ListActivity {
  
      // url to make request
      private static String url = "http://inspirontrance.com/tpb.json";
  
      // JSON Node names
      private static final String TAG_UPLOADS = "uploads";
      private static final String TAG_ID = "id";
      private static final String TAG_NAME = "name";
      private static final String TAG_DATE = "date";
  
      ProgressDialog pDialog;
  
      // JSONArray
      JSONArray uploads = null;
  
      @Override
      public void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.main);
  
          new MyAsyncTask().execute(url);
          // Showing progress dialog before sending http request
          pDialog = new ProgressDialog(this);
          pDialog.setMessage("Please wait..");
          pDialog.setIndeterminate(true);
          pDialog.setCancelable(false);
          pDialog.show();
      }
  
      class MyAsyncTask extends
              AsyncTask&lt;String, Integer, ArrayList&lt;HashMap&lt;String, String&gt;&gt;&gt; {
  
          // Hashmap for ListView
          ArrayList&lt;HashMap&lt;String, String&gt;&gt; UploadsList = new ArrayList&lt;HashMap&lt;String, String&gt;&gt;();
  
          @Override
          protected ArrayList&lt;HashMap&lt;String, String&gt;&gt; doInBackground(
                  String... params) {
  
              // Creating JSON Parser instance
              JSONParser jParser = new JSONParser();
  
              // getting JSON string from URL
              JSONObject json = jParser.getJSONFromUrl(url);
  
              try {
                  // Getting Array of Uploads
                  uploads = json.getJSONArray(TAG_UPLOADS);
  
                  // looping through All Uploads
                  for (int i = 0; i &lt; uploads.length(); i++) {
                      JSONObject c = uploads.getJSONObject(i);
  
                      // Storing each json item in variable
                      String id = c.getString(TAG_ID);
                      String name = c.getString(TAG_NAME);
                      String date = c.getString(TAG_DATE);
  
                      // creating new HashMap
                      HashMap&lt;String, String&gt; map = new HashMap&lt;String, String&gt;();
  
                      // adding each child node to HashMap key =&gt; value
                      map.put(TAG_ID, id);
                      map.put(TAG_NAME, name);
                      map.put(TAG_DATE, date);
  
                      // adding HashList to ArrayList
                      UploadsList.add(map);
                  }
              } catch (JSONException e) {
                  e.printStackTrace();
              }
  
              return UploadsList;
          }
  
      }
  
      protected void onPostExecute(ArrayList&lt;HashMap&lt;String, String&gt;&gt; result) {
  
          pDialog.dismiss();
  
          ListAdapter adapter = new SimpleAdapter(this, result,
                  R.layout.list_item,
                  new String[] { TAG_NAME, TAG_ID, TAG_DATE }, new int[] {
                          R.id.name, R.id.id, R.id.date });
  
          setListAdapter(adapter);
      }
  }
  </code></pre>