0

My app fetches data from a Google Cloud Endpoint backend and this data is in json format.When I view this data in a ListView, this is what I get:

enter image description here

which is expected since I haven't done any form of parsing or conversion. My target output is this:

enter image description here

I have searched and searched for a way to do this with no luck. One example uses a different method and IMO is way too complicated for something I feel shouldn't take more than 5 lines of code.Another reads its data from a file and so do several others.

This is from the AsyncTask where am making the call to the endpoint:

@Override
protected void onPostExecute(List<Student> result) {
    ArrayAdapter<Student> arrayAdapter = new ArrayAdapter<Student>(getApplicationContext(), R.layout.row_text, result);
    studentListView.setAdapter(new ArrayAdapter<Student>(getApplicationContext(),  R.layout.activity_endpoint_launcher, result));
    studentListView.setAdapter(arrayAdapter);
}

I tried adding this:

String jsString = result.toString();
try {
    JSONObject jsonObject = new JSONObject(jsString);
    String name = jsonObject.getString(TAG_NAME);
    Log.d("EndpointLauncher", name);
} catch (JSONException e) {
    e.printStackTrace();
}

but I get the error (which doesn't crash the app though):

W/System.err﹕ org.json.JSONException: Value [{"grade":50,"matriculationNumber":"453","name":"Vera Brown"},{"grade":90,"matriculationNumber":"123456","name":"Sam Sung"},{"grade":90,"matriculationNumber":"654321","name":"To Shiba"},{"grade":90,"matriculationNumber":"876123","name":"Ann Droid"}] of type org.json.JSONArray cannot be converted to JSONObject

Any help would be greatly appreciated.

Community
  • 1
  • 1
Ojonugwa Jude Ochalifu
  • 26,627
  • 26
  • 120
  • 132

2 Answers2

1

the issue is its returning a jsonarray and you are casting it to an object, try this

JSONArray arr = new JSONArray(jsString);  

Then you can do

arr.getJSONObject(0).getString(TAG_NAME);

where you can loop over every element in the json array and put it in a new class

List<Student> students = new ArrayList<Student>();
for(int i =0; i < arr.length(); i ++)
{
    JSONObject jObject = arr.getJSONObject(i);
   Student newS = new Student();
   newS.name = jObject.getString("name");
    etc...
   students.add(newS);
}
Tomer Shemesh
  • 10,278
  • 4
  • 21
  • 44
0

So I finally found a way around this.First of all, I needed to create a new Student class in my main project package (app) and store values I was getting from the backend in objects of this class, and not one from the backend package.

In the end, this was what I got (

 public class EndpointAsyncTask extends AsyncTask(...) {
 ....
 @Override
    protected void onPostExecute(List<Student> result) {

        String jsString = result.toString();
        studentList = new ArrayList<NewStudentClass>(); 
        Student students;

        try {
            JSONArray jsonArray = new JSONArray(jsString);

            for (int i = 0; i  < jsonArray.length(); i++) {
                students = result.get(i);

                String name = students.getName();
                int grade = students.getGrade();
                long matricNumber = students.getMatriculationNumber();


                NewStudentClass st = new NewStudentClass();
                st.setMatriculationNumber(matricNumber);
                st.setGrade(grade);
                st.setName(name);

                studentList.add(st);
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }

ArrayAdapter<NewStudentClass> arrayAdapter = new ArrayAdapter<NewStudentClass>(getApplicationContext(), R.layout.row_text, studentList);
        studentListView.setAdapter(new ArrayAdapter<NewStudentClass>(getApplicationContext(), R.layout.activity_endpoint_launcher, studentList));
        studentListView.setAdapter(arrayAdapter);
 }
 }

When the toString() method is called on the NewStudentClass instances from the ListView, I get the output I wanted.

enter image description here

Ojonugwa Jude Ochalifu
  • 26,627
  • 26
  • 120
  • 132