- Home /
Trying to parse Facebook FQL response with String.Split in webplayer; no luck
I finally worked out how to do it on the html side of things, I am successfully getting the entire response.data into a String ( which I have verified with console.log ) and sending it to the Unity webplayer. But then I am failing to decode it in Unity, which should have been the easy part.
I can't figure out what I'm doing wrong here.
function ParseFQLResponse (inString : String)
{
var chopChar : char[] = new char[1];
chopChar[0] = ";"[0];
var diceChar : char[] = new char[1];
diceChar[0] = ","[0];
var chop : String[] = inString.Split(chopChar);
for (var p : int = 0; p < chop.Length; p++)
{
var dice : String[] = chop[p].Split(diceChar);
if (dice.Length == 4)
{
fbFriendIDs.Add(dice[0]);
fbFriendNames.Add(dice[1]);
fbFriendsOnline.Add(dice[2]);
fbFriendsWithGame.Add(dice[3]);
}
else
{
playerText.text = "oops "+dice.Length;
}
}
playerText.text = fbFriendIDs.Count+","+fbFriendNames.Count+","+fbFriendsOnline.Count+","+fbFriendsWithGame.Count+" "+fbFriendIDs[0]+" "+fbFriendNames[0]+" "+fbFriendsOnline[0]+" "+fbFriendsWithGame[0];
ProcessFBFriendList();
}
This gives me the message "oops 1". The second message never appears.
This is the Javascript function on the webpage where the player is embedded:
function OnUnityReady()
{
GetUnity().SendMessage("_MasterControl","PersonalizeFBData",gPlayerFBID);
var outString = "";
FB.api('fql', {q : 'SELECT uid, first_name, online_presence, is_app_user FROM user WHERE uid IN ( SELECT uid2 FROM friend WHERE uid1 = me())'}, function(response) {
console.log(response.data.length);
for (var i = 0; i < response.data.length; i++)
{
outString += (response.data[i].uid + "," + response.data[i].first_name + "," + response.data[i].online_presence + "," + response.data[i].is_app_user + ";");
}
console.log(outString);
});
GetUnity().SendMessage("_MasterControl","ParseFQLResponse",outString);
}
Answer by Graham-Dunnett · Mar 06, 2013 at 12:48 PM
This is a classic example of why doing some testing is so critical. You're trying to do a facebook thing, I know, but in reality, all that ParseFQLResponse()
does it break up a string that you send it. So, make up some simple tests:
ParseFQLResponse("");
ParseFQLResponse("a,b,c,d;");
ParseFQLResponse("a,b,c,d;1,2,3,4;");
Obviously you'll want to comment out the call into ProcessFBFriendList()
. Do this from Unity, and add debugging code to your function to see what actually happens. My suspicion is that the Split("fred;", ";")
will give you two results, fred
and an empty string, where you expect to get just a single value back.
Your suspicion was right- and I made a function to write to the JavaScript console with an external call, since I was testing online and couldn't do Debug.Log. The onscreen text$$anonymous$$esh was a poor substitute because the last thing sent replaces all the rest, and the "oops 0" made me think it wasn't working for any of them.
What I had to do was modify the web script so it output something like ";,a1,b1,c1,d1;,a2,b2,c2,d2;,a1,b1,c1,d1" so that the empty string is now at the start, and then check for 5 values ins$$anonymous$$d of 4 and start the for loop at [1] ins$$anonymous$$d of [0] to skip over it.
Which made me feel like String.Split is stupid, but after reading a post on StackOverflow by someone who felt the same way and dozens of replies by people with far thicker neckbeards and richer monitor tans than my own explaining at great length why you really DO want it to return an empty string... I will just have to take their word for it and downgrade my opinion to "String.Split is unintuitive."
The one other problem which took me longer to figure out was that I was sending the string after the FB.api callback ins$$anonymous$$d of inside it, which meant it actually happened before, so the string was still empty. Now it finally works fine.