2016-09-25 205 views
0

我試圖使用JSON並將其顯示在Android列表視圖中。錯誤:org.json.JSONException:類型java.lang.String的值http無法轉換爲JSONObject

這是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" 
    }] 
} 

我的AsyncTask在安卓

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

不知道爲什麼我收到錯誤:

E/JSONException: Error: org.json.JSONException: Value http of type java.lang.String cannot be converted to JSONObject 
+0

在嘗試轉換爲JSONObject之前記錄結果的值 –

+0

您的錯誤顯示爲「值http」,因此您似乎沒有從doInBackround返回正確的值 –

+1

感謝是的,幫助我返回了url,而不是json字符串。謝謝你 ! – headcrabz

回答

0

的字符串結果要傳遞onPostExecute()方法顯然不是JSON字符串。

此外,爲什麼不在doInBackground()中解析JSON字符串。後臺線程是執行此類任務的更好選擇 - 如果可能,請保留在UI線程上運行的onPostExecute()方法,以便在UI線程上進行操作。 JSON解析絕對可以在後臺完成。

相關問題