Go Back   Android Forums > Android Development > Application Development
Application Development Dev lounge for our application developers.

test: Reply
 
LinkBack Thread Tools
Old December 23rd, 2012, 08:04 AM   #1 (permalink)
Junior Member
Thread Author (OP)
 
Join Date: Dec 2012
Posts: 15
 
Device(s):
Carrier: Not Provided

Thanks: 0
Thanked 0 Times in 0 Posts
Default JSON Parser/For loop help

Recently I started developing a app that searches and API to find a game your looking for. A friend of mine lent me his twitter project to edit so I could make my own. I've been modifying it, but the problem I'm having right now is that the for loop is not returning the searched game.

The image below is an example of what the JSONParser should be getting if the user searched Far Cry 3.



The problem I think is that the for loop is not returning the name value and I currently have no idea why. Anyone know what my problem is?

[HIGH] private void loadTweetsJSON(String search) throws Exception
{
results = downloadTweets("http://api.giantbomb.com/search/?query=" + URLEncoder.encode(search, "UTF-8") + "&field_list=name,id&resources=game,concept&api_ke y=1234556789&format=json&offset=0");
System.out.println(results.toString());

JSONObject jsonResults = new JSONObject(results);
JSONArray resultsArray = jsonResults.getJSONArray("results");

listItems = new ArrayList<String>();

for (int i = 0; i < resultsArray.length(); i++)
{
JSONObject j = (JSONObject) resultsArray.get(i);
listItems.add(j.getString("name") + ": " + j.getString("text"));
}

handler.post(displayResultsRunnable);
}
[/HIGH]

EDIT: A quick update. After debugging the for loop I've noticed that the int i value never increases. This leads me to believe that the code is searching through the "results" array, but it is failing to find the value of my search string. Which in this case would be Far Cry 3. Once again though I'm not sure why it's doing this.

Advertisements
KarneeKarnay is offline  
Last edited by KarneeKarnay; December 23rd, 2012 at 05:20 PM.
Reply With Quote
sponsored links
Old December 24th, 2012, 10:28 PM   #2 (permalink)
Senior Member
 
jonbonazza's Avatar
 
Join Date: Jul 2010
Gender: Male
Posts: 1,935
 
Device(s): Nexus 4, Samsung Galaxy S II, HTC Evo 4G, Amazon Kindle Fire
Carrier: ATT

Thanks: 235
Thanked 482 Times in 294 Posts
Default

If the for loop only executes once, then that means that resultsArray.length() == 1, so the resultsArray only has 1 item in it.

Messing with raw json is usually best avoided if possible. I highly recommend taking a look at the Twitter4j library. It provides an easy to use high level wrapper around the Twitter apis. It's what we use in all of our apps and seems to be the best one we have found.
__________________
My blog, Inverted Keystrokes, is looking for aspiring developers (not necessarily with Android) to post articles. If you have any development experience and are interested in participating, please PM me. =)
jonbonazza is offline  
Reply With Quote
Old December 25th, 2012, 03:08 PM   #3 (permalink)
Junior Member
Thread Author (OP)
 
Join Date: Dec 2012
Posts: 15
 
Device(s):
Carrier: Not Provided

Thanks: 0
Thanked 0 Times in 0 Posts
Default

Quote:
Originally Posted by jonbonazza View Post
If the for loop only executes once, then that means that resultsArray.length() == 1, so the resultsArray only has 1 item in it.

Messing with raw json is usually best avoided if possible. I highly recommend taking a look at the Twitter4j library. It provides an easy to use high level wrapper around the Twitter apis. It's what we use in all of our apps and seems to be the best one we have found.
The problem is that i never seems to increase. I assumed that this is because it can not find the name variable. I don't think the for loop works at all, because it's just not finding the item. Either that or it has found it and is simply not converting the text value to the value in the name variable.
KarneeKarnay is offline  
Reply With Quote
Old December 25th, 2012, 04:16 PM   #4 (permalink)
Senior Member
 
