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.

2 Answers 2

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);
}
Sign up to request clarification or add additional context in comments.

6 Comments

@ojonugwaochalifu where?
In arr.get(0).getString(TAG_NAME);
Hi Tomer, I used your example and am able to view the student name in the log but still having problems formatting it in the listview.However, your example has given me a good place to work from, I'll get back when I sort it out.In your code, the for loop uses j.length(), don't you mean jsonArray.length()?
@ojonugwaochalifu sorry yes it is suppoesd to be arr.length
I finally solved it.I had to create a new Student class though, just like you said.
|
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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.