0

这是我在我的主机上运行的 php 中的 web 服务。

<?php  
    $data = array("T1","T2","T3","T4","T5");
    header('Content-Type: application/json');
    echo json_encode($data);
?>

这是 xcode 片段。

NSURL * url = [NSURL URLWithString:myDinUrl];
NSData *urlData = [NSData dataWithContentsOfURL:url];

如果 Y 在我的网络浏览器中执行 url,你会得到:

["T1","T2","T3","T4","T5"]

但是如果我显示错误描述,Xcode 输出总是显示urlData为一个空对象(0x000000000)Operation couldn't be completed code=256

如果我使用另一个 URL,例如:Url1Url1NSData对象会得到一些可以格式化和解析的字节。

4

1 回答 1

0

Since your PHP encodes the response in JSON, you have to parse it expecting a JSON-encoded response.

NSJSONSerialization can do this for you, but it requires NSArray or NSDictionary (that means you must change your NSData declaration to either one of these.)

于 2014-02-19T19:23:27.313 回答