美文网首页网络
iOS--AFNetworking 3.0向服务器传递appli

iOS--AFNetworking 3.0向服务器传递appli

作者: 阿木马克 | 来源:发表于2017-08-13 00:05 被阅读455次

    1.初始化字典并设置key-value。
    (注意:字典参数不需要转成Json)

    NSMutableDictionary *params = [[NSMutableDictionary alloc] init];
    [dic setObject:@"balabala" forKey:@"Name"];
    [dic setObject:@"123456" forKey:@"Id"];
    ...
    
    

    2.设置AFHTTPSessionManager:

    //这里配置请求类型为Json
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    manager.requestSerializer = [AFJSONRequestSerializer serializer];
    manager.responseSerializer = [AFJSONResponseSerializer serializer];
    [manager.requestSerializer setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
    
    

    3.正常发起Post请求:

     [manager POST:url parameters: params progress:^(NSProgress * _Nonnull uploadProgress) {
            
            
        } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
            
            MYLog(@"success");
            
            NSHTTPURLResponse * responses = (NSHTTPURLResponse *)task.response;
            
            if ([responses statusCode] == 200) {
                
               //success
              do u method...
                
            }
            
            
        } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
            
            MYLog(@"failure");
            
        }];
    

    相关文章

      网友评论

        本文标题:iOS--AFNetworking 3.0向服务器传递appli

        本文链接:https://www.haomeiwen.com/subject/uvlsrxtx.html