標籤:
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); }];}
iOS網路-AFNetworking序列化