0

現在、次のスクリプトを使用して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 では、ログに次のエラー行が表示されます

リクエストがエラーで失敗しました: エラー Domain=AFNetworkingErrorDomain Code=-1016 "Expected content type {( "text/json", "application/json", "text/javascript" )}, got text/plain" UserInfo=0xa49f990 {NSLocalizedRecoverySuggestion= [

ログにテキストドキュメントの内容を表示します

質問

テキストドキュメントではなく、アプリケーション/JsonドキュメントをPHP JSON Printで印刷するにはどうすればよいですか?

ありがとう

4

3 に答える 3

0

php 終了タグの後にスペースがないことを connect.php の内容で確認するか、単に ?> を削除します。

于 2013-04-08T14:18:35.053 に答える
0

ヘッダーが既に送信された問題であると仮定すると、「connect.php」の一番上に、

ob_start();

これは出力バッファリングを開始します。これは基本的に、すべてがレンダリングされるまで (またはバッファが送信されるか停止されるまで)、php はブラウザに何も送信しないことを意味します。

それ以外の場合で、データをjsonファイルに保存したいだけの場合は、使用できます

file_put_contents('myfile.json', json_encode($anArray, JSON_PRETTY_PRINT));

また、json_encode 関数に渡すフラグに問題がある可能性もあります。ちょっとした考え。

于 2013-04-08T13:38:43.847 に答える
0

OK、印刷の問題は解決しませんでしたが、単純なコード行で機能しました

    [AFJSONRequestOperation addAcceptableContentTypes:[NSSet setWithObject:@"text/plain"]];

これが同様の問題を抱えている人に役立つことを願っています

于 2013-04-10T08:44:03.103 に答える