jonbonazza's Avatar
 
Join Date: Jul 2010
Gender: Male
Posts: 1,935
 
Device(s): Nexus 4, Samsung Galaxy S II, HTC Evo 4G, Amazon Kindle Fire
Carrier: ATT

Thanks: 235
Thanked 482 Times in 294 Posts
Default

I think you are misunderstanding how a for loop works. In java, a for loops is really just a glorified while loop. the for loop you posted above is the same thing as the following:

[HIGH]
int i = 0;
while(i < resultsArray.length()) {
JSONObject j = (JSONObject) resultsArray.get(i);
listItems.add(j.getString("name") + ": " + j.getString("text"));
i++;
}
[/HIGH]

As you can see, i will always be updated as long as it enters the loop. The ONLY case where i will not change at all is if resultsArray.length() is equal to 0. In that case, the loop will never be entered and thus i will never be incremented. In short, the way your code is written, if the for loop is entered even once, i will be updated.

If you enter a log statement inside of the for loop, you can easily check to make sure that it is actually looping. for example, if you change your code the following:
[HIGH]
int i;
for (i = 0; i < resultsArray.length(); i++)
{
JSONObject j = (JSONObject) resultsArray.get(i);
listItems.add(j.getString("name") + ": " + j.getString("text"));
}
Log.d("loop_check", "i = " + i);
[/HIGH]

you can see exactly what i is when the loop exits. If i is equal to 0, then the loop was never executed.
jonbonazza is offline  
Reply With Quote
Old December 26th, 2012, 07:38 PM   #5 (permalink)
Junior Member
Thread Author (OP)
 
Join Date: Dec 2012
Posts: 15
 
Device(s):
Carrier: Not Provided

Thanks: 0
Thanked 0 Times in 0 Posts
Default

So I've done as you suggested and the output has been "0". It never increases. There is stuff in the array it just doesn't seem to get the array's length or it just ignores it. I've used the eclipse debugger to look at the values displayed in the "resultsArray" and it does indeed contain the all the information stored in the "results" array from the API.

My understanding is because of this problem with "i", it never downloads the contents of the array, and therefor there can't be anything to put as the text value or on the ListView in my program.

No that I defiantly know the problem is with the for loop, how do I get this to work?

EDIT: I just found something strange. The length of the array is equal to exactly 1...Not sure how that's possible.

EDIT2: I've found that the "resultsArray" length is equal to the amount of times there is a "results" json array on the page. The problem now has to be with the fact that the "text" value is not being converted into string associated with the "name :" string. I've taken some screenshots to help.



Once I realised the problem with the "resultsArray.length()" I changed my search query to "Far Cry" instead of "Far Cry 3". That way I got more than one result instead of the 1. I will change the for loop at a later time to fix this.



With this new search query I added the log code you suggested by the problem was that the program never reached it. Instead I had wrote a "catch" method that would pick up any errors and then print it in the log. Below is the output.



EDIT3:

I feel like the biggest of idiots the reason as to why there was no text value was because it didn't exist. :P There was the reason for the catch activating and for the program to stop working. I'm sorry this took up so much time. Thank you very much for all the help jonbonazza.

KarneeKarnay is offline  
Last edited by KarneeKarnay; December 27th, 2012 at 07:18 AM.
Reply With Quote
Old December 27th, 2012, 09:13 AM   #6 (permalink)
Senior Member
 
jonbonazza's Avatar
 
Join Date: Jul 2010
Gender: Male
Posts: 1,935
 
Device(s): Nexus 4, Samsung Galaxy S II, HTC Evo 4G, Amazon Kindle Fire
Carrier: ATT

Thanks: 235
Thanked 482 Times in 294 Posts
Default

Glad you got it sorted. Sorry I wasn't of more help.
jonbonazza is offline  
Reply With Quote
Reply


Go Back   Android Forums > Android Development > Application Development
Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT -5. The time now is 06:46 PM.
Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2014, vBulletin Solutions, Inc.