Search code examples
phpjavascriptjsontitaniumappcelerator

JSON array returns empty using appcelerator/titanium


I'm writing an iPhone app at the moment using Appcelerator's Titanium. One of the requirements of the app is to be able to retrieve a JSON array of recently played songs, and display this in a tableView.

I have a separate tab in the app that parses JSON from twitter without a problem, however no matter what I do, I cannot get appcelerator to parse my generated json.

The JSON is generated by a PHP file located on my server using json_encode();

This is my json:

[
    {
        "title": "Cramp Pres. Sliders - Meteor"
    },
    {
        "title": "Progressiver pres. FineSky - Fine Sky"
    },
    {
        "title": "Armin Van Buuren #x26; DJ Shah feat. Chris Jones - Going Wrong (Alex M.O.R.P.H #x26; Woody Van Eyden Remix)"
    },
    {
        "title": "Bart Claessen - Hartseer"
    },
    {
        "title": "Will Holland feat. Jeza - Start Again (Juventa Remix)"
    }
]

I've checked this with numerous JSON validators just to be sure, and it is indeed valid.

This is my JS:

var loader = Ti.Network.createHTTPClient();
loader.setTimeout(10000);

loader.open("GET","http://myurl.com/service/played.json");

loader.onload = function()
{
    // Ti.API.info writes information to the debug console.
    Titanium.API.info('Loaded! Status: ' + this.status);
    Titanium.API.info('Response Header: ' + this.getResponseHeader('Content-Type'));
    Titanium.API.info('Response Text: ' + this.responseText);
}

loader.send();

In the app though, the responseText is simply coming back as [] - That's it. Nothing else. I've inspected the structure of my JSON and the JSON that the twitter API returns, and both seem to be layed out pretty much the same, only the twitter API is coming back with a lot more info as you can imagine.

Here are the steps I have tried:

  1. I saved the PHP generated JSON into a text file called "test.json" and attempted to parse it.. This worked fine!

  2. Previously the web service I was writing required an ?action=played query string. I removed this to ensure I was definately retreiving a JSON array. This made no difference

  3. I have sent headers in PHP for text/plain AND application/json to no avail

  4. Finally, I have employed mod_rewrite on my server so I can serve the output from the web service as a json file.. I.e. played.json -- Still, an empty array.

I just feel like i'm repeatedly banging my head against a brick wall, and would appreciate some insight from some less stressed people :)

Any help would be greatly appreciated!

Kind Regards,

Dave

More information: I've found by inspecting the http headers that the content-length returned is always 2. However, viewing the page in the browser, or the generated .json file, shows a full JSON array.

As previously suspected, this seems to be coming down to a PHP related issue now. The way the script works is simple. It queries a MySQL table to retrieve a list of songs. While it's looping through the array it populates an array called $songs

At the end of the function, I simply use:

echo json_encode($songs);

Even more info If I remove the content-type header and view the page in the browser, I receive the JSON array.. If I then copy that and paste it into the end of my PHP file, after the closing tag, the data I paste in is displayed.


Solution

  • Thanks for the answers everyone.

    I have investigated further and found that the problem seemed to lie with the way my PHP script was creating the JSON array.

    Although the JSON validated, and was visible both in the browser and in the .json file I was generating, for some reason it wasn't being created? Really strange, and I still cannot get my head around it.

    However, I decided the problem may of lay within the way I was parsing the XML feed that provided the song information. Funnily enough, it did. The code I had previously used on my website, works perfectly, but appcelerator didn't like it one little bit.

    So I took the XML feed and ran it through simplexml_load_string() and was able to use that directly in php's json_encode() function. BAM.. It worked.

    Thanks for the time u guys put in, it's really appreciated!

    Dave