美文网首页iOS开发
解决AFNetworking升级后出现的Unable to co

解决AFNetworking升级后出现的Unable to co

作者: Lskyu | 来源:发表于2018-11-04 17:25 被阅读0次

    先要确定是哪个引起的不能解析。

    /**单例一个AFHTTPSessionManager*/
    + (AFHTTPSessionManager *)defaultManager{
        static AFHTTPSessionManager *manager;
        static dispatch_once_t onceToken;
        dispatch_once(&onceToken, ^{
            manager = [AFHTTPSessionManager manager];
            //设置接受解析的内容
            manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"text/html",@"text/json",@"text/plain",@"text/javascript",@"application/json", nil];
    //需要驾驶下面两行
            manager.requestSerializer = [AFHTTPRequestSerializer serializer];
            manager.responseSerializer = [AFHTTPResponseSerializer serializer];
        });
        return manager;
    }
    
    +(id)GET:(NSString *)path parameters:(NSDictionary *)params completionHandle:(void (^)(id _Nonnull, NSError * _Nullable))completed{
        NSLog(@"Request Method:GET,Path:%@,params:%@",path,params);
        return [[self defaultManager] GET:path parameters:params progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
          NSLog(@"%@",task.response);//打印头文件,确定编码方式
           //关键代码 
            NSError * error;
            NSStringEncoding enc = CFStringConvertEncodingToNSStringEncoding (kCFStringEncodingGB_18030_2000);
            NSString* strdata = [[NSString alloc]initWithData:responseObject encoding:enc];//在将NSString类型转为NSData
            
            NSData * data = [strdata dataUsingEncoding:NSUTF8StringEncoding];//这样解决的乱码问题。
            
            NSDictionary *json = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:&error ];
            
            NSLog(@"%@",json);
            completed(json,nil);
        } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
            NSLog(@"GET ERROR:%@",error.userInfo);
        }];
       
    }
    

    相关文章

      网友评论

        本文标题:解决AFNetworking升级后出现的Unable to co

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