AFN它内部默认把服务器响应的数据当做json来进行解析,所以如果服务器返回给我的不是JSON数据那么请求报错,这个时候需要设置AFN对响应信息的解析方式。AFN提供了三种解析响应信息的方式,分别是:
AFJSONResponseSerializer---------JSON 默认模式
AFXMLParserResponseSerializer---XML
AFHTTPResponseSerializer---------二进制响应数据
还有一种情况就是服务器返回给我们的数据格式不太一致(开发者工具Content-Type:text/xml),那么这种情况也有可能请求不成功。解决方法:
直接在源代码中修改,添加相应的Content-Type
拿到这个属性,添加到它的集合中
AFJSONResponseSerializer模式示例代码:
-(void)json { //1.创建会话管理者 AFHTTPSessionManager* manager = [AFHTTPSessionManager manager]; NSDictionary *dictM = @{ @"username":@"520it", @"pwd":@"520it", @"type":@"JSON" }; //2.发送GET [manager GET:@"http://120.25.226.186:32812/login" parameters:dictM progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) { NSLog(@"成功--%@",responseObject); } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) { NSLog(@"失败--%@",error); }]; }
AFXMLParserResponseSerializer模式示例代码:
-(void)xml { //1.创建会话管理者 AFHTTPSessionManager* manager = [AFHTTPSessionManager manager]; NSDictionary *dictM = @{ @"type":@"XML" }; manager.responseSerializer = [AFXMLParserResponseSerializer serializer]; //2.发送GET [manager GET:@"http://120.25.226.186:32812/video" parameters:dictM progress:nil success:^(NSURLSessionDataTask * _Nonnull task, NSXMLParser *parser) { //设置代理 parser.delegate = self; //开始解析 [parser parse]; } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) { NSLog(@"失败--%@",error); }]; } #pragma mark - NSXMLParserDelegate -(void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary<NSString *,NSString *> *)attributeDict { NSLog(@"%@---%@",elementName,attributeDict); }
AFHTTPResponseSerializer
模式示例代码:
-(void)http { //1.创建会话管理者 AFHTTPSessionManager* manager = [AFHTTPSessionManager manager]; NSString *urlStr = @"http://img4q.duitang.com/uploads/item/201411/21/20141121231731_PkXNk.jpeg"; //调整AFN的解析方式 // manager.responseSerializer = [AFJSONRequestSerializer serializer];//默认 manager.responseSerializer = [AFHTTPResponseSerializer serializer]; //2.发送GET [manager GET:urlStr parameters:nil progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) { NSLog(@"成功--%@",[responseObject class]); NSLog(@"%@",[NSThread currentThread]); self.imageView.image = [UIImage imageWithData:responseObject]; } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) { NSLog(@"失败--%@",error); }]; }
-(void)http2 { //1.创建会话管理者 AFHTTPSessionManager* manager = [AFHTTPSessionManager manager]; NSString *urlStr = @"http://www.baidu.com"; //调整AFN的解析方式 如果报错(text/html是在报错里面提供的),百分之七八十都是因为缺少这俩句代码原因 manager.responseSerializer.acceptableContentTypes = [NSSet setWithObject:@"text/html"]; //Error Domain=com.alamofire.error.serialization.response Code=-1016 "Request failed: unacceptable content-type: text/html",缺少上面代码报的错误 manager.responseSerializer = [AFHTTPResponseSerializer serializer]; //Error Domain=NSCocoaErrorDomain Code=3840 "JSON text did not start with array or object and option to allow fragments not set.",缺少上面代码报的错误 //2.发送GET [manager GET:urlStr parameters:nil progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) { NSLog(@"成功--%@",[responseObject class]); } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) { NSLog(@"失败--%@",error); }]; }
时间: 2024-10-10 01:53:24