objective-c – 如何创建json并将其发布到Web服务目标c
发布时间:2020-12-16 07:15:45 所属栏目:百科 来源:网络整理
导读:我尝试将NSDictionary转换为 JSON数据,并使用setHTTPBody在“POST”请求中将其发送到 PHP.server. 当我从我的应用程序发送时,我从服务器收到一个空值,但是当我从PostMan发送JSON时,我收到了这些对象. 我哪里错了? - (void)viewDidLoad{ [super viewDidLoad]
我尝试将NSDictionary转换为
JSON数据,并使用setHTTPBody在“POST”请求中将其发送到
PHP.server.
当我从我的应用程序发送时,我从服务器收到一个空值,但是当我从PostMan发送JSON时,我收到了这些对象. 我哪里错了? - (void)viewDidLoad { [super viewDidLoad]; NSError *error = nil; NSString *url = [NSString stringWithFormat:@"http://myAddress/sql_service.php"]; NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:url]]; [request setHTTPMethod:@"POST"]; [request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"]; [request setValue:@"application/json" forHTTPHeaderField:@"Accept"]; NSArray *arrayOfStrings = @[@"alex",@"dima"]; NSDictionary *dict = @{@"request_type" : @"select_with_params",@"table" : @"user",@"where" : @"f_name=? OR f_name=?",@"values" : arrayOfStrings}; NSData* jsonData1 = [NSJSONSerialization dataWithJSONObject:dict options:kNilOptions error:&error]; [request setHTTPBody:jsonData1]; NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:request delegate:self]; [connection start]; } - (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data { if (data) { [receivedData appendData:data]; } else { } } - (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error { NSLog(@"didFailWithError"); } - (void)connectionDidFinishLoading:(NSURLConnection *)connection { NSError * error = nil; NSMutableDictionary *dictionary = [NSJSONSerialization JSONObjectWithData:receivedData options:0 error:&error]; NSLog(@"connectionDidFinishLoading"); } 这是我需要发布的json. { request_type: "select_with_params",table: "user",where: "f_name=? OR f_name=?",values: ["dima","alex"] } jsonData1不是零. didReceiveData中的数据是: 解决方法
尝试AFNetworking
编辑 NSString *url = [NSString stringWithFormat:@"http://myAddress/sql_service.php"]; NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:url]]; [request setHTTPMethod:@"POST"]; [request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"]; [request setValue:@"application/json" forHTTPHeaderField:@"Accept"]; NSArray *arrayOfStrings = @[@"alex",@"dima"]; NSDictionary *dict = @{@"request_type" : @"select_with_params",@"values" : arrayOfStrings}; NSData* jsonData1 = [NSJSONSerialization dataWithJSONObject:dict options:kNilOptions error:&error]; [request setHTTPBody: [[NSString stringWithFormat:@"%@",jsonData1] dataUsingEncoding:NSUTF8StringEncoding]]; AFHTTPRequestOperation *op = [[AFHTTPRequestOperation alloc] initWithRequest:request]; op.responseSerializer = [AFJSONResponseSerializer serializer]; [op setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation,id responSEObject){ if (responSEObject) { NSLog(@"Success!"); }} failure:^(AFHTTPRequestOperation *operation,NSError *error) { NSLog(@"Error"); }]; [op start]; 希望这可以帮助 (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |