美文网首页
iOS开发问题

iOS开发问题

作者: Mi欧阳 | 来源:发表于2021-07-11 16:43 被阅读0次

    AFNetworking的body如何传json类型

    parameters加入body中,UTF-8格式

    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    //发送类型(二进制格式)
    manager.requestSerializer = [AFHTTPRequestSerializer serializer];
    //接收类型(二进制格式)
    manager.responseSerializer = [AFHTTPResponseSerializer serializer];
    //超时时间
    manager.requestSerializer.timeoutInterval = 20.0;
    
    [manager PUT:URLString parameters:parameters success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        //查看body内容,data转string
        if(task.originalRequest.HTTPBody != nil){
            NSString *str = [[NSString alloc] initWithData:task.originalRequest.HTTPBody encoding:kCFStringEncodingUTF8];
            NSLog(@"成功,打印httpBody:%@", str);
        }
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        //查看body内容,data转string
        if(task.originalRequest.HTTPBody != nil){
            NSString *str = [[NSString alloc] initWithData:task.originalRequest.HTTPBody encoding:kCFStringEncodingUTF8];
            NSLog(@"成功,打印httpBody:%@", str);
        }
    }];
    
    

    parameters加入body中,JSON格式

    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    //发送类型(JSON格式)
    manager.requestSerializer = [AFJSONRequestSerializer serializer];
    //接收类型(二进制格式)
    manager.responseSerializer = [AFHTTPResponseSerializer serializer];
    //超时时间
    manager.requestSerializer.timeoutInterval = 20.0;
    //写与不写对最终结果不造成影响
    [manager.requestSerializer setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
    //写与不写对最终结果不造成影响
    [manager.requestSerializer setValue:@"application/json" forHTTPHeaderField:@"Accept"];
    
    [manager PUT:URLString parameters:parameters success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        //查看body内容,json解析
        if(task.originalRequest.HTTPBody != nil){
            NSString *str = [NSJSONSerialization JSONObjectWithData:task.originalRequest.HTTPBody options:0 error:nil];
            NSLog(@"成功,打印httpBody:%@", str);
        }
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        //查看body内容,json解析
        if(task.originalRequest.HTTPBody != nil){
            NSString *str = [NSJSONSerialization JSONObjectWithData:task.originalRequest.HTTPBody options:0 error:nil];
            NSLog(@"成功,打印httpBody:%@", str);
        }
    }];
    
    

    附:一种错误的写法

    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    //发送类型(二进制格式)
    manager.requestSerializer = [AFHTTPRequestSerializer serializer];
    //接收类型(二进制格式)
    manager.responseSerializer = [AFHTTPResponseSerializer serializer];
    //超时时间
    manager.requestSerializer.timeoutInterval = 20.0;
    //这里一旦多加这句,就会造成400错误。数据解析失败。AFNetworking没法正常解析需发送的body
    [manager.requestSerializer setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
    
    [manager PUT:URLString parameters:parameters success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        //查看body内容,data转string
        if(task.originalRequest.HTTPBody != nil){
            NSString *str = [[NSString alloc] initWithData:task.originalRequest.HTTPBody encoding:kCFStringEncodingUTF8];
            NSLog(@"成功,打印httpBody:%@", str);
        }
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        //查看body内容,data转string
        if(task.originalRequest.HTTPBody != nil){
            NSString *str = [[NSString alloc] initWithData:task.originalRequest.HTTPBody encoding:kCFStringEncodingUTF8];
            NSLog(@"成功,打印httpBody:%@", str);
        }
    }];
    
    

    相关文章

      网友评论

          本文标题:iOS开发问题

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