0

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.

Any ideas?

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);
    }

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

public ArrayList<String> getPerson() {

ArrayList<String> peopleArray = new ArrayList<String>();


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 < 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;

}

This method is supposed to take in the returned ArrayList (peopleArray) and add it to a tablerow. At the moment all that displays is "[]"

    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();

}



}

In the Manifest the "internet" is enabled.

<uses-permission android:name="android.permission.INTERNET"/>

Here is my layout:

<?xml version="1.0" encoding="utf-8"?>
<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">

    <LinearLayout
        android:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_height="55dp">

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Get Person"
            android:id="@+id/button"
            android:onClick="onClick" />
    </LinearLayout>

    <TableLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/tableLayout">

        <TableRow
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:id="@+id/tableRow"></TableRow>
    </TableLayout>
</LinearLayout>

Here's a snippet from the JSON data from the URL

[
  {
    "FirstName": "Jimmy",
    "LastName": "Cricket",
    "Address": "Arizona",
  },
  {
    "FirstName": "Freddy",
    "LastName": "Foxx",
    "Address": "Washington State",
  },
  {
    "FirstName": "Louis",
    "LastName": "Jones",
    "Address": "Texas",
  }
  ]
Sam
  • 47
  • 2
  • 12
  • see this http://stackoverflow.com/questions/9605913/how-to-parse-json-in-android – ELITE Feb 26 '16 at 13:39
  • `JSONArray jArray = new JSONArray(line);` you are parsing just one line from server response.Is that correct json array you got from server in just one line?? – ELITE Feb 26 '16 at 13:40
  • is there any your snippet of jsonObject? may be something wrong in parsing jsonObjects(also jsonArrays) – iroiroys Feb 26 '16 at 13:44
  • 1
    To avoid this kind of parsing problems , take a look at google Gson library : https://sites.google.com/site/gson/gson-user-guide – bashizip Feb 26 '16 at 13:44

0 Answers0