I am currently using the following script to print a json file
我目前正在使用以下脚本打印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)
?>
I am then fetching the json file in xcode using AFNETWORKING and saving it into the document directory
然后,我使用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];
I am then trying to read the downloaded document as a JSON file with the following
然后,我尝试以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];
However AFNetworking gives me the following error line in log
然而,AFNetworking在日志中给我提供以下错误行
Request Failed with Error: Error Domain=AFNetworkingErrorDomain Code=-1016 "Expected content type {( "text/json", "application/json", "text/javascript" )}, got text/plain" UserInfo=0xa49f990 {NSLocalizedRecoverySuggestion=[
错误请求失败:ErrorDomain =AFNetworkingErrorDomain Code=-1016“预期内容类型{(“text/json”、“application/json”、“text/javascript”)},获取text/plain”UserInfo=0xa49f990 {nslocalizedrecoverysuggest =[
and then shows the text documents contents in the log
然后显示日志中的文本文档内容
QUESTION
问题
How do I make the PHP JSON Print, print a Application/Json document and not a text document?
如何使PHP JSON打印、打印应用程序/ JSON文档而不是文本文档?
Thanks
谢谢
3 个解决方案
#1
0
Assuming it is a headers already sent problem, in your "connect.php", at the very top, put
假设它是一个已经发送了问题的标题,在您的“连接”中。在最上面写上php
ob_start();
This will start outbut buffering which basically means, php doesnt send anything to the browser until everything is rendered (or until the buffer is sent, stopped etc..)
这将从缓存开始,这基本上意味着,php在呈现所有内容之前不会向浏览器发送任何内容(或者直到发送缓冲区,停止等等)。
If its something else and you just want to save your data into a json file, you can use
如果它是别的东西,而您只想将数据保存到json文件中,您可以使用
file_put_contents('myfile.json', json_encode($anArray, JSON_PRETTY_PRINT));
Also, it could be a problem with the flags you are passing to the json_encode function. Just a thought.
此外,传递给json_encode函数的标志也可能存在问题。只是一个想法。
#2
0
Check content of connect.php that no spaces are after the php end tag, or just remove ?>
检查连接的内容。php结束标记后没有空格,还是删除?>
#3
0
Ok i didnt resolve the print issue but a simple line of code made it work
好的,我没有解决打印问题,但是简单的一行代码使它工作
[AFJSONRequestOperation addAcceptableContentTypes:[NSSet setWithObject:@"text/plain"]];
hope this helps somebody with a similar issue
希望这能对有类似问题的人有所帮助
#1
0
Assuming it is a headers already sent problem, in your "connect.php", at the very top, put
假设它是一个已经发送了问题的标题,在您的“连接”中。在最上面写上php
ob_start();
This will start outbut buffering which basically means, php doesnt send anything to the browser until everything is rendered (or until the buffer is sent, stopped etc..)
这将从缓存开始,这基本上意味着,php在呈现所有内容之前不会向浏览器发送任何内容(或者直到发送缓冲区,停止等等)。
If its something else and you just want to save your data into a json file, you can use
如果它是别的东西,而您只想将数据保存到json文件中,您可以使用
file_put_contents('myfile.json', json_encode($anArray, JSON_PRETTY_PRINT));
Also, it could be a problem with the flags you are passing to the json_encode function. Just a thought.
此外,传递给json_encode函数的标志也可能存在问题。只是一个想法。
#2
0
Check content of connect.php that no spaces are after the php end tag, or just remove ?>
检查连接的内容。php结束标记后没有空格,还是删除?>
#3
0
Ok i didnt resolve the print issue but a simple line of code made it work
好的,我没有解决打印问题,但是简单的一行代码使它工作
[AFJSONRequestOperation addAcceptableContentTypes:[NSSet setWithObject:@"text/plain"]];
hope this helps somebody with a similar issue
希望这能对有类似问题的人有所帮助