iOS开发中以application/json上传文件实例详解
时间:2021-07-08 14:49:33|栏目:iOS代码|点击: 次
本文通过实例代码给大家讲解iOS中以application/json上传文件的形式,具体内容详情大家参考下本文。
在和sever后台交互的过程中、有时候、他们需要我们iOS开发者以“application/json”形式上传。
NSString *accessUrl = [NSString stringWithFormat:@"%@/xxx",@"https://www.xxxxx.com:xxxx"]; NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:accessUrl]]; request.HTTPMethod = @"POST"; //设置请求头 [request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"]; //设置请求体 NSMutableData *body = [NSMutableData data]; [body appendData:[jsonStr dataUsingEncoding:NSUTF8StringEncoding]]; [request setHTTPBody:body]; NSHTTPURLResponse* urlResponse = nil; NSError *error = [[NSError alloc] init]; NSData *responseData = [NSURLConnection sendSynchronousRequest:request returningResponse:&urlResponse error:&error]; NSString *result = [[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding]; if (result == nil) { NSLog(@"json解析失败!"); } else { NSData *jsonData = [result dataUsingEncoding:NSUTF8StringEncoding]; NSError *err; NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:jsonData options:NSJSONReadingMutableContainers error:&err]; if(err) { NSLog(@"json解析失败:%@",err); } success(dic); }
总结
栏 目:iOS代码
下一篇:详解ios中自定义cell,自定义UITableViewCell
本文标题:iOS开发中以application/json上传文件实例详解
本文地址:http://www.codeinn.net/misctech/154518.html