本文通过实例代码给大家讲解iOS中以application/json上传文件的形式,具体内容详情大家参考下本文。
在和sever后台交互的过程中、有时候、他们需要我们iOS开发者以“application/json”形式上传。
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
|
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开发中以application/json上传文件实例详解,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对服务器之家网站的支持!
原文链接:http://www.cnblogs.com/jukaiit/p/6669939.html