AFNetworking 2.0 使用总结
iOS应用开发,存在网络请求是在所难免的,而AFNetworking是一个很不错的选择。以下是本人对AFNetworking请求的一些总结,但愿能帮到大家!
1.JSON Post请求
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
|
NSString *strName = @ "user1" ; NSString *strPsw = @ "pwd1" ; AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager]; manager.requestSerializer = [AFJSONRequestSerializer serializer]; NSDictionary *parameters = [NSDictionary dictionaryWithObjectsAndKeys:strName, @ "user" , strPsw, @ "password" , nil]; NSString *strURL = [GT123_ServerHost stringByAppendingString:GT123_LogonServlet]; [manager POST:strURL parameters:parameters success:^(AFHTTPRequestOperation *operation, id responseObject) { if (responseObject != nil) { NSDictionary *dicRes = responseObject; NSString *strState = [dicRes objectForKey:@ "state" ]; if (strState && [strState isEqualToString:@ "success" ]) { NSString *strSession = [dicRes objectForKey:@ "session" ]; NSLog(@ "%@" , strSession); } else { } } else { } } failure:^(AFHTTPRequestOperation *operation, NSError *error) { }]; |
2.JSON Get请求
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
|
long long lTimestamp = [[NSDate date] timeIntervalSince1970] * 1000; AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager]; NSString *strURL = [NSString stringWithFormat:@ "%@%@?max=20&et=%lld" ,GT123_ServerHost,GT123_GetNewsListBeforeServlet, lTimestamp]; [manager GET:strURL parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) { if (responseObject != nil) { NSDictionary *dicRes = responseObject; NSString *strState = [dicRes objectForKey:@ "state" ]; if (strState && [strState isEqualToString:@ "success" ]) { NSArray *lNews = [dicRes objectForKey:@ "post_list" ]; if (lNews && lNews.count > 0) { NSLog(@ "news count - %lu" , (unsigned long )lNews.count); } } else { } } else { } } failure:^(AFHTTPRequestOperation *operation, NSError *error) { }]; |
3.图片上传
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
29
30
31
32
33
34
|
- ( void )uploadImg:(UIImage *)img withName:(NSString *)imgName { NSData *imageData = UIImageJPEGRepresentation(img, 1.0); NSMutableDictionary *parameters = [[NSMutableDictionary alloc] init]; [parameters setObject:imgName forKey:@ "name" ]; [parameters setObject:@ "jpg" forKey:@ "type" ]; AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager]; manager.requestSerializer = [AFJSONRequestSerializer serializer]; NSString *strURL = [GT123_ServerHost stringByAppendingString:GT123_UploadPostPictureServlet]; [manager POST:strURL parameters:parameters constructingBodyWithBlock:^(id<AFMultipartFormData> formData) { if (imageData) { [formData appendPartWithFileData:imageData name:imgName fileName:imgName mimeType:@ "image/jpeg" ]; } } success:^(AFHTTPRequestOperation *operation, id responseObject) { if (responseObject) { NSDictionary *dicRes = responseObject; NSString *strState = [dicRes objectForKey:@ "state" ]; if (strState && [strState isEqualToString:@ "success" ]) { NSLog(@ "upload success!" ); } else { } } else { } } failure:^(AFHTTPRequestOperation *operation, NSError *error) { }]; } |
1
|
[formData appendPartWithFileData:imageData name:imgName fileName:imgName mimeType:@ "image/jpeg" ]; |
返回内容还是一个NSDictionary对象。
AFNetworking请求方面,本人的总结暂时有这些,后续再补上...