title: Get JSON data from URL and display in tablelayout (Android Studio)
body: <p>I have some JSON data stored online and I want to grab that from the URL and display it in a tablelayout in Android Studio. At present the code I have done doesnt appear to be working. When I click the button on the app all that displays is "[]" and not the desired information from the arraylist.</p>
  
  <p>Any ideas?</p>
  
  <pre><code>public class MainActivity extends AppCompatActivity {
  
      TableLayout tabLay;
      TableRow tabRow;
      TextView label1, label2;
  
  
      @Override
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_main);
  
          tabLay = (TableLayout) findViewById(R.id.tableLayout);
          tabRow = (TableRow) findViewById(R.id.tableRow);
  
  
  
      }
  
      @Override
      public boolean onCreateOptionsMenu(Menu menu) {
          // Inflate the menu; this adds items to the action bar if it is present.
          getMenuInflater().inflate(R.menu.menu_main, menu);
          return true;
      }
  
      @Override
      public boolean onOptionsItemSelected(MenuItem item) {
          // Handle action bar item clicks here. The action bar will
          // automatically handle clicks on the Home/Up button, so long
          // as you specify a parent activity in AndroidManifest.xml.
          int id = item.getItemId();
  
          //noinspection SimplifiableIfStatement
          if (id == R.id.action_settings) {
              return true;
          }
  
          return super.onOptionsItemSelected(item);
      }
  </code></pre>
  
  <p>This method is supposed to get the person's attributes from the JSON data, put them in an array list and then return the array list</p>
  
  <pre><code>public ArrayList&lt;String&gt; getPerson() {
  
  ArrayList&lt;String&gt; peopleArray = new ArrayList&lt;String&gt;();
  
  
  try {
  
      URL url = new URL("http://some_url_with_JSON_data");
      HttpURLConnection urlCON = (HttpURLConnection) url.openConnection();
  
      InputStreamReader isr = new InputStreamReader(urlCON.getInputStream());
  
      BufferedReader br = new BufferedReader(isr);
  
      String line;
      while ((line = br.readLine()) != null) {
  
          JSONArray jArray = new JSONArray(line);
          for (int i = 0; i &lt; jArray.length(); i++) {
  
              JSONObject jObject = (JSONObject) jArray.get(i);
  
              peopleArray.add(jObject.getString("FirstName"));
              peopleArray.add(jObject.getString("LastName"));
              peopleArray.add(jObject.getString("Address"));
  
          }
  
      }
  
  } catch (SecurityException e) {
      throw new ArrayIndexOutOfBoundsException();
  } catch (Exception e) {
      e.printStackTrace();
  }
  
  
  return peopleArray;
  
  }
  </code></pre>
  
  <p>This method is supposed to take in the returned ArrayList (peopleArray) and add it to a tablerow. At the moment all that displays is "[]"</p>
  
  <pre><code>    public void table() {
  
  
          String array = getPerson().toString();
  
              label1 = new TextView(this);
  
              label1.setText(array);
  
              tabRow = new TableRow(this);
  
              tabRow.addView(label1);
  
              tabLay.addView(tabRow);
  
  
  
      }
  
  
  // this method calls the table method when the "get person" button is clicked
      public void onClick(View v){
          table();
  
  }
  
  
  
  }
  </code></pre>
  
  <p>In the Manifest the "internet" is enabled.</p>
  
  <pre><code>&lt;uses-permission android:name="android.permission.INTERNET"/&gt;
  </code></pre>
  
  <p>Here is my layout:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
      xmlns:app="http://schemas.android.com/apk/res-auto"
      xmlns:tools="http://schemas.android.com/tools"
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      android:paddingBottom="@dimen/activity_vertical_margin"
      android:paddingLeft="@dimen/activity_horizontal_margin"
      android:paddingRight="@dimen/activity_horizontal_margin"
      android:paddingTop="@dimen/activity_vertical_margin"
      app:layout_behavior="@string/appbar_scrolling_view_behavior"
      tools:context="@string/name"
      tools:showIn="@layout/activity_main"
      android:orientation="vertical"&gt;
  
      &lt;LinearLayout
          android:orientation="horizontal"
          android:layout_width="match_parent"
          android:layout_height="55dp"&gt;
  
          &lt;Button
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:text="Get Person"
              android:id="@+id/button"
              android:onClick="onClick" /&gt;
      &lt;/LinearLayout&gt;
  
      &lt;TableLayout
          android:layout_width="match_parent"
          android:layout_height="match_parent"
          android:id="@+id/tableLayout"&gt;
  
          &lt;TableRow
              android:layout_width="match_parent"
              android:layout_height="match_parent"
              android:id="@+id/tableRow"&gt;&lt;/TableRow&gt;
      &lt;/TableLayout&gt;
  &lt;/LinearLayout&gt;
  </code></pre>
  
  <p>Here's a snippet from the JSON data from the URL</p>
  
  <pre><code>[
    {
      "FirstName": "Jimmy",
      "LastName": "Cricket",
      "Address": "Arizona",
    },
    {
      "FirstName": "Freddy",
      "LastName": "Foxx",
      "Address": "Washington State",
    },
    {
      "FirstName": "Louis",
      "LastName": "Jones",
      "Address": "Texas",
    }
    ]
  </code></pre>