although I've read lots of articles talking this, I still can not solve this problem.
First, This is the code of connection in IOS, I try to connect to server by a POST METHOD in my local network. (server: CENTOS 5.1, PHP 5.5 client: MAC OSX 10.10.4, IOS 8.4)
-(void)postTest:(NSString *) account and:(NSString *) password{
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:@"http://192.168.1.104/test.php"]];
[request setHTTPMethod:@"POST"];
[request addValue:@"postValues" forHTTPHeaderField:@"METHOD"];
NSMutableDictionary *dictionary = [[NSMutableDictionary alloc] init];
[dictionary setValue:account forKey:@"account"];
[dictionary setValue:password forKey:@"password"];
NSData *data = [[dictionary copy] JSONValue];
[request setHTTPBody:data]; //set the data as the post body
[request addValue:[NSString stringWithFormat:@"%d",data.length] forHTTPHeaderField:@"Content-Length"];
NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:request delegate:self];
if(!connection){
NSLog(@"Connection Failed");
}
}
After POST, the code here handle with the received JSON data which is sent from server
+(NSDictionary*)dictionaryWithJSONData:(NSData*)data{
NSError *error = nil;
NSDictionary *result = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:&error];
if(error){
NSLog(@"%@",error);
return nil;
};
return result;
}
In my main view controller, a simple display like this
-(void)requestReturnedData:(NSData *)data{ //activated when data is returned
NSDictionary *dictionary = [NSDictionary dictionaryWithJSONData:data];
NSLog(@"%@",dictionary);
NSLog(@"%@",data);
}
The server side PHP code:
if($_POST == null){
$handle = fopen('php://input', 'r');
$rawData = fgets($handle);
$body = json_decode($rawData,true);
$file = fopen("testf.txt","a+");
fwrite($file,$body['account']);
fwrite($file,$body['password']);
fclose($file);
}
else{
$body == $_POST;
}
echo json_encode($body);
}
The error message showed by XCODE is like:
2015-08-10 23:49:35.170 Client_Test[8878:114989] Error Domain=NSCocoaErrorDomain Code=3840 "The operation couldn’t be completed. (Cocoa error 3840.)" (JSON text did not start with array or object and option to allow fragments not set.) UserInfo=0x7f8ff27eaea0 {NSDebugDescription=JSON text did not start with array or object and option to allow fragments not set.}
2015-08-10 23:49:35.170 Client_Test[8878:114989] (null)
2015-08-10 23:49:35.171 Client_Test[8878:114989] <0a3c6874 6d6c206c 616e673d 22656e22 3e0a2020 3c686561 643e0a20 2020203c 7469746c 653e6462 5f746573 743c2f74 69746c65 3e0a2020 3c2f6865 61643e0a 20203c62 6f64793e 0a20203c 2f626f64 793e0a3c 2f68746d 6c3e0a0a>
The most strange part here is I also test the same php code in my mac with localhost server, It works, and XCODE showed the complete JSON data.
In the cent os server, I checked the file(testf.txt) that created by the php code, the data was correct. So the main problem is the response of server while POST.
Please give some advices, thanks a lot!