在做app应用的时候,需要将数据提交到服务器去存储,那么方法可以参考如下,哈,自己搜索整理的
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59
| NSString *postString = [NSString stringWithFormat:@"app_key=XXXXXXXXXXXXXXXXXX"]; postString = [postString stringByAppendingString:[NSString stringWithFormat:@"&device_token=%@",deviceId]]; postString = [postString stringByAppendingString:[NSString stringWithFormat:@"&teleplay_title=%@",title]]; postString = [postString stringByAppendingString:[NSString stringWithFormat:@"&job_type=%@",type]]; postString = [postString stringByAppendingString:[NSString stringWithFormat:@"&deadline=%@",time]]; postString = [postString stringByAppendingString:[NSString stringWithFormat:@"&age=%@",age]]; postString = [postString stringByAppendingString:[NSString stringWithFormat:@"&sex=%@",sex]]; postString = [postString stringByAppendingString:[NSString stringWithFormat:@"&people_num=%@",num]]; postString = [postString stringByAppendingString:[NSString stringWithFormat:@"&contact_person=%@",person]]; postString = [postString stringByAppendingString:[NSString stringWithFormat:@"&contact=%@",phone]]; postString = [postString stringByAppendingString:[NSString stringWithFormat:@"&description=%@",description]];
NSLog(@"postString:%@",postString);
NSData *postData = [postString dataUsingEncoding:NSUTF8StringEncoding allowLossyConversion:YES];
NSString *postLength = [NSString stringWithFormat:@"%d",[postData length]]; NSLog(@"postLength=%@",postLength);
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
[request setURL:[NSURL URLWithString:[NSString stringWithFormat:@"http://www.baidu.com"]]];
[request setHTTPMethod:@"POST"];
[request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
[request setValue:postLength forHTTPHeaderField:@"Content-Length"];
[request setHTTPBody:postData];
NSHTTPURLResponse* urlResponse = nil; NSError *error = [[NSError alloc] init];
[UIApplication sharedApplication].networkActivityIndicatorVisible = YES;
NSData *responseData = [NSURLConnection sendSynchronousRequest:request returningResponse:&urlResponse error:&error];
NSError *jsonError = [[NSError alloc] init]; NSDictionary *personDictionary = [NSJSONSerialization JSONObjectWithData:responseData options:NSJSONReadingMutableContainers error:&jsonError]; NSString *status = [personDictionary objectForKey:@"status"];
if ([@"ok" compare:status] == NSOrderedSame) { [UIApplication sharedApplication].networkActivityIndicatorVisible = NO; return YES; } return NO;
|