MKNetworkKit的基本用法(Get请求、Post请求、文件上传)

1、Get请求

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
// Get请求
        // 初始化网络引擎对象
        MKNetworkEngine *engine = [[MKNetworkEngine alloc] initWithHostName:@"192.168.2.176:9502/api" customHeaderFields:nil];
 
        // 创建一个Get请求
        MKNetworkOperation *op = [engine operationWithPath:@"login.php?userid=admin&userpwd=123" params:nil httpMethod:@"GET"];
 
        // 设置Get请求处理方式
        [op onCompletion:^(MKNetworkOperation *operation){        // 请求成功
 
        NSLog(@"request string: %@",[operation responseString]);
 
    } onError:^(NSError *error){        // 请求失败
 
                    NSLog(@"%@", error);
 
    }];
 
        // 入列操作(发起网络请求)
        [engine enqueueOperation:op];

2、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
// Post请求
        // 初始化网络引擎对象
        MKNetworkEngine *engine = [[MKNetworkEngine alloc] initWithHostName:@"192.168.2.176:9502/api" customHeaderFields:nil];
 
        // 创建请求参数
        NSMutableDictionary *dic = [[NSMutableDictionary alloc] init];
        [dic setValue:@"admin" forKey:@"username"];
        [dic setValue:@"123" forKey:@"password"];
 
  // 创建一个Post请求
        MKNetworkOperation *op = [engine operationWithPath:@"user.do" params:dic httpMethod:@"POST"];
 
  // 创建Post请求处理方式
        [op onCompletion:^(MKNetworkOperation *operation){
 
        NSLog(@"post response string :%@",[operation responseString]);  
 
    } onError:^(NSError *error) {
 
        NSLog(@"%@", error);
    }];
 
  // 入列操作(发起网络请求) 
        [engine enqueueOperation:op];

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
35
36
37
38
  // 上传操作
  // 初始化网络引擎对象
        MKNetworkEngine *engine = [[MKNetworkEngine alloc] initWithHostName:@"192.168.2.176:9502/api" customHeaderFields:nil];
 
        // 创建请求参数
        NSDictionary *dict = [NSDictionary dictionaryWithObjectsAndKeys:@"admin", @"username", @"123", @"password",nil]
 
  // 创建一个Post请求        
        MKNetworkOperation *op = [engine operationWithPath:@"upload" params:dict httpMethod:@"POST"];  
 
  // 在Post请求中附件文件及指定文件类型
        NSString *filePath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"SampleImage.jpg"]; 
        NSString *fileType = @"jpg";  
        [op addFile:filePath forKey:@"media" mimeType:fileType];  
 
        // 设置冻结属性,可在恢复网络时自动上传  
        [op setFreezable:YES];  
 
  // 设置上传处理方式
        [op addCompletionHandler:^(MKNetworkOperation* completedOperation) {  
 
                        NSString *responseString = [completedOperation responseString];  
                        NSLog(@"server response: %@",responseString);  
 
        } errorHandler:^(MKNetworkOperation *errorOp, NSError* err){  
 
                        NSLog(@"Upload file error: %@", err);  
 
        }];  
 
  // 入列操作(发起网络请求)
        [engine enqueueOperation:op];  
 
  // 上传进度
        [op onUploadProgressChanged:^(double progress) {  
 
                        DLog(@"Upload file progress: %.2f", progress*100.0);  
        }];

Leave a Reply