0

I write this piece of code for reading json file and show it in listview but it return empty list . I would appreciate if anybody advise me. Thanks

eventList.json:

    {
"events": [
{
"event": "Taste of day"
},
{
"event": "House Party at Park"
},
{
"event": "Farmers Markets"
},
{
"event": "Blues Festival Preview Events"
},
{
"event": "Cultural Alliance - Heritage and Fashion"
}
]
}

and this an Activity for reading and parsing json file :

     public class EventSelectActivity extends Activity {




@Override


protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_event_select);


    // Reading json file from assets folder
    StringBuffer sb = new StringBuffer();
    BufferedReader br = null;
    try {
        br = new BufferedReader(new InputStreamReader(getAssets().open(
                "eventList.json")));
        String temp;
        while ((temp = br.readLine()) != null)
            sb.append(temp);
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        if ((br != null)) {
            try {
                br.close(); // stop reading
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    try {
        JSONObject jsonObjMain = new JSONObject();
        JSONArray jsonArray = jsonObjMain.getJSONArray("events");

        ArrayList<String> messages = new ArrayList<String>();
        for (int i = 0; i < jsonArray.length(); i++) {

            // Creating JSONObject from JSONArray
            JSONObject jsonObj = jsonArray.getJSONObject(i);

            // Getting data from individual JSONObject
            String message = jsonObj.getString("msg");
            messages.add(message);

        }

        ArrayAdapter<String> adapter = new ArrayAdapter<String>(EventSelectActivity.this,
                android.R.layout.simple_list_item_1, messages);
        ListView list=(ListView)findViewById(R.id.eventList);
        list.setAdapter(adapter);
        list.setOnItemClickListener(new AdapterView.OnItemClickListener()
        {

            @Override
            public void onItemClick(AdapterView<?> parent, View view,
                                    int position, long id) {
                Toast.makeText(EventSelectActivity.this, "TEST List View", Toast.LENGTH_SHORT).show();

            }
        });

    } catch (JSONException e) {
        e.printStackTrace();
    }


}

}

2 Answers2

1

The variable sb holds the consumed JSON data.

There was no reference to actual JSON object as it was empty.

JSONObject jsonObjMain = new JSONObject();

Change the code block:

try {
   JSONObject jsonObjMain = new JSONObject(sb.toString());
   JSONArray jsonArray = jsonObjMain.getJSONArray("events");
   // ... 

There is a undefined reference to the JSON field, "msg", maybe it should be "event"?

// Getting data from individual JSONObject
String message = jsonArray.getString(i);
messages.add(message);
t0mm13b
  • 34,087
  • 8
  • 78
  • 110
0

Change to below code :

    public class JsonActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_json);

        // Reading json file from assets folder
        StringBuffer sb = new StringBuffer();
        BufferedReader br = null;
        try {
            br = new BufferedReader(new InputStreamReader(getAssets().open(
                    "eventList.json")));
            String temp;
            while ((temp = br.readLine()) != null)
                sb.append(temp);
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if ((br != null)) {
                try {
                    br.close(); // stop reading
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }

        try {
            JSONObject jsonObjMain = new JSONObject(sb.toString());
            JSONArray jsonArray = jsonObjMain.getJSONArray("events");

            ArrayList<String> messages = new ArrayList<String>();
            for (int i = 0; i < jsonArray.length(); i++) {
                // Creating JSONObject from JSONArray
                JSONObject object = jsonArray.getJSONObject(i);
                String message = object.getString("event");
                messages.add(message);
            }

            ArrayAdapter<String> adapter = new ArrayAdapter<String>(JsonActivity.this,
                    android.R.layout.simple_list_item_1, messages);
            ListView list = (ListView) findViewById(R.id.eventList);
            list.setAdapter(adapter);
            list.setOnItemClickListener(new AdapterView.OnItemClickListener() {

                @Override
                public void onItemClick(AdapterView<?> parent, View view,
                                        int position, long id) {
                    Toast.makeText(JsonActivity.this, "TEST List View", Toast.LENGTH_SHORT).show();

                }
            });

        } catch (JSONException e) {
            e.printStackTrace();
        }
    }


}
Ashwini
  • 245
  • 1
  • 7
  • @saeidjafari : Check the above code. I have edited it. It will work now. – Ashwini May 24 '16 at 06:19
  • Make sure you have created assets folder. Try printing "sb.toString()" if value is null or not. I tried this code and its working for me. – Ashwini May 25 '16 at 04:21