ios发送HTTP请求和操作JSON
来源:互联网 发布:二次型的矩阵的标准型 编辑:程序博客网 时间:2024/06/06 00:18
发送HTTP请求的代码:
-(void) queryUserDataFromServer{ YLSClientInfo *clientInfo = [YLSClientInfo new]; NSString *versionCode = clientInfo.currentClientVersion;// 应用当前版本号 YLSUserData *userData = [YLSUserDataUtil readUserData]; NSString *userId = userData.userId;// userId NSString *url = [QUERY_USER_BY_USERNAME_SERVICE_URL stringByAppendingString:userId]; NSMutableURLRequest *request = [NSMutableURLRequest new]; [request setURL:[NSURL URLWithString:url]]; [request setHTTPMethod:@"GET"]; [request setValue:@"true" forHTTPHeaderField:@"xhr"]; [request setValue:versionCode forHTTPHeaderField:@"clientVersionCode"]; loginResponse = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];}
另外,ios会自动处理cookie的保存和发送,一般不需要特别编码处理
操作JSON的代码:
NSError *error = nil; NSDictionary *jsonArray = [NSJSONSerialization JSONObjectWithData:loginResponse options:kNilOptions error:&error];// loginResponse是NSData类型,包含两个key,分别是code和result NSDictionary *result = [jsonArray objectForKey:@"result"];// 取出result字段 NSString *enterpriseId = [result objectForKey:@"enterpriseId"]; // 其他字段 NSDictionary *features = [result objectForKey:@"versionFeature"];// versionFeature是嵌套的另一个object NSNumber *maxServiceCount = [features objectForKey:@"maxServiceCount"]; // 其他字段
HTTP响应的返回值是一个NSData*,用ios原生的NSJSONSerialization类就可以处理。上面代码处理的文本格式是:
{ "result": { "enterpriseId":xxxx, "userId":xxxx, "versionFeature": { "maxServiceCount":20, "maxEmployeeCount":5 } }, "code":0}
- ios发送HTTP请求和操作JSON
- http+json发送请求
- ios发送http GET和POST请求
- iOS发送http请求
- android http 发送json请求
- 通过json发送Http请求
- java 发送http json请求
- 通过json发送Http请求
- java 发送http json请求
- java 发送http json请求
- 重温 ios发送http GET和POST请求
- Android发送http请求并解析json
- RestTemplate 发送json格式的http请求
- Python发送http请求(json格式)
- python 发送HTTP请求 post json 格式
- iOS上的http请求和JSON解析
- Http Post发送json序列请求(json序列化和反序列化)
- Http Post发送json序列请求(json序列化和反序列化)
- AndroidAnnotations——Injecting SystemServices注入系统服务
- 打印输出沙盒地址
- 帝国cms 搜索功能总结
- Delphi图像处理 -- RGB与HSV转换
- POJ 2773 Happy 2006
- ios发送HTTP请求和操作JSON
- javascript的window.onload方法使用
- mstsc远程连接
- AndroidAnnotations——Injecting html注入html文本
- Unity中连线
- JS随机数的几种用法
- C++ 歌手大奖赛计分系列
- 树状数组小结--转自风神Blog
- 第十五周 项目3--学生成绩单