title: Android - Loading JSON to ListView
body: <p>I'm still pretty new to Android development, am doing an app/project which involves loading this JSON (listed below) into a <code>ListView</code> with the name, description, and image, but it has been coming up blank on the emulator. Please tell me what I'm doing wrong - any help would be greatly appreciated! (The more specific the better as I am such a newbie..)</p>
  
  <p><strong>JSON:</strong></p>
  
  <pre><code> {
     "success":true,
     "games":[
        {
           "id":"1",
           "name":"Name of the Game",
           "description":"This is what it is about",
           "image":"IMAGE URL"
        }
     ]
  }
  </code></pre>
  
  <p><strong>AndroidJSONParsingActivity:</strong></p>
  
  <pre><code>// URL to make request
      private static final String url = "URL of API";
  
      // JSON Node names
      private static final String TAG_GAMES = "games";
      private static final String TAG_ID = "id";
      private static final String TAG_NAME = "name";
      private static final String TAG_DESCRIPTION = "description";
      private static final String TAG_IMAGE = "image";
  
      // games JSONArray
      JSONArray games = null;
  
      @Override
      public void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.main);
  
      // Hash map for ListView
                  ArrayList&lt;HashMap&lt;String, String&gt;&gt; gameList = new ArrayList&lt;HashMap&lt;String, String&gt;&gt;();
  
              // Creating JSON Parser instance
              JSONParser jParser = new JSONParser();
  
              // getting JSON string from URL
              JSONObject json = jParser.getJSONFromUrl(url);
  
              try {
                  // Getting Array of Games
                  games = json.getJSONArray(TAG_GAMES);
  
                  // looping through All Games
                  for(int i = 0; i &lt; games.length(); i++){
                      JSONObject c = games.getJSONObject(i);
  
                      // Storing each JSON item in variable
                      String id = c.getString(TAG_ID);
                      String image = c.getString(TAG_IMAGE);
  
                      // is again JSON Object
                      JSONObject author = c.getJSONObject(TAG_DESCRIPTION);
                      String name = author.getString(TAG_NAME);
  
                      // 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_IMAGE, image);
                      // adding HashList to ArrayList
                      gameList.add(map);
                  }
              } catch (JSONException e) {
                  e.printStackTrace();
              }
  
  
              /**
               * Updating parsed JSON data into ListView
               * */
              ListAdapter adapter = new SimpleAdapter(this, gameList,
                      R.layout.list_item,
                      new String[] { TAG_NAME, TAG_DESCRIPTION }, new int[] {
                              R.id.name, R.id.description });
  
              setListAdapter(adapter);
  
              // selecting single ListView item
              ListView lv = getListView();
  
              // Launching new screen on Selecting Single ListItem
              lv.setOnItemClickListener(new OnItemClickListener() {
  
                  public void onItemClick(AdapterView&lt;?&gt; parent, View view,
                          int position, long id) {
                      // getting values from selected ListItem
                      String name = ((TextView) view.findViewById(R.id.name)).getText().toString();
                      String description = ((TextView) view.findViewById(R.id.description)).getText().toString();
  
                      // Starting new intent
                      Intent in = new Intent(getApplicationContext(), SingleMenuItemActivity.class);
                      in.putExtra(TAG_NAME, name);
                      in.putExtra(TAG_DESCRIPTION, description);
                      startActivity(in);
  
                  }
              });
  
  
  
          }
  </code></pre>
  
  <p><strong>JSON Parser:</strong></p>
  
  <pre><code>public class JSONParser {
  
      static InputStream is = null;
      static JSONObject jObj = null;
      static String json = "";
  
      // constructor
      public JSONParser() {
  
      }
  
      public JSONObject getJSONFromUrl(String url) {
  
          // Making HTTP request
          try {
              // defaultHttpClient
              DefaultHttpClient httpClient = new DefaultHttpClient();
              HttpPost httpPost = new HttpPost(url);
  
              HttpResponse httpResponse = httpClient.execute(httpPost);
              HttpEntity httpEntity = httpResponse.getEntity();
              is = httpEntity.getContent();
  
          } catch (UnsupportedEncodingException e) {
              e.printStackTrace();
          } catch (ClientProtocolException e) {
              e.printStackTrace();
          } catch (IOException e) {
              e.printStackTrace();
          }
  
          try {
              BufferedReader reader = new BufferedReader(new InputStreamReader(
                      is, "iso-8859-1"), 8);
              StringBuilder sb = new StringBuilder();
              String line = null;
              while ((line = reader.readLine()) != null) {
                  sb.append(line + "\n");
              }
              is.close();
              json = sb.toString();
          } catch (Exception e) {
              Log.e("Buffer Error", "Error converting result " + e.toString());
          }
  
          // try parse the string to a JSON object
          try {
              jObj = new JSONObject(json);
          } catch (JSONException e) {
              Log.e("JSON Parser", "Error parsing data " + e.toString());
          }
  
          // return JSON String
          return jObj;
  
      }
  }
  </code></pre>
  
  <p><strong>main.xml:</strong></p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
      android:layout_width="fill_parent"
      android:layout_height="fill_parent"
      android:orientation="vertical"&gt;
      &lt;!-- Main ListView 
           Always give id value as list(@android:id/list)
      --&gt;
      &lt;ListView
          android:id="@android:id/list"
          android:layout_width="fill_parent"
          android:layout_height="wrap_content"/&gt;
  
  &lt;/LinearLayout&gt;
  </code></pre>
  
  <p>list_item.xml:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;LinearLayout
      xmlns:android="http://schemas.android.com/apk/res/android"
      android:layout_width="fill_parent"
      android:layout_height="wrap_content"
      android:orientation="vertical"&gt;
      &lt;!-- Name Label --&gt;
      &lt;TextView
          android:id="@+id/name"
          android:layout_width="fill_parent"
          android:layout_height="wrap_content"
          android:textColor="#43bd00"
          android:textSize="16sp"
          android:textStyle="bold"
          android:paddingTop="6dip"
          android:paddingBottom="2dip" /&gt;
          &lt;TextView
              android:id="@+id/description"
              android:layout_width="fill_parent"
              android:layout_height="wrap_content"
              android:textColor="#acacac"
              android:paddingBottom="2dip" /&gt;
          &lt;/LinearLayout&gt;
  </code></pre>
  
  <p>Adding LogCat info:</p>
  
  <pre><code>01-03 06:16:15.254: E/ActivityThread(616): Service com.android.exchange.ExchangeService has leaked ServiceConnection com.android.emailcommon.service.ServiceProxy$ProxyConnection@40d47b40 that was originally bound here
  01-03 06:16:15.254: E/ActivityThread(616): android.app.ServiceConnectionLeaked: Service com.android.exchange.ExchangeService has leaked ServiceConnection com.android.emailcommon.service.ServiceProxy$ProxyConnection@40d47b40 that was originally bound here
  01-03 06:16:15.254: E/ActivityThread(616):  at android.app.LoadedApk$ServiceDispatcher.&lt;init&gt;(LoadedApk.java:969)
  01-03 06:16:15.254: E/ActivityThread(616):  at android.app.LoadedApk.getServiceDispatcher(LoadedApk.java:863)
  01-03 06:16:15.254: E/ActivityThread(616):  at android.app.ContextImpl.bindService(ContextImpl.java:1418)
  01-03 06:16:15.254: E/ActivityThread(616):  at android.app.ContextImpl.bindService(ContextImpl.java:1407)
  01-03 06:16:15.254: E/ActivityThread(616):  at android.content.ContextWrapper.bindService(ContextWrapper.java:473)
  01-03 06:16:15.254: E/ActivityThread(616):  at com.android.emailcommon.service.ServiceProxy.setTask(ServiceProxy.java:157)
  01-03 06:16:15.254: E/ActivityThread(616):  at com.android.emailcommon.service.ServiceProxy.setTask(ServiceProxy.java:145)
  01-03 06:16:15.254: E/ActivityThread(616):  at com.android.emailcommon.service.ServiceProxy.test(ServiceProxy.java:191)
  01-03 06:16:15.254: E/ActivityThread(616):  at com.android.exchange.ExchangeService$7.run(ExchangeService.java:1850)
  01-03 06:16:15.254: E/ActivityThread(616):  at com.android.emailcommon.utility.Utility$2.doInBackground(Utility.java:551)
  01-03 06:16:15.254: E/ActivityThread(616):  at com.android.emailcommon.utility.Utility$2.doInBackground(Utility.java:549)
  01-03 06:16:15.254: E/ActivityThread(616):  at android.os.AsyncTask$2.call(AsyncTask.java:287)
  01-03 06:16:15.254: E/ActivityThread(616):  at java.util.concurrent.FutureTask.run(FutureTask.java:234)
  01-03 06:16:15.254: E/ActivityThread(616):  at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080)
  01-03 06:16:15.254: E/ActivityThread(616):  at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573)
  01-03 06:16:15.254: E/ActivityThread(616):  at java.lang.Thread.run(Thread.java:856)
  01-03 06:16:15.365: E/StrictMode(616): null
  01-03 06:16:15.365: E/StrictMode(616): android.app.ServiceConnectionLeaked: Service com.android.exchange.ExchangeService has leaked ServiceConnection com.android.emailcommon.service.ServiceProxy$ProxyConnection@40d47b40 that was originally bound here
  01-03 06:16:15.365: E/StrictMode(616):  at android.app.LoadedApk$ServiceDispatcher.&lt;init&gt;(LoadedApk.java:969)
  01-03 06:16:15.365: E/StrictMode(616):  at android.app.LoadedApk.getServiceDispatcher(LoadedApk.java:863)
  01-03 06:16:15.365: E/StrictMode(616):  at android.app.ContextImpl.bindService(ContextImpl.java:1418)
  01-03 06:16:15.365: E/StrictMode(616):  at android.app.ContextImpl.bindService(ContextImpl.java:1407)
  01-03 06:16:15.365: E/StrictMode(616):  at android.content.ContextWrapper.bindService(ContextWrapper.java:473)
  01-03 06:16:15.365: E/StrictMode(616):  at com.android.emailcommon.service.ServiceProxy.setTask(ServiceProxy.java:157)
  01-03 06:16:15.365: E/StrictMode(616):  at com.android.emailcommon.service.ServiceProxy.setTask(ServiceProxy.java:145)
  01-03 06:16:15.365: E/StrictMode(616):  at com.android.emailcommon.service.ServiceProxy.test(ServiceProxy.java:191)
  01-03 06:16:15.365: E/StrictMode(616):  at com.android.exchange.ExchangeService$7.run(ExchangeService.java:1850)
  01-03 06:16:15.365: E/StrictMode(616):  at com.android.emailcommon.utility.Utility$2.doInBackground(Utility.java:551)
  01-03 06:16:15.365: E/StrictMode(616):  at com.android.emailcommon.utility.Utility$2.doInBackground(Utility.java:549)
  01-03 06:16:15.365: E/StrictMode(616):  at android.os.AsyncTask$2.call(AsyncTask.java:287)
  </code></pre>