シリアル化されたオブジェクトを iOS アプリから Web サーバーに送信しようとしていますが、通常はすべて機能します。巨大な文字列を送信しようとしましたが、サーバー上で問題なく送信されます。しかし、シリアライズされたオブジェクトをサーバーに送信すると、文字列が途切れてしまいます。アプリから文字列をログに記録しました-すべてそこにあります。Web サーバー上のファイルに同じ文字列を書き込もうとしましたが、最も奇妙な方法で途切れています。
その理由は何ですか?これはエンコーディングと関係があると思います。サーバーにデータを送信する方法は次のとおりです。
+ (void)postRequestWithData:(id)data toURL:(NSURL *)url {
// Prepare data
NSData *postData = [NSJSONSerialization dataWithJSONObject:data options:NSJSONWritingPrettyPrinted error:nil];
NSString *postDataString = [[NSString alloc] initWithData:postData encoding:NSUTF8StringEncoding];
NSString *requestString = [NSString stringWithFormat:@"sales=%@", postDataString];
NSString *postLength = [NSString stringWithFormat:@"%d", [requestString length]];
// Make the request
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
[request setURL:url];
[request setHTTPMethod:@"POST"];
[request setValue:postLength forHTTPHeaderField:@"Content-Length"];
[request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
[request setHTTPBody:[requestString dataUsingEncoding:NSUTF8StringEncoding allowLossyConversion:YES]];
NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:request delegate:self];
// Error reporting
if (!connection) {
NSLog(@"Could not make a connection!");
}
}
(id)data は辞書の配列です。
サーバーでそれを処理する方法は次のとおりです。
<?php
$sales = $_POST['sales'];
$myFile = "testFile.txt";
$fh = fopen($myFile, 'w') or die("can't open file");
fwrite($fh, $sales);
fclose($fh);
?>