2

I'm trying to write an iOS (5) app that uses json objects to get data from a server. im calling a php script from the app, that gets the data from the server, encodes it as json, and then outputs it. the line in the php script that does that is:

$json_encoded =  json_encode($normal_array, JSON_FORCE_OBJECT)
echo $json_encoded;

but when I do:

NSURL *url = [NSURL URLWithString:@"192.168.1.100/testing/get.php?number=5"];
NSData *data = [NSData dataWithContentsOfURL:url];

the data variable is of size 0 bytes and contains no data.

Can anyone tell me why the dataWithContentsOfURL gets something thats empty? Is it not enough to echo? Do i need to send a response or something like that? If so, how would I send a response from the php script? If i call the exact same script in a browser, it outputs:

{
    "0": {
        "0":"asdfgh",
        "1":"2012-06-11 16:21:15"
    },
    "1": {
        "0":"jumps over",
        "1":"2012-06-11 16:20:52"
    },
    "2": {
        "0":"the quick brown fox",
        "1":"2012-06-11 16:20:40"
    },
    "3": {
        "0":"the quick brown fox",
        "1":"2012-06-11 16:20:40"
    },
    "4": {
        "0":"the quick brown fox",
        "1":"2012-06-11 16:20:21"
    }
}

which is a valid JSON object, as far as I know.

0

3 Answers 3

1

Make sure you send a header with a content type.

header('Content-type: application/json');
print json_encode($array);
exit();
Sign up to request clarification or add additional context in comments.

Comments

0

My guess is that your device doesnt see that local ip address? Either that or maybe it's json parsing is much stricter. Did you output the json header with your php script?

Comments

0

The url outputs string, so use the following

NSURL *url = [NSURL URLWithString:@"192.168.1.100/testing/get.php?number=5"];
NSString *jsonVal = [NSString stringWithContentsOfURL:url];
NSLog(jsonVal);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.