-2
 @Override
    protected String doInBackground(String... params) {

        try{
        JSONParser jParser = new JSONParser();
        String url="http://earlykid.com/android/?page=2";
        IdArray=jParser.getJSONFromUrl(url);
        Log.e("Fetch Data",IdArray.toString());
        mLatestList.clear();
        for(int i=0;i<IdArray.length();i++){
            try{
                 JSONObject jObject;
                 mKids=new Kids();
                 jObject=IdArray.getJSONObject(i);
                 mKids.SetTotalPages(jObject.getString("totalItems"));
                 mKids.SetCurrentPage(jObject.getString("currentPage"));
            }catch(JSONException e){
                 Log.e("log_tag", "Error parsing data "+e.toString());
                    Log.e("log_tag", "Failed data was:\n" + IdArray);
            }
        }


    }catch(Exception e){

    }

    return null;
}

    @Override
    protected void onPostExecute(String result) {
        mProgress.dismiss();

}

When i fetch the data from this code.it shows me error.

Logcat here:-

error parsing data org.json.JSONException: Value {"totalItems":38,"currentPage":"2","items":[{"id":"Atb1lE9_wzk","title":"ABCD Alphabets Song - Songs for Kids","category":"Education","thumbnail":"http:\/\/www.earlykid.com\/android\/timthumb.php?w=600&h=330src=http:\/\/i1.ytimg.com\/vi\/Atb1lE9_wzk\/hqdefault.jpg"},{"id":"UXeeSU0QNro","title":"The rich man and his sons story - Animated Stories for Kids","category":"Education","thumbnail":"http:\/\/www.earlykid.com\/android\/timthumb.php?w=600&h=330src=http:\/\/i1.ytimg.com\/vi\/UXeeSU0QNro\/hqdefault.jpg"},{"id":"HmiyKDYrELk","title":"Here we go round the mulberry bush - Nursery Rhyme for Kids","category":"Education","thumbnail":"http:\/\/www.earlykid.com\/android\/timthumb.php?w=600&h=330src=http:\/\/i1.ytimg.com\/vi\/HmiyKDYrELk\/hqdefault.jpg"},{"id":"9TLnCurMs5c","title":"Old Mac Donald had a farm - Nursery Rhymes for Kids","category":"Education","thumbnail":"http:\/\/www.earlykid.com\/android\/timthumb.php?w=600&h=330src=http:\/\/i1.ytimg.com\/vi\/9TLnCurMs5c\/hqdefault.jpg"},{"id":"DPQ_5GR_MMo","title":"Five Little Monkeys jumping on the bed - Nursery Rhymes","category":"Education","thumbnail":"http:\/\/www.earlykid.com\/android\/timthumb.php?w=600&h=330src=http:\/\/i1.ytimg.com\/vi\/DPQ_5GR_MMo\/hqdefault.jpg"},{"id":"CvwHp2xFlJw","title":"Rain Rain go away - Nursery Rhyme","category":"Education","thumbnail":"http:\/\/www.earlykid.com\/android\/timthumb.php?w=600&h=330src=http:\/\/i1.ytimg.com\/vi\/CvwHp2xFlJw\/hqdefault.jpg"},{"id":"WEVA9iF6i3s","title":"I'm a little teapot Nursery Rhyme with lyrics","category":"Education","thumbnail":"http:\/\/www.earlykid.com\/android\/timthumb.php?w=600&h=330src=http:\/\/i1.ytimg.com\/vi\/WEVA9iF6i3s\/hqdefault.jpg"},{"id":"TQHyRssAM5Y","title":"Ten little fingers ten little toes - Nursery Rhyme","category":"Education","thumbnail":"http:\/\/www.earlykid.com\/android\/timthumb.php?w=600&h=330src=http:\/\/i1.ytimg.com\/vi\/TQHyRssAM5Y\/hqdefault.jpg"},{"id":"fDGOlmgF1NE","title":"Jingle Bells Christmas Song","category":"Education","thumbnail":"http:\/\/www.earlykid.com\/android\/timthumb.php?w=600&h=330src=http:\/\/i1.ytimg.com\/vi\/fDGOlmgF1NE\/hqdefault.jpg"},{"id":"Y83fbhN6FBk","title":"Pussy Cat Pussy Cat where have you been? - Nursery Rhyme","category":"Film","thumbnail":"http:\/\/www.earlykid.com\/android\/timthumb.php?w=600&h=330src=http:\/\/i1.ytimg.com\/vi\/Y83fbhN6FBk\/hqdefault.jpg"},{"id":"UuqNHZEIwEI","title":"Thank you for the world so sweet - Kids Song","category":"Film","thumbnail":"http:\/\/www.earlykid.com\/android\/timthumb.php?w=600&h=330src=http:\/\/i1.ytimg.com\/vi\/UuqNHZEIwEI\/hqdefault.jpg"},{"id":"g0u1iWUmg8Q","title":"Ding dong bell - Nursery Rhyme","category":"Film","thumbnail":"http:\/\/www.earlykid.com\/android\/timthumb.php?w=600&h=330src=http:\/\/i1.ytimg.com\/vi\/g0u1iWUmg8Q\/hqdefault.jpg"}]
4

2 回答 2

0

What you are getting is a JSONObject from this url http://earlykid.com/android/?page=2

You have this

       IdArray=jParser.getJSONFromUrl(url); // wrong. you get a json object

{ represents a json object node

[ represents a json array node

Your json

{
    "totalItems": 38,
    "totalPages": 3,
    "itemsPerPage": 12,
    "currentPage": "2",
    "items": [
        {
            "id": "Atb1lE9_wzk",
            "category": "Education",
            "title": "ABCD Alphabets Song - Songs for Kids",
            "thumbnail": "http://www.earlykid.com/android/timthumb.php?w=600&h=330src=http://i1.ytimg.com/vi/Atb1lE9_wzk/hqdefault.jpg"
        },
        {
            "id": "UXeeSU0QNro",
            "category": "Education",
            "title": "The rich man and his sons story - Animated Stories for Kids",
            "thumbnail": "http://www.earlykid.com/android/timthumb.php?w=600&h=330src=http://i1.ytimg.com/vi/UXeeSU0QNro/hqdefault.jpg"
        },
        {
            "id": "HmiyKDYrELk",
            "category": "Education",
            "title": "Here we go round the mulberry bush - Nursery Rhyme for Kids",
            "thumbnail": "http://www.earlykid.com/android/timthumb.php?w=600&h=330src=http://i1.ytimg.com/vi/HmiyKDYrELk/hqdefault.jpg"
        },
        {
            "id": "9TLnCurMs5c",
            "category": "Education",
            "title": "Old Mac Donald had a farm - Nursery Rhymes for Kids",
            "thumbnail": "http://www.earlykid.com/android/timthumb.php?w=600&h=330src=http://i1.ytimg.com/vi/9TLnCurMs5c/hqdefault.jpg"
        },
        {
            "id": "DPQ_5GR_MMo",
            "category": "Education",
            "title": "Five Little Monkeys jumping on the bed - Nursery Rhymes",
            "thumbnail": "http://www.earlykid.com/android/timthumb.php?w=600&h=330src=http://i1.ytimg.com/vi/DPQ_5GR_MMo/hqdefault.jpg"
        },
        {
            "id": "CvwHp2xFlJw",
            "category": "Education",
            "title": "Rain Rain go away - Nursery Rhyme",
            "thumbnail": "http://www.earlykid.com/android/timthumb.php?w=600&h=330src=http://i1.ytimg.com/vi/CvwHp2xFlJw/hqdefault.jpg"
        },
        {
            "id": "WEVA9iF6i3s",
            "category": "Education",
            "title": "I'm a little teapot Nursery Rhyme with lyrics",
            "thumbnail": "http://www.earlykid.com/android/timthumb.php?w=600&h=330src=http://i1.ytimg.com/vi/WEVA9iF6i3s/hqdefault.jpg"
        },
        {
            "id": "TQHyRssAM5Y",
            "category": "Education",
            "title": "Ten little fingers ten little toes - Nursery Rhyme",
            "thumbnail": "http://www.earlykid.com/android/timthumb.php?w=600&h=330src=http://i1.ytimg.com/vi/TQHyRssAM5Y/hqdefault.jpg"
        },
        {
            "id": "fDGOlmgF1NE",
            "category": "Education",
            "title": "Jingle Bells Christmas Song",
            "thumbnail": "http://www.earlykid.com/android/timthumb.php?w=600&h=330src=http://i1.ytimg.com/vi/fDGOlmgF1NE/hqdefault.jpg"
        },
        {
            "id": "Y83fbhN6FBk",
            "category": "Film",
            "title": "Pussy Cat Pussy Cat where have you been? - Nursery Rhyme",
            "thumbnail": "http://www.earlykid.com/android/timthumb.php?w=600&h=330src=http://i1.ytimg.com/vi/Y83fbhN6FBk/hqdefault.jpg"
        },
        {
            "id": "UuqNHZEIwEI",
            "category": "Film",
            "title": "Thank you for the world so sweet - Kids Song",
            "thumbnail": "http://www.earlykid.com/android/timthumb.php?w=600&h=330src=http://i1.ytimg.com/vi/UuqNHZEIwEI/hqdefault.jpg"
        },
        {
            "id": "g0u1iWUmg8Q",
            "category": "Film",
            "title": "Ding dong bell - Nursery Rhyme",
            "thumbnail": "http://www.earlykid.com/android/timthumb.php?w=600&h=330src=http://i1.ytimg.com/vi/g0u1iWUmg8Q/hqdefault.jpg"
        }
    ]
}

Parsing

       try {
        JSONObject jb = new JSONObject(myjsonstring);
        String totalitems = jb.getString("totalItems");
        Log.i("......", "" + totalitems);
        String totalpages = jb.getString("totalPages");
        String itemsPerPage = jb.getString("itemsPerPage");
        String currentPage = jb.getString("currentPage");
        JSONArray jarr = jb.getJSONArray("items");
        for (int i = 0; i < jarr.length(); i++) {
            JSONObject jb1 = jarr.getJSONObject(i);
            String id = jb1.getString("id");
            String categoy = jb1.getString("category");
            String title = jb1.getString("title");
            String pic = jb1.getString("thumbnail");
            Log.i("........", id);

        }
    } catch (Exception e) {

    }
于 2013-08-13T11:47:23.060 回答
0

Try this :

JsonObject oJsonObject = new JSONObject(myjsonstring);

if(oJsonObject.has("items"))
{
    oJsonArray  = oJsonObject.getJSONArray("items");

    for(int i = 0; i < oJsonArray.length(); i++)
    {
        String id        = oJsonArray.getJSONObject(i).getString("id");
        String title     = oJsonArray.getJSONObject(i).getString("title");
        String category  = oJsonArray.getJSONObject(i).getString("category");
        String thumbnail = oJsonArray.getJSONObject(i).getString("thumbnail");
    }
}

Actually in your JSON you have missed a } at the end of JOSN data so it is not a vlid JSON which you have posted over here

于 2013-08-13T11:52:27.890 回答