0

I am trying to consume a JSON and display it in an Android listview.

This is the JSON:

{
    "meta": {
        "limit": 20,
        "next": null,
        "offset": 0,
        "previous": null,
        "total_count": 5
    },
    "objects": [{
        "acad_rating": "4.58",
        "address": "Karawal Nagar Delhi-110094",
        "adm_close": "5/2/2016",
        "adm_open": "1/1/2016",
        "affiliated_till": "31/3/2017",
        "area": "Karawal Nagar (West)",
        "average_board_perc": null,
        "boardhighsec": "Cbse",
        "boardsec": "Cbse",
        "books_in_lib": 9684,
        "boyperc": "72.00",
        "category": " Primary With Upper Primary And Secondary And Higher Secondary(1-12) ",
        "cce_impl": true,
        "city": "Delhi",
        "cityslug": "delhi",
        "coed": "Co-Ed",
        "computer_aid_learn": true,
        "description": null,
        "district": "North East Delhi",
        "drinking_water": "Tap Water",
        "email": "[email protected]",
        "facilities_rating": "5.00",
        "fax": "4132622661",
        "girlperc": "28.00",
        "good_classrooms": 21,
        "grad_teachers": "92.16",
        "highestclass": 12,
        "image": null,
        "library": true,
        "lowestclass": 1,
        "medical_checkup_last_yr": true,
        "medium": " English ",
        "mid_day_meal": "Na",
        "minage": 3,
        "name": "Sardar Patel Public Sr.Sec. School",
        "no_classrooms": 21,
        "no_computers": 35,
        "overall_rating": "4.33",
        "padmin": 15,
        "parents_smc": 2,
        "phone": "22934441",
        "pincode": 110094,
        "playground": true,
        "pqualification": "B.A., M.Phil, M.Ed.",
        "principal": "Mohd Zahid Khan",
        "pteach": 9,
        "ptotal": 24,
        "ramp": true,
        "rating": "3.5",
        "residential_school": false,
        "resource_uri": "/api/schools/7030327101/",
        "school_mgt_com": true,
        "schoolid": "7030327101",
        "schooltype": "Private Unaided School",
        "since": 1985,
        "slug": "sardar-patel-public-srsec-school",
        "student_teacher": "33.45",
        "total_seats": 0,
        "total_smc": 18,
        "total_students": 1706,
        "total_teachers": 51,
        "totalews": 0,
        "trust": "Geeta Educational Society, Shera",
        "tuition": 0,
        "website": "http://www.auroville.org.in",
        "workdays_pr": 240,
        "written_test": true,
        "zone": "4"
    }]
}

My Asynctask in Android:

@Override
    protected void onPostExecute(String result) {

        try {
            JSONObject jObj = new JSONObject(result);
            String notes = jObj.getString("objects");
      } catch (JSONException e) {
            Log.e("JSONException", "Error: " + e.toString());
        }

Not sure why I am getting error:

E/JSONException: Error: org.json.JSONException: Value http of type java.lang.String cannot be converted to JSONObject
3
  • Log the value of result before you try to convert to a JSONObject Commented Sep 25, 2016 at 22:15
  • Your error says Value http, so you seem not to be returning the correct value from doInBackround Commented Sep 25, 2016 at 22:17
  • 1
    thanks yes that helped , I was returning the url and not the json string. Thank u ! Commented Sep 25, 2016 at 22:29

1 Answer 1

1

The String result that you are passing on to the onPostExecute() method is not a JSON string apparently.

Also, why not parse the JSON string as well within doInBackground(). The background thread is a better option to do such tasks - if possible, reserve the onPostExecute() method which runs on the UI thread for manipulations on the UI thread. And JSON parsing can definitely be done in the background.

Sign up to request clarification or add additional context in comments.

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.