美文网首页
AFNetworking不支持text/plain,错误解决方法

AFNetworking不支持text/plain,错误解决方法

作者: H_A_N | 来源:发表于2017-09-16 16:42 被阅读0次

    /**
    * 错误解决方法"Request failed: unacceptable content-type: text/plain"
    * 1.添加一句代码:
    * AFHTTPSessionManager *session.responseSerializer = [[AFCompoundResponseSerializer alloc] init];
    * 2.在头文件"AFURLResponseSerialization.m"的如下地方添加text/plain就行了
    * self.acceptableContentTypes = [NSSet setWithObjects:@"application/json", @"text/plain",@"text/json", @"text/javascript", nil];
    */

    使用nfnetworking上传图片:

    • (void)addTouXiangWithImage:(UIImage *)image andImagePath:(NSString *)imagePath {
     NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration defaultSessionConfiguration];
    NSString *urlString = [NSString stringWithFormat:@"%@addtouxiang.do?file=%@",IP_AND_WWW,imagePath];
    

    // NSString urlString = [NSString stringWithFormat:@"%@addtouxiang.do",IP_AND_WWW];
    AFHTTPSessionManager managerS = [[AFHTTPSessionManager alloc] initWithSessionConfiguration:configuration];
    /

    * 错误解决方法"Request failed: unacceptable content-type: text/plain"
    * 1.添加一句代码:
    * AFHTTPSessionManager *session.responseSerializer = [[AFCompoundResponseSerializer alloc] init];
    * 2.在头文件"AFURLResponseSerialization.m"的如下地方添加text/plain就行了
    * self.acceptableContentTypes = [NSSet setWithObjects:@"application/json", @"text/plain",@"text/json", @"text/javascript", nil];
    */
    managerS.responseSerializer = [[AFCompoundResponseSerializer alloc] init];
    NSURLSessionDataTask *taskUpload = [managerS POST:urlString parameters:nil constructingBodyWithBlock:^(id<AFMultipartFormData> _Nonnull formData) {

        // 上传图片,以文件流的格式
        NSData *imageData = UIImageJPEGRepresentation(image, 1);
        [formData appendPartWithFileData:imageData name:@"current_Image.png" fileName:imagePath mimeType:@"image/png"];
    } progress:^(NSProgress * _Nonnull uploadProgress) {
        
        
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        
        [self.upLoadImageHUD hide:NO];
        [self showHint:@"图片上传成功"];
        
        //IOS5自带解析类NSJSONSerialization从response中解析出数据放到字典中
        NSError *error = nil;
        NSDictionary *imageDic = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableLeaves error:&error];
        NSArray *imageArr = [imageDic objectForKey:@"list"];
        NSLog(@"imageDic------->>>>>%@,%@",imageDic,[[imageArr firstObject] objectForKey:@"url"]);
        
        //获取个人信息中的头像
        [self addTouXiangs:[[imageArr firstObject] objectForKey:@"url"]];
        
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        
        [self.upLoadImageHUD hide:NO];
        [self showHint:error.userInfo[NSLocalizedDescriptionKey]];
        ALog(@"%@",error.userInfo);
        
        
    }];
     
    [taskUpload resume];
    

    }

    相关文章

      网友评论

          本文标题:AFNetworking不支持text/plain,错误解决方法

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