iOS網絡-AFNetworking序列化

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模式示例代碼:html

-(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模式示例代碼:json

-(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); }]; }
相關文章
相關標籤/搜索