我目前正在使用以下脚本打印一个 json 文件
<?PHP
include ('connect.php');
$get_student = mysql_query("SELECT * FROM student ORDER BY name asc");
$anArray = json_decode ($data);
while ($row = mysql_fetch_assoc($get_student)) {
$anArray[] = $row;
}
header("Content-type: application/json");
echo json_encode ($anArray, JSON_PRETTY_PRINT)
?>
然后我使用 AFNETWORKING 在 xcode 中获取 json 文件并将其保存到文档目录中
url = [NSURL URLWithString:@"url/Json3.php"];
NSURLRequest *request = [[NSURLRequest alloc] initWithURL:url];
AFHTTPRequestOperation *downloadOperation = [[AFHTTPRequestOperation alloc] initWithRequest:request];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *path = [[paths objectAtIndex:0] stringByAppendingPathComponent:@"database"];
downloadOperation.outputStream = [NSOutputStream outputStreamToFileAtPath:path append:NO];
[downloadOperation start];
然后,我尝试将下载的文档作为 JSON 文件读取,其中包含以下内容
NSURL *JsonUrl = [NSURL fileURLWithPath:path];
NSURLRequest *JSONrequest = [[NSURLRequest alloc] initWithURL:JsonUrl];
AFJSONRequestOperation *operation = [AFJSONRequestOperation JSONRequestOperationWithRequest:JSONrequest success:^(NSURLRequest *request, NSHTTPURLResponse *response, id JSON) {
anArray = JSON;
} failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error, id JSON) {
NSLog(@"Request Failed with Error: %@, %@", error, error.userInfo);
}];
[operation start];
但是 AFNetworking 在日志中给了我以下错误行
请求失败并出现错误:错误域 = AFNetworkingErrorDomain 代码 =-1016 “预期的内容类型 {(“text/json”、“application/json”、“text/javascript”)},得到文本/纯文本“UserInfo=0xa49f990 {NSLocalizedRecoverySuggestion= [
然后在日志中显示文本文档内容
问题
如何制作 PHP JSON Print,打印 Application/Json 文档而不是文本文档?
谢谢