Parsing JSON from url with exception: Error parsing data org.json.JSONException: Unterminated array at character 115

后端 未结 3 1469
被撕碎了的回忆
被撕碎了的回忆 2020-12-22 05:59

I\'m parsing this url and I\'ve got an exception and don\'t know how to skip it over... I need to get only names of 100 the most popular apps. There is key \"im:name\" and i

3条回答
  •  一整个雨季
    2020-12-22 06:17

    Thanks to njzk2 for the advice to avoid using JSONParser, so I've made all I needed in very shorter time and with less efforts. I needed to get root JSONObject - names of applications in iTunes. and insert it into database. This is the full code of the method doInBackground in AsyncTask.class

    @Override
        protected Boolean doInBackground(Void... params) {
            Log.d(LOG, "AsyncTask. Do in background");
            ContentValues cv = new ContentValues();
    
            try {//Making a request to server getting entities
                JSONObject json = new JSONObject(EntityUtils.toString(
                        new DefaultHttpClient().execute(
                                new HttpGet(url)).getEntity()));
                //getting json root object
                JSONObject feedObject = json.getJSONObject("feed");
                //getting needed array in root json object
                JSONArray entryArray = feedObject.getJSONArray("entry");
    
                //moving though the array
                for(int i = 0; i < entryArray.length(); i++){
                    //getting all objects in array
                    JSONObject entryObjects = entryArray.getJSONObject(i);
                    //taking objects with needed key
                    JSONObject nameObject = entryObjects.getJSONObject("im:name");
                    //getting string name
                    String name = nameObject.getString("label");
                    //putting data into ContentValues - name and id (for making a numbers
                    // next to records in ListView)
                    cv.put(DB.COL_NAME, name);
                    cv.put(DB.COL_ID, i+1);
                    mDB.insert(cv);
                    //just controlling in log
                    Log.d(LOG, "" + name);
                }
            } catch (JSONException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
            return true;
        }
    

提交回复
热议问题