Skip to content Skip to sidebar Skip to footer

What Should I Do To Solve This Error?

I was using JSONParser to obtain results of a search, for that I followed this tutorial: http://www.androidhive.info/2012/01/android-json-parsing-tutorial/ The thing is that, the A

Solution 1:

Your JSON sample is a poor way to organize the results: mixing the number in with the result objects. Is the number supposed to indicate the number of objects in the array or something else?

If you can assume that this number will always be the first element, and according to this then it's supposed to work this way, you can try to read the first value of the array outside the loop:

response = json.getJSONArray(TAG_RESPONSE);

// from your example, num will be 50036:
num = response.getInt(0);

for (inti=1; i < response.length(); i++){
    JSONObjectc= response.getJSONObject(i);

Note that the example in the linked documentation has this number as a string:

{"response":
    ["5",
     {"aid":"60830458","owner_id":"6492","artist":"Noname","title":"Bosco",
"duration":"195","url":"http:\/\/cs40.vkontakte.ru\/u06492\/audio\/2ce49d2b88.mp3"},
     {"aid":"59317035","owner_id":"6492","artist":"Mestre Barrao","title":"Sinhazinha",
"duration":"234","url":"http:\/\/cs510.vkontakte.ru\/u2082836\/audio\/
d100f76cb84e.mp3"}]}

But JSONArray.getInt() will parse the String as an int for you.

And notice that some of the values in the objects in your array are also numbers, you may want to read those as int also:

int aid = c.getInt(TAG_AID);
int owner_id = c.getInt(TAG_OWNER_ID);
int duration = c.getInt(TAG_DURATION);

Solution 2:

A lot of the values you are trying to parse in are not String objects, specifically "aid", "owner_id", and "duration". Use the correct method to retrieve values. For example:

intaid= c.getInt(TAG_AID);
intowner_id= c.getInt(TAG_OWNER_ID);
Stringartist= c.getString(TAG_ARTIST);
Stringtitle= c.getString(TAG_TITLE);
intduration= c.getInt(TAG_DURATION);

edit: Another error that I missed is you start your array with 50036. This is not a JSONObject and cannot be parsed as so. You can add a conditional statement to check if it's array index 0 to parse the int using getInt(), and then parse as JSONObjects for the rest of the array values.

Solution 3:

Try changing

response = json.getJSONArray(TAG_RESPONSE);

into

response = (JSONObject)json.getJSONArray(TAG_RESPONSE);

I dont have any experience with JSONObject, but works often with type mismatches.

Solution 4:

Try putting 50036 in quotes like this "50036" .

Post a Comment for "What Should I Do To Solve This Error?"