title: Json Web Service Data Retrieval
body: <p>How to retrieve JSON web service data for android? I'm currently trying to retrieve event data in JSON format and display it but I'm not really sure how should I do it. But somehow I just can't run in my mobile application. Here is a sample of my code: </p>
  
  <pre><code>package com.androidhive.jsonparsing;
  
  import java.util.ArrayList;
  import java.util.HashMap;
  
  import org.json.JSONArray;
  import org.json.JSONException;
  import org.json.JSONObject;
  
  import android.app.ListActivity;
  import android.content.Intent;
  import android.os.Bundle;
  import android.util.Log;
  import android.view.View;
  import android.widget.AdapterView;
  import android.widget.AdapterView.OnItemClickListener;
  import android.widget.ListAdapter;
  import android.widget.ListView;
  import android.widget.SimpleAdapter;
  import android.widget.TextView;
  
  public class AndroidJSONParsingActivity extends ListActivity {
  
      // url to make request
      private static String url = "http://api.eventful.com/json/events/get?app_key=rDkKF6nSx6LjWTDR&amp;id=E0-001-000324672-7";
  
      // JSON Node names
      private static final String TAG_ID = "id";
      private static final String TAG_REGION = "region";
      private static final String TAG_STARTTIME = "start_time";
      private static final String TAG_TITLE = "title";
      private static final String TAG_CITY = "city";
      private static final String TAG_VENUE_NAME = "venue_name";
  
      // contacts JSONArray
      JSONArray id;
  
      @Override
      public void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.main);
  
          // Hashmap for ListView
          ArrayList&lt;HashMap&lt;String, String&gt;&gt; contactList = 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 Contacts
              id = json.getJSONArray(TAG_ID);
  
              // looping through All Contacts
              for (int i = 0; i &lt; id.length(); i++) {
                  JSONObject c = id.getJSONObject(i);
  
                  // Storing each json item in variable
                  String mid = c.getString(TAG_ID);
                  String region = c.getString(TAG_REGION);
                  String starttime = c.getString(TAG_STARTTIME);
                  String mtitle = c.getString(TAG_TITLE);
                  String city = c.getString(TAG_CITY);
                  String venuename = c.getString(TAG_VENUE_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, mid);
                  map.put(TAG_REGION, region);
                  map.put(TAG_STARTTIME, starttime);
                  map.put(TAG_TITLE, mtitle);
                  map.put(TAG_CITY, city);
                  map.put(TAG_VENUE_NAME, venuename);
  
                  // adding HashList to ArrayList
                  contactList.add(map);
              }
          } catch (JSONException e) {
              e.printStackTrace();
          }
  
          /**
           * Updating parsed JSON data into ListView
           * */
          ListAdapter adapter = new SimpleAdapter(this, contactList,
                  R.layout.list_item, new String[] { TAG_TITLE },
                  new int[] { R.id.mtitle });
  
          setListAdapter(adapter);
  
          // selecting single ListView item
          ListView lv = getListView();
  
          // Launching new screen on Selecting Single ListItem
          lv.setOnItemClickListener(new OnItemClickListener() {
  
              @Override
              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.mtitle))
                          .getText().toString();
  
                  // Starting new intent
                  Intent in = new Intent(getApplicationContext(),
                          SingleMenuItemActivity.class);
                  in.putExtra(TAG_TITLE, name);
  
  
              }
          });
  
      }
  
  }
  </code></pre>
  
  <p><strong>As Requested, this is my sample code for JSONParser:</strong></p>
  
  <pre><code>package com.androidhive.jsonparsing;
  
  import java.io.BufferedReader;
  import java.io.IOException;
  import java.io.InputStream;
  import java.io.InputStreamReader;
  import java.io.UnsupportedEncodingException;
  
  import org.apache.http.HttpEntity;
  import org.apache.http.HttpResponse;
  import org.apache.http.client.ClientProtocolException;
  import org.apache.http.client.methods.HttpPost;
  import org.apache.http.impl.client.DefaultHttpClient;
  import org.json.JSONException;
  import org.json.JSONObject;
  
  import android.util.Log;
  
  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>