本文通過實(shí)例代碼給大家講解iOS中以application/json上傳文件的形式,具體內(nèi)容詳情大家參考下本文。
在和sever后臺(tái)交互的過程中、有時(shí)候、他們需要我們iOS開發(fā)者以“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" ; //設(shè)置請(qǐng)求頭 [request setValue:@ "application/json" forHTTPHeaderField:@ "Content-Type" ]; //設(shè)置請(qǐng)求體 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); } |
總結(jié)
以上所述是小編給大家介紹的iOS開發(fā)中以application/json上傳文件實(shí)例詳解,希望對(duì)大家有所幫助,如果大家有任何疑問請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)服務(wù)器之家網(wǎng)站的支持!
原文鏈接:http://www.cnblogs.com/jukaiit/p/6669939.html