APP端实现代码:
- (IBAction)uploadAction:(id)sender {
mutData = [[NSMutableData alloc] init];
NSURL *url = [NSURL URLWithString:@"http://192.168.222.111/Local/PostDemo.php"];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url cachePolicy:NSURLRequestReloadIgnoringCacheData timeoutInterval:30.0];
[request setHTTPMethod:@"POST"];
NSMutableDictionary *dict = [[NSMutableDictionary alloc] initWithObjectsAndKeys:@"1.1.1", @"version",@"1",@"type",@"open", @"sw", nil];
NSData *postData = [NSJSONSerialization dataWithJSONObject:dict options:1 error:nil];
[request setHTTPBody:postData];
[NSURLConnection connectionWithRequest:request delegate:self];
}
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response {
NSLog(@"%s", __func__);
mutData.length = 0;
}
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
NSLog(@"%s", __func__);
[mutData appendData:data];
}
- (void)connectionDidFinishLoading:(NSURLConnection *)connection {
NSLog(@"%s", __func__);
NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:mutData options:1 error:nil];
NSLog(@"%@",dic);
}
- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error {
NSLog(@"%s", __func__);
}
服务端实现代码:
<?
if (!empty($GLOBALS['HTTP_RAW_POST_DATA']))
{
$data = isset($GLOBALS['HTTP_RAW_POST_DATA']) ? $GLOBALS['HTTP_RAW_POST_DATA'] : file_get_contents("php://input");
$json = json_decode($data,TRUE);
echo json_encode($json);
}else if(file_get_contents("php://input")){
$data = file_get_contents("php://input");
$json = json_decode($data,TRUE);
echo json_encode($json);
}else{
echo json_encode("Error!");
}
?>
上传后再返回原本数据DICTIONARY.