在做app应用的时候,需要将数据提交到服务器去存储,那么方法可以参考如下
2014-09-13 16:12
513 查看
http://www.gowhich.com/blog/186
//参数1名字=参数1数据&参数2名字=参数2数据&参数3名字=参数3数据&... 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); //将NSSrring格式的参数转换格式为NSData,POST提交必须用NSData数据。 NSData *postData = [postString dataUsingEncoding:NSUTF8StringEncoding allowLossyConversion:YES]; //计算POST提交数据的长度 NSString *postLength = [NSString stringWithFormat:@"%d",[postData length]]; NSLog(@"postLength=%@",postLength); //定义NSMutableURLRequest NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init]; //设置提交目的url [request setURL:[NSURL URLWithString:[NSString stringWithFormat:@"http://www.baidu.com"]]]; //设置提交方式为 POST [request setHTTPMethod:@"POST"]; //设置http-header:Content-Type //这里设置为 application/x-www-form-urlencoded ,如果设置为其它的,比如text/html;charset=utf-8,或者 text/html 等,都会出错。不知道什么原因。 [request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"]; //设置http-header:Content-Length [request setValue:postLength forHTTPHeaderField:@"Content-Length"]; //设置需要post提交的内容 [request setHTTPBody:postData]; //定义 NSHTTPURLResponse* urlResponse = nil; NSError *error = [[NSError alloc] init]; //设置网络状态显示 [UIApplication sharedApplication].networkActivityIndicatorVisible = YES; //同步提交:POST提交并等待返回值(同步),返回值是NSData类型。 NSData *responseData = [NSURLConnection sendSynchronousRequest:request returningResponse:&urlResponse error:&error]; //将NSData类型的返回值转换成NSString类型 // NSString *result = [[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding]; //将NSData装换为字典类型 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;
相关文章推荐
- 在做app应用的时候,需要将数据提交到服务器去存储,那么方法可以参考如下
- 在做app应用的时候,需要将数据提交到服务器去存储,那么方法可以参考如下
- 当应用继承ContentProvider类,并重写该类用于提供数据和存储数据的方法,就可以向其他应用共享其数据。 虽然使用其他方法也可以对外共享数
- 对于web api 从页面post数据到web api,如果用ajax,则可以成功上传到服务器,需要一个类来存储那些变量
- Android应用开发中,会经常要提交数据到服务器和从服务器得到数据,本文主要是给出了利用http协议采用HttpClient方式向服务器提交数据的方法。
- 应用开发框架之——根据数据表中的存储的方法名称来调用方法
- 求SQL数据库同步方法或程序参考(可以在同一服务器中两数据库之间),要不毕设晚了
- jquery ajax return没有返回值的解决方法,需要的朋友可以参考下。
- Android提交数据到服务器的两种方式四种方法
- 服务器硬件知识普及篇(需要配置服务器的朋友可以参考)第1/7页
- 利用js提交数据给服务器 可以很秘密的提交数据到服务器 运行会访问 一个gif文件
- Asp.net 更改Alert样式和后台注册Jquery的Ajax异步提交事件[包括处理前操作、处理后操作(注明:此方法只是作为参考,如有需要可自行修改)]
- JS 截取字符串substr 和 substring方法的区别,需要的朋友可以参考下,根据需要自行选择。
- SQL2000系统表、存储过程、函数的功能介绍及应用2009年01月21日 星期三 11:38虽然使用系统存储过程、系统函数与信息架构视图已经可以为我们提供了相当丰富的元数据信息,但是对于某些特殊的元数据信息,我们仍然需要直接对系统表进行查询。因为SQL
- QTP在录制脚本的时候会将鼠标点击过的对象自动添加到对象库中,而那些未被点击过的对象将不会被添加,导致针对某一对象手动添加的脚本运行不通过,因此需要手动把测试对象添加到对象库中,方法如下:
- Android提交数据到服务器的两种方式四种方法
- qt中数据存储方法(接口)的思路应用1(thinkvd开发日志)
- WCF客户端动态设置WCF服务器主机的地址的方法参考,可以连接多个相同WCF主机的方法
- C# 模拟post数据提交时 出现如下错误: System.Net.WebException: 远程服务器返回错误: (417) Expectation Failed 的解决办法
- Android提交数据到服务器的两种方式四种方